A_Gupta
A_Gupta

Reputation: 87

How can I scan a file for a word and then print the line containing that word in another file in C programming?

How can I scan a file for a word and then print the line containing that word in another file in C programming?

META_FILE = fopen("vs2008.map","r");
fp=fopen("META_DATA_INFO","w");


while(fgets(line, sizeof(line), META_FILE)) 
{
    if (strstr(line,"0004:") != NULL) 
    {
        puts(line,fp); // this line print on screen i want a function to write in fp file   
        }
}

Upvotes: 0

Views: 365

Answers (1)

Change puts(line, fp); to one of:

fputs(line, fp); // note: this does not write a newline

or:

fprintf(fp, "%s\n", line); // this writes a newline

Upvotes: 1

Related Questions