Thread: Ofstream, Ifstream, Searching files for strings?

  1. #1
    #junkie
    Join Date
    Oct 2004
    Posts
    240

    Ofstream, Ifstream, Searching files for strings?

    Ok, just to keep topic order neat, i am posting a new post on this to recap a bit also.

    I am looking to search files for strings, ie
    Code:
    item1=value
    item2=value
    item3=value
    Now to my lack of understanding, i only know of
    Code:
    object.ignore(100,'=');
    To move the curser up to an items value. But i wish to search at random points throughout the file to find the item strings.

    Is there a way to do something like
    Code:
    object.ignore(100,"item1=");
    object.getline(String,20,'\n');
    ?
    Notice i am finding the item1 item, and returning its value.

    And to my knowlage, once you do that, the curser is now at the end of that line, how would i move the curser to the beginning of the file to find previous items?
    ie
    Code:
    object.ignore(100,"item5=");
    object.getline(String,20,'\n');
    object.ignore(100,"item1=");
    object.getline(String,20,'\n');
    And lastly, with getline(), do you have to use a character array? Is it possible to use the string class? Or would i just use the character array and convert it to a string. Thanks!
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  2. #2
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    What size files are you dealing with?

    If you're desperate and the files are small enough, you can always load the entire file into an std::string, and then use s.find() to find the strings you're looking for.

    http://www.msoe.edu/eecs/cese/resources/stl/string.htm

    size_type find (const string& str, size_type pos=0) const;
    size_type find (char ch, size_type pos=0) const;
    size_type rfind (const string& str, size_type pos=npos) const;
    size_type rfind (char ch, size_type pos=npos) const;

    The find function searches for the first occurrence of the substring str (or the character ch) in the current string, starting at position pos. If found, return the position of the first character in the matching substring. If not found, return the value string::npos. The member function rfind does the same thing, but returns the position of the last occurrence of the specified string or character, searching backwards from pos.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'll need to seek back to the beginning. Something like:
    Code:
    inputfile.seekg(0, ios::beg);
    However, the way you're doing this leaves a lot to be desired.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    Is there a way to do something like
    object.ignore(100,"item1=");
    Nope: the second parameter of ignore() is not a string.

    object.getline(String,20,'\n');
    ...
    ...
    And to my knowlage, once you do that, the curser is now at the end of that line,And to my knowlage, once you do that, the curser is now at the end of that line,
    It might be in the middle of the line. The getline() statement (if it were in the proper format) would read in 20 chars or if it came upon a newline character before reading in 20 chars it would stop. So, getline() could read in 20 chars without encountering a newline, and there might still be 100 more chars on the current line.

    how would i move the curser to the beginning of the file to find previous items?
    Like this:
    Code:
    ifstream inFile("fullPathNameOfYourFile");
    ...
    ...
    inFile.seekg(0);
    And lastly, with getline(), do you have to use a character array? Is it possible to use the string class?
    No. Yes:
    Code:
    #include <string>
    using namespace std;
    ...
    ...
    string str;
    getline(inFile, str); //reads a whole line of text
    
    inFile.seekg(0);
    getline(inFile, str, '#'); //reads input until the end of the file or a '#' is found
    If you read the whole file into a string variable, then you can use string functions, like find(), to search around the string, which I believe is more efficient than searching around a file.
    Quote Originally Posted by quzah
    However, the way you're doing this leaves a lot to be desired.
    That's a bit cryptic don't you think? How about some guidance on a better method?
    Last edited by 7stud; 04-03-2005 at 09:59 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So I'm supposed to teach people basic logic skills now? That's right, most people can't think for themselves. I keep forgetting that, as I don't seem to have that problem.

    Read a line from a file.
    Parse the line.
    Do whatever.
    Lather.
    Rinse.
    Repeat.

    Or is that too cryptic too?

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    So I'm supposed to teach people basic logic skills now?
    Not at all, but then why answer a post from someone who is obviously struggling?

    That's right, most people can't think for themselves. I keep forgetting that, as I don't seem to have that problem.
    No matter how smart you are, there is likely to be someone else who will think the same thing about you.
    Last edited by 7stud; 04-04-2005 at 01:02 AM.

  7. #7
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    Quote Originally Posted by quzah
    So I'm supposed to teach people basic logic skills now? That's right, most people can't think for themselves. I keep forgetting that, as I don't seem to have that problem.

    Read a line from a file.
    Parse the line.
    Do whatever.
    Lather.
    Rinse.
    Repeat.

    Or is that too cryptic too?

    Quzah.

    Well couple of comments on this,
    A: Sorry if my ways are unconventional, its been a while since i have been immersed in coding (i'v been teach myself 3d modeling lol), that and i have different habits from script kiddie languages that gave me many prebuilt packages.

    I became very in depth at using the "packages" to their full potential and i'm still getting the grasps of breaking bad habits and thinking outside of my box.


    B: this goes back to my script kiddie packaging, but i am used to dealing with structures as a whole. And due to how i came about learning C++ (via a c++/opp learning book, not specifically the tools and commands associated with ansi c++), i tend to (consciously or unconsciously) stick within my happy place.


    C: In response to Lithorien,
    Thats a good idea, the file sizes are plenty small enough i am sure. (Were talking a matter of under 50 lines of 30ish characters... and thats most likely being generous)







    Quote Originally Posted by quzah
    Read a line from a file.
    Parse the line.
    Do whatever.
    Lather.
    Rinse.
    Repeat.
    And lastly, if quzah wouldn’t mind a response with the flaming kept to a minimum (not that you already havent i am sure lol)

    Seeing as i may be needing any line/item at any time, and that i would like to keep the file system dynamic enough that i don’t have to simply loop through my file and put each value into a predefined variable. How about would that work? I am in no means trying to start some flameish ..........fest, i am being serious. (god forbid i try to learn from people who know more than i)

    I could easily get away with, and most likely will, put the file into a string, then just pull my values out in a parsing manner on the fly.
    I am just curious if there is something i am missing. As always, any replies (pertaining to the questions at hand) are always welcome and much appreciated.

    Thanks for everyone’s posts
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  8. #8
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Zeusbwr
    Thats a good idea, the file sizes are plenty small enough i am sure. (Were talking a matter of under 50 lines of 30ish characters... and thats most likely being generous)
    This seems like the best way, then. You'd only have one read/write operation through the course of the program, unless you needed to check for updates of the file. In that case, you can write a function that returns the string to main() (or wherever) and work with it that way.

    In either case, the string seems easiest for the size of your file.


    Seeing as i may be needing any line/item at any time, and that i would like to keep the file system dynamic enough that i don’t have to simply loop through my file and put each value into a predefined variable. How about would that work?
    You would run through a while() loop, and use std::getline to pull out one line at a time from the file. Then use the s.find() method I was talking about before on just that line, or, conversely, do string comparison in another for() loop that creates a new char[X] to pull X letters from the string and do a strcmp() at the end.

    That may have been confusing, so here's some sample code..

    Code:
    #include <iostream>
    
    int main()
    {
    	while (!file.eof()) // This is horrible. See the cprogramming FAQ on better methods! -Lith
    	{
    		std::getline(cin, buffer, '\n');
    		
    		char *c = new char[SIZE_OF_WORD_YOU_WANT];
    		
    		for (int x = 0; x < SIZE_OF_WORD_YOU_WANT; x++)
    		{
    			c[x] = buffer[x];
    		}
    	}
    	
    	std::cin.get();
    	return(0);
    }
    Your code will, of course, use what you need.

    Hope this helps!

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You would run through a while() loop, and use std::getline to pull out one line at a time from the file.
    I don't see the point in pulling one line at a time from the file, since the op is going to be doing multiple searches. I'm not sure, but I think it would be more efficient to read in the whole file in one shot, rather than repeatedly reading from the file. I seem to recall reading some posts on this forum that said file reading was a relatively expensive operation.

    Once the op has the whole file read into a string variable, then the op can use all the various string functions to search around for whatever they want.

  10. #10
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by 7stud
    I don't see the point in pulling one line at a time from the file, since the op is going to be doing multiple searches. I'm not sure, but I think it would be more efficient to read in the whole file in one shot, rather than repeatedly reading from the file. I seem to recall reading some posts on this forum that said file reading was a relatively expensive operation.

    Once the op has the whole file read into a string variable, then the op can use all the various string functions to search around for whatever they want.
    Which is what I said up above.

    This was only if he wanted it to be dynamic, and could at one day be dealing with large files.

  11. #11
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    For now i am just going to use strings, it will be small enough that i shouldent have problems at all. And this project will not need big file sizes to store its updated data, though i'm sure i'll need to do something like that in the near future with other stuff.

    Thanks for all the replies!
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 08:34 AM
  2. searching files
    By Gil22 in forum C++ Programming
    Replies: 1
    Last Post: 03-03-2003, 06:10 PM
  3. searching for binery files
    By Granger9 in forum C Programming
    Replies: 2
    Last Post: 09-11-2002, 05:31 PM
  4. Searching txt files (another file i/o qn)
    By Catif in forum C++ Programming
    Replies: 9
    Last Post: 05-13-2002, 04:14 PM
  5. searching files in directory
    By lobo in forum Windows Programming
    Replies: 5
    Last Post: 10-05-2001, 03:00 AM