0

I have a file with path names to files:

/my/path1
/my/path11
/my/path12
/my/path13

The file structure is that it has individual paths in each line. All I want to do is search for the existence of a string /my/path1 or anyother in the above file many times

I could think of 2 methods.

  1. every time get file contents line by line and then search the string. Advantage is that the file can be of anysize and I dont need to worry about buffer overflow.

  2. Load the contents into a buffer and search it using the buffer. But as I dont have control over the file size I should be cautious here.

What is the best approach? I am working in unix. Is there any in-build library commands in C that I can make use of for this purpose? Or how can I accomplish the same task using awk in C code.

footy
  • 5,803
  • 13
  • 48
  • 96
  • you have to use C and cannot just use a system command with the execv family of `sed`/`awk` ? otherwise exec: `sed -n '/pathtomatch/p' pathfile.txt` – Bort Feb 09 '12 at 10:21

2 Answers2

1

If you use stdio it will do the buffering for you. You can change its operation by using the function setvbuf to buffer more than a single line. getline can by used to check line by line.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • `getline` is a part of C++, isn't it? – mikithskegg Feb 09 '12 at 10:22
  • It is a part of stdio (GNU extension) - C. See http://www.gnu.org/software/libc/manual/html_node/Line-Input.html Or you can use fgets – Ed Heal Feb 09 '12 at 10:29
  • O, thanks. I did not know this function. I've read `man getline` and found out it quite useful. Especially its ability to reallocate dynamic buffer if it is not large enough to get all the string. – mikithskegg Feb 09 '12 at 10:35
0

I think loading all the file in memory is not a good idea. Using fgets and strcmp is the best way, I guess.

mikithskegg
  • 806
  • 6
  • 10