'grep string file > newfile'
i need an idea how to do this in C.
I know how to open, create files, compare strings .. etc ...etc ... but i'm newbie .. and making the whole thing out .. it's still hard for me
'grep string file > newfile'
i need an idea how to do this in C.
I know how to open, create files, compare strings .. etc ...etc ... but i'm newbie .. and making the whole thing out .. it's still hard for me
It seems like you already know most of the things needed to accomplish this. Here's one way:
Try to code this out and we'll help you out if you run into problems.Code:open new file for output open input file while not end of file get line compare search string with every string in line - if match, print line, increment count, etc...(whatever you want to do depending on the option set) end while print count to file, etc... close all files
Try not.
Do or do not.
There is no try.
- Master Yoda
Instead of strcmp, try 'man strstr'.
This seems to work ... thanks
Code:#include <string.h> #include <stdio.h> main () { FILE *cita; FILE *pise; char b[100]; char string[] = "milan"; cita = fopen ("/tmp/cita", "r"); pise = fopen ("/tmp/pise", "w+"); while ((fgetc (cita) != EOF)) { fgets (b, 100, cita); if ((strstr (b, string)) != 0) { fputs (b, pise);} } fclose (cita); fclose (pise); return 0; }
Last edited by milan; 12-18-2002 at 02:20 AM.
>'grep string file > newfile'
For simple comparisons, strstr is a good choice. If you want similar functionality to grep then you've got a lot of work ahead of you, grep is very thorough. If you'd like you can download the source code of a grep implementation from http://www.gnu.org/directory/grep.html
-Prelude
My best code is written with the delete key.