Reading User Defined Files

This is a discussion on Reading User Defined Files within the C++ Programming forums, part of the General Programming Boards category; the function I use is something (I'm doing this from memory, so it's likely there are mistakes!), like this: Code: ...

  1. #16
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    the function I use is something (I'm doing this from memory, so it's likely there are mistakes!), like this:

    Code:
    bool GetFileInfo ( string FileName, string &FileInfo )
    {
    
        ifstream in ( FileName.c_str() );
    
        if ( in )
        {
            string Temp;
            FileInfo.clear();
    
            while ( getline( in, Temp ) )
            {
                FileInfo += Temp;
                FileInfo += "\n";
            }
    
            in.close();
        }
        else
        {
            in.close();
            return false;
        }
    
        in.close();
    
        return true;
    }
    it's VERY handy if you have a program which uses a LOT of file IO. I did a Book/DVD/Music Catloguer the other day, and I think I used that at least 12 times. I have it as a bool so that I can have it in an if statement in my main program, like so:

    Code:
    string Destination;
    if ( GetFileInfo( "MyFile.txt", Destination ) )
    {
        cout<< "the file is as follows: " << endl
            << Destination << '\n';
    }
    else cout<< "Error in opening the file";
    instead of having to do the function millions of times.

  2. #17
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Another alternative is take much of Twomers' code and turn it into a handy-dandy logging class. That way one class has control over the entire file handling. Not to mention with multiple threads it would be possible to make it thread safe-er. Start it up once during the program runtime, shut it down once, and you are all set. You can flush the output stream for "on-the-fly" updating.

    Also, on destruction, you could handle fileclosing so you don't constantly hit the disk opening and closing all the time.

  3. #18
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    I dont know whether this will be of any use however i made a file editor not so long ago here is the source code for it though there is no comments on it sorry bout that:

    Also there are a few bugs in it that i keep meaning to get rid of but never really done it however it works!
    Attached Files Attached Files
    Last edited by L_U_K_E; 06-30-2006 at 01:58 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User defined functions
    By alexpos in forum C Programming
    Replies: 2
    Last Post: 10-23-2005, 03:53 PM
  2. Issues reading text files
    By ozzy34 in forum C++ Programming
    Replies: 5
    Last Post: 06-01-2004, 09:15 AM
  3. Replies: 4
    Last Post: 04-21-2004, 05:18 PM
  4. question about reading files in
    By smd in forum C++ Programming
    Replies: 11
    Last Post: 08-25-2003, 08:40 PM
  5. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 12:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21