Re: [C] how to search a string ?

From: Richard Heathfield (dontmail_at_address.co.uk.invalid)
Date: 10/23/03


Date: Thu, 23 Oct 2003 04:24:18 +0000 (UTC)


[Followups set to acllcc++]

Alan wrote:

> I want to search a string in a file, but I don't know the position, how
> can I search the string and stop at that line containing that string ?
>
> for example, I want to seach the string ".ABC" in a file, how to do ?

Read the string one line at a time. If you know the longest line you can
expect, then fgets is suitable for this. If you don't, consider using
something along the same lines as my fgetline function, which you can get
from:

http://users.powernet.co.uk/eton/c/fgetline.h
http://users.powernet.co.uk/eton/c/fgetline.c

Using my code, the problem is easily solved:

#include <stdio.h>
#include <string.h>

#include "fgetline.h"

int main(int argc, char **argv)
{
  if(argc > 2)
  {
    FILE *fp = fopen(argv[1], "r");
    if(fp != NULL)
    {
      char *line = NULL;
      size_t size = 0;
      while(0 == fgetline(&line, &size, (size_t)-1, fp, 0))
      {
        if(strstr(line, argv[2]) != NULL)
        {
          printf("%s\n", line);
        }
      }
      free(line);
      fclose(fp);
    }
    else
    {
      handle the file-opening error correctly
    }
  }
  else
  {
    explain to the user how to use the program
  }

  return 0;
}

If you'd rather use fgets:

#include <stdio.h>
#include <string.h>

#define MAXLINE 1024 /* change this if necessary */

int main(int argc, char **argv)
{
  if(argc > 2)
  {
    FILE *fp = fopen(argv[1], "r");
    if(fp != NULL)
    {
      char line[MAXLINE + 2] = {0};
      while(fgets(line, sizeof line, fp) != NULL)
      {
        if(strstr(line, argv[2]) != NULL)
        {
          printf("%s", line);
        }
      }
      fclose(fp);
    }
    else
    {
      handle the file-opening error correctly
    }
  }
  else
  {
    explain to the user how to use the program
  }

  return 0;
}

-- 
Richard Heathfield : binary@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


Relevant Pages

  • SOLVED: need to call managed code from unmanaged c++ code !
    ... Public Function(ByVal argc as Int32, ... SizeParamIndex:=0)> ByVal argvas string) as Int32 ... I tried the "Public Function(ByVal argc as Int32, ByVal/byRef argv as ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to add thousand separators
    ... First, this code is obsolete as written, because char is a dead data type and should not ... Note that both of these should be stored as string resources since they might need to be ... 18 digits for any reason. ... you have made a VERY SERIOUS DESIGN ERROR. ...
    (microsoft.public.vc.mfc)
  • Re: what is the best way of passing floats into a string
    ... I do not null-terminate as snprintf takes care of this (according to ... But the easiest way to determine the size needed to format a number, ... int length_of_representation(double n,const char* format){ ... I get a nice result of -10.000000 in my char * string. ...
    (comp.unix.programmer)
  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)
  • Re: why I can not write to the file after initialize the MFC in a service program
    ... you don't use char, an obsolete data type ... Why do you need an intermedate buffer to write literal strings anyway? ... For example, if AfxWinInit fails, you copy a 45-character string into a ... So you are going to try to initialize MFC EACH TIME THROUGH THE LOOP? ...
    (microsoft.public.vc.mfc)