Finding a specific line in a file?

I was wondering if anyone new of some text to read or if they know of the syntax itself on how to search a file for a specific string. After I find that string I then need to output the contents of that line so the user can see and then allow them to change a quantity of that line. This is for a database program I am writing if you could not tell. A tutorial or syntax will help.
Last edited on
Well, one way I think you could solve it is using the getline function.

1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream inFile;
inFile.open('fileName')

while (inFile.good())
{
getline(inFile, str1)
line++

if (strcmp(str1, str2))
{
break;
}
}


I believe this will get you to the line you want, just enter your line you want to find in str2. Then you will have the line number it appears on in the file, so that way you can modify it.

If you aren't searching for a whole line in the file, you can change the string comparison part of the program (strcmp) so that you can see if it is in the line. Then you can use the line number and start the modification of where it starts to where it ends.

If you are looking for a string across lines in a file, I don't know how to help, sorry :(
This is kind of what I was thinking because the first word in each line is going to be the property for each entry. So I only need to search those. I am just no sure once it is found how to read the rest of the line and then change the quantity that is in that line.

so if the entry is

cardID companyID cardInfo Quantity

I will search using cardID and then when it is found I need to either add or subtract from the quantity.
Hmmm, could well what you could do (even though it is very complicated) is:

Find the line, and open up an output stream at the same time, and have 2 files. Once you find the line you are looking for (stored in str1), modify it. Once you modify it, you can write it into your second file. Then at the end, just write over the entire first file with the seoncd.

So first file contains:

aaaa
bbbb
cccc
dddd
....
zzzz

then make your program write a second file (and after modifying the nth line:)

aaaa
bbbb
....
n888
oooo
....
zzzz

then after that is complete, make your program overwrite the first file.

It is complicated, and I am sure there is a better way to do it, but that is all I can think of right off the top of my head.
I would try loading the file into a vector of strings (or data structures, if you want to parse the file) for the lines, and then modifying that. Then once you are done, you just write the lines/structures to the file.
That is what I thought about but it seems like there should be a better way to do it than that though. Because once it is read into the vectors how do I search the vectors?
while, string one is stored in the first vector slot (Array[0]) Compare Array 0 to your string you want to find (strcmp or something similar). Then if that doesn't work, go to Array[1]. I would recommend a while loop, and while bool found = false && count < size, run the loop.
You would iterate through the vector and check the elements. If the data is ordered by some key, you should consider using a std::map instead.
I am fairly new to c++ what is the differences between list, vectors and maps
A list is just a bunch of items in a row linked together by pointers to the next item in them list. I don't think std::lists are sorted.

Vectors are wrappers for an array of a particular item that grows as required so you don't have to manage the size yourself.

The don't recall the internal implementation maps usually have, but basically they store data with a "key" and the "data" itself, and are designed to have efficient lookup times for search for elements by the "key."
so it seems a map would be a good way to go. Has anyone ever used a map and is this assumption correct?
I would create objects, simply in a normal array, because I like to be lazy. and sort / search / modify array.

eze peezy
Last edited on
the only problem with using an array is you have to define the size of the array at the beginning right? Isnt that the bonus of using the vector is no size defining
you can have a running count, or as you say use vectors. (only reason I don't is we aren't taught vectors or templates to be backward compatitble).
Topic archived. No new replies allowed.