Thread: Ofstream, Ifstream, Searching files for strings?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    #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

  2. #2
    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!

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