?
Solved

searching trhough a text file.

Posted on 2004-11-04
14
Medium Priority
?
198 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:Carl3003
  • 8
  • 5
14 Comments
 
LVL 2

Expert Comment

by:shah1d1698
ID: 12501630
How are chars arranged on the file?? i.e are the numbers seperated by spaces or else??
0
 

Author Comment

by:Carl3003
ID: 12501640
yes they are separted by ':'
0
 

Author Comment

by:Carl3003
ID: 12501682
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..
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:shah1d1698
ID: 12501698
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.
0
 
LVL 2

Expert Comment

by:shah1d1698
ID: 12501778
I'll be away for some time..If u run into any problem implementing the above steps, let us know...
0
 

Author Comment

by:Carl3003
ID: 12501841
ok thanks
0
 

Author Comment

by:Carl3003
ID: 12501890
what i dont understand is how to read a whole line from the file.
0
 

Author Comment

by:Carl3003
ID: 12502103
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;
            }
      }
0
 

Author Comment

by:Carl3003
ID: 12502109
down=done
0
 

Author Comment

by:Carl3003
ID: 12502119
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..
0
 
LVL 2

Expert Comment

by:Sadrul
ID: 12503744
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
0
 

Author Comment

by:Carl3003
ID: 12506928
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");
            }

      }
0
 
LVL 2

Expert Comment

by:shah1d1698
ID: 12512823
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);
          }
0
 
LVL 2

Accepted Solution

by:
shah1d1698 earned 400 total points
ID: 12512875
To reflect the changes in the file u can create a new file with the changed entries written in it. Then remove the old file and rename the new file with the name of the old file.

Using your code..

FILE *fp=fopen("passwd","r");//for reading the old file
FILE *fp1=fopen("passwd1","w");// for writing the new file
char str[10];
sprintf(str,"%d",getuid());
char *startOfSubStr;
char *line;
while(fgets(line, 512, fp)!=NULL)
     {
          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);
             fputs(line, fp1); //writing to the new file
          }
          else
          {
             fputs(line, fp1); // writing to the new file
          }
     }//end of while

fclose(fp);
fclose(fp1);
remove("passwd"); // removing file named passwd.
rename("passwd1", "passwd"); // renaming passwd1 to passwd.

//This method incurs excessive overhead...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question