Search code examples
c++cprintfstring-searchfgetc

Searching for a word in a text using C, and display the info after that word


Say I have a text file like this:

User: John

Device: 12345

Date: 12/12/12

EDIT:

I have my code to successfully search for a word, and display the info after that word. However when I try to edit the code to search for 2 or 3 words and display the info after them instead of just 1 word, I cannot get it to work. I have tried adding codes into the same while loop, and creating a new while loop for the other word, but both doesn't work. There must be something I am doing wrong/not doing.

Please advice, thanks!

Here is my code:

 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 int main() {

char file[100];
char c[100];

printf ("Enter file name and directory:");
scanf ("%s",file);

    FILE * fs = fopen (file, "r") ;
    if ( fs == NULL )
    {
           puts ( "Cannot open source file" ) ;
           exit( 1 ) ;
    }

    FILE * ft = fopen ( "book5.txt", "w" ) ;
    if ( ft == NULL )
    {
           puts ( "Cannot open target file" ) ;
           exit( 1 ) ;
    }

while(!feof(fs)) {
   char *Data;
   char *Device;
   char const * rc = fgets(c, 99, fs);

   if(rc==NULL) { break; }

   if((Data = strstr(rc, "Date:"))!= NULL)
   printf(Data+5);

   if((Data = strstr(rc, "Device:"))!=NULL)
   printf(Device+6);
   }



    fclose ( fs ) ;
    fclose ( ft ) ;

return 0;

 }

Solution

  • Ok, hope I can clear it this time. Sorry if I get confusing sometimes but my english is not the best.

    I'll explain the implementation inside comments:

    #define BUFFSIZE 1024
    int main()....
    
    char buff[BUFFSIZE];
    char delims[] = " ";  /*Where your strtok will split the string*/
    char *result = NULL;
    char *device; /*To save your device - in your example: 12345*/
    char *date; /*To save the date*/
    int stop = 0;
    
    fp = fopen("yourFile", "r");
    
    while( fgets(buff, BUFFSIZE,fp) != NULL )  /*This returns null when the file is over*/
    {
     result = strtok( buff, delims );   /*You just need to do reference to buff here, after this, strtok uses delims to know where to do the next token*/
    
       while(result != NULL){   /*Strtok returns null when finishes reading the given string*/
          if(strcmp(result,"Device")==0){   /*strcmp returns 0 if the strings are equal*/
             result = strtok(NULL, delims); /*this one gets the 12345*/
             device = (char*)malloc((strlen(result)+1)*sizeof(char)); /*Alocate the right amount of memory for the variable device*/
             strcpy(device, result); /*Now, device is "12345"*/
          }
           /*Here you do the same but for the string 'Date'*/
           if(strcmp(result,"Date")==0){   /*strcmp returns 0 if the strings are equal*/
             result = strtok(NULL, delims); /*this one gets the 12345*/
             date = (char*)malloc((strlen(result)+1)*sizeof(char)); /*Alocate the right amount of memory for the variable device*/
             strcpy(date, result); /*Now, device is "12/12/12"*/
          }
          /*And you can repeat the if statement for every string you're looking for*/
          result = strtok(NULL,delims);  /*Get the next token*/
       }
    }
    
    /*No strtok necessary here */
    
    ...
    

    Hope this helps.