Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top