Reading whole file w/ ifstream

This is a discussion on Reading whole file w/ ifstream within the C++ Programming forums, part of the General Programming Boards category; I'm having a problem using ifstream; I'm more used to using FILE*'s. I want to read an entire file using ...

  1. #1
    Supermassive black hole ahluka's Avatar
    Join Date
    Jul 2005
    Location
    South Wales, UK
    Posts
    1,709

    Reading whole file w/ ifstream

    I'm having a problem using ifstream; I'm more used to using FILE*'s.

    I want to read an entire file using an ifstream and store it in a buffer, without loosing formatting. Here's what I have so far:

    Code:
    // Read (): Reads the specified data file
    // and returns it's contents
    std::string Dataf::Read (std::string fileName)
    {
    	std::ifstream   fd (fileName.c_str());
    	std::string	 buffer;
    	std::string	 errorBuf ("Error opening data file [");
    
    	if (!fd.is_open()) {
    		 errorBuf += fileName + "], please check it exists.\n";
    		 KillGame (errorBuf);
    	}
    
    	fd >> buffer;
    
    	return buffer;
    }
    That only reads one word ('Adventures') from the data file. I did try a little 'double buffering' techinique but I lost all formatting:

    Code:
    // Read (): Reads the specified data file
    // and returns it's contents
    std::string Dataf::Read (std::string fileName)
    {
            std::ifstream   fd (fileName.c_str());
            std::string     buffer, buf;
            std::string     errorBuf ("Error opening data file [");
    
            if (!fd.is_open()) {
                    errorBuf += fileName + "], please check it exists.\n";
                    KillGame (errorBuf);
            }
    
            while (!fd.eof()) {
                    fd >> buf;
                    buffer += buf + " ";
            }
    
            return buffer;
    }
    Any help appreciated as usual
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,800
    I am not sure if this is the best method but this works
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    std::string readFile(const std::string &toRead)
    {
        std::ifstream fd(toRead.c_str());
        std::string buffer;
        char ch;
        while(fd.get(ch))
        {
            buffer.push_back(ch);
        }
        return buffer;
    }
    
    int main()
    {
        std::string getBuffer = readFile("test.txt");
        std::cout<<getBuffer<<std::endl;
        std::cin.get();
        return 0;
    }
    Input:
    Code:
    Hello my name
    is brian how are
    you today
    Output:
    Code:
    Hello my name
    is brian how are
    you today
    Woop?

  3. #3
    Supermassive black hole ahluka's Avatar
    Join Date
    Jul 2005
    Location
    South Wales, UK
    Posts
    1,709
    Thanks, I didn't realise you could push_back() a string
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    ZuK
    ZuK is online now
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,881
    This would work as well
    Code:
    #include <iostream>
    #include <iomanip>
    #include <iterator>
    #include <fstream>
    
    using namespace std;
    int main() {   
        string file("a.cpp");
        ifstream infile(file.c_str());
        if(!infile) {
            return 0;
        }
        infile >> noskipws;
        string code_str;
        code_str.assign( istream_iterator<char>(infile),istream_iterator<char>() );
        cout << code_str;
        return 1;
    }
    Kurt
    Last edited by ZuK; 09-03-2005 at 04:18 AM.

  5. #5
    Supermassive black hole ahluka's Avatar
    Join Date
    Jul 2005
    Location
    South Wales, UK
    Posts
    1,709
    Thanks
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,675
    Include the <sstream> header and alter the following 3 lines of code:

    Code:
    // Read (): Reads the specified data file
    // and returns it's contents
    std::string Dataf::Read (std::string fileName)
    {
        std::ifstream     fd (fileName.c_str());
        std::stringstream buffer;
        std::string       errorBuf ("Error opening data file [");
    
        if (!fd.is_open()) {
            errorBuf += fileName + "], please check it exists.\n";
            KillGame (errorBuf);
        }
    
        buffer << fd.rdbuf();
    
        return buffer.str();
    }
    I used to be an adventurer like you... then I took an arrow to the knee.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Resetting a ifstream object after reading the whole file
    By Zeeshan in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2008, 08:03 AM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM

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