Link to home
Start Free TrialLog in
Avatar of Carl3003
Carl3003

asked on

searching trhough a text file.

I have a text file constisting of lines. Each line has 512 char. I am trying to find a way to make a string search and return the number of line where the string was found. The string is a uniqe number. Any help is appriciated.
Thank you
Avatar of shah1d1698
shah1d1698

How are chars arranged on the file?? i.e are the numbers seperated by spaces or else??
Avatar of Carl3003

ASKER

yes they are separted by ':'
this is a copy of only one line, the other lines are similar
myusername:x:userid:300:fName lName:/data/myuserid:/bin/bash

i am asking the user the enter fName and lName and then i am trying to search by userid and to replace the old fName and lName with new one..
steps are..

1. Initialize file pointer.
2. Initialize a variable named lineCounter to zero.
3. Read lines from the txt file and increment the lineCounter at the same time.
4. For each line, parse the whole string into tokens and check against the target string.
5. Report a match with the current value of the lineCounter.  
6. Close the file.


To create tokens of a string, u can use strtok() library function which is defined in string.h header file.
I'll be away for some time..If u run into any problem implementing the above steps, let us know...
ok thanks
what i dont understand is how to read a whole line from the file.
so far down up to here. Still cant get how i can change the fName and the lName..
 char buf[SIZE];
      printf("new gcos :  ");      
    if( fgets(buf, SIZE, stdin) == NULL)
            exit(1);
      else
        buf[strlen(buf)-1] = '\0';

    int fd = open("passwd", O_RDWR);
      FILE *fp=fopen("passwd","r");
    if(fp==NULL)
    {
            //perror(av[1]); exit(1);
    }
      char line[512];
      char current;
      int count=0;
      while(current = getchar())
      {
            if(current=='\n')
            {
                  if(count==512)
                        exit(1);
                  

                  count=0;
            }
            else
            {
                  line[count]=current;
                  ++count;
            }
      }
down=done
Also, i forgot to mention but when the user enter fName  lName the program must make sure that the lines doesnt exceed 512 char per line..
you can use fgets to read a whole line. you can set the max number of characters to read, so you won't read more than 512 bytes.

you do not need to open and fopen the same file. i would suggest you fopen the file and fgets each line, then do a strstr to see if the substring you are looking for is present in the line.

-- Adil
thats done..Any suggestions on how to replace fName and lName with new ones?

FILE *fp=fopen("passwd","r");
    if(fp==NULL)
    {
            //perror(av[1]); exit(1);
    }
      char line[512];
      char current;
      int count=0;
      while(fgets(line, 512, fp)!=NULL)
      {
            char str[10];
            sprintf(str,"%d",getuid());

            if( strstr( line, str)!=NULL)
            {
                  printf("String found");
            }

      }
Are the new and old fName and IName are of same length ??..if so then following segment can be used to replace a portion from the line..

char *startOfSubStr;
while(fgets(line, 512, fp)!=NULL)
     {
          char str[10];
          sprintf(str,"%d",getuid());

          startOfSubStr = strstr(line, str); //startOfSubStr has the starting location of str in line.
          if( startOfSubStr != NULL)
          {
             strncpy(startOfSubStr, str, strlen(str));//replacement occurs.  
             printf("String found and replaced %s", line);
          }
ASKER CERTIFIED SOLUTION
Avatar of shah1d1698
shah1d1698

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial