Let's say i have a txt file:
Date: 11/11/11
Device: Boxster
Status: Good
I am trying to make my code search for a word (Say Device:), and display the info after that word (Boxster). So far i have the code working to only search for one word. How can i fix the code so that it can search 2 or 3 words, and display the info after them?
It would be even more helpful if i can display the info in the following format:
Boxster, 11/11/11, good.
Here is my code, thanks in advance!
#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+7);
if((Data = strstr(rc, "Device:"))!=NULL)
printf(Device+6);
}
fclose ( fs ) ;
fclose ( ft ) ;
return 0;
}