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 cboard_member's Avatar
    Join Date
    Jul 2005
    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,810
    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 cboard_member's Avatar
    Join Date
    Jul 2005
    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 offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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 05:18 AM.

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    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,806
    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();
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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, 05: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, 08: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, 09:03 AM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM

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