Search a input string in a text file 
Author Message
 Search a input string in a text file


Quote:
> Dear everyone,
>     Can anyone tell me how to search a input string in a text file, then
> input other string after this string in the text file, then save it?
> Thanks

If, by that, you mean insert a new text line in an existing file after a
found line, then the easiest/safest way is to copy the file while you are
reading/scanning it to a temporary file, then kill the original and rename
the temporary to the original name.

// pseudo code
hInFile = open original file;
hOutFile = open temp file;
while (bytesleft in original) {
    readline(hInFile, szBuf);
    writeline(hOutFile, szBuf);
    if !strcmp(szBuf, "look for this\n")
        writeline(hOutFile, "extra line to appear after\n");

Quote:
}

remove(original file);
rename(temp file, original file);

-LZ



Tue, 03 Sep 2002 03:00:00 GMT  
 Search a input string in a text file

Quote:

> Dear everyone,
> Can anyone tell me how to search a input string in a text file, then
> input other string after this string in the text file, then save it?
> Thanks

You're not clear. You mean you have a file containing a line like

name: Fred

and you want to search for the string "name:", input "Fred" and save "Fred"
to another file ?

If you know that the string you are scanning for will not be split by
newlines (and that the file contains reasonable length lines) use fgets() to
read the file line by line

do
{
  fgets(buff, sizeof(buff), fp);
  ptr = strstr(buff, "name:");
  if(ptr)
  {
    ptr += stren("name:");
    sscanf(ptr, "%32s", name); /* always pass buffer size to sscanf() */
   /* now write name to another file, outfp */
    fwrite( name, strlen(name), 1, outfp);
  }

Quote:
} while( etc )

Sent via Deja.com http://www.deja.com/
Before you buy.


Tue, 03 Sep 2002 03:00:00 GMT  
 Search a input string in a text file
Dear everyone,
    Can anyone tell me how to search a input string in a text file, then
input other string after this string in the text file, then save it?
Thanks


Wed, 04 Sep 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Open a text file and search the file for a STRING of characters

2. Problem opening file and searching for a text string

3. Searching a Text File for a Given String

4. Searching for a text string in a file.

5. Searching a text file for strings

6. Searching for text string in binary file?

7. searching for high performance C-libraries for input-output and file-handling

8. Reading a pattern of string from text file and writing it to a new file

9. searching for text with file I/O

10. Searching/Modifying a text file

11. search for text in files

12. Help--need really fast search to compare two HUGE files of text records

 

 
Powered by phpBB® Forum Software