reading entire file into a class

This is a discussion on reading entire file into a class within the C++ Programming forums, part of the General Programming Boards category; I'm needing to make a class to read the entire contents of a file into a class, and then there's ...

  1. #1
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    reading entire file into a class

    I'm needing to make a class to read the entire contents of a file into a class, and then there's a function in the class that returns a char * so that it can be used by cout.

    right now I have:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    class FileString
    {
    private:
        char buff[2000];
    
    public:
      // If File errors exist place them into the string buffer 
      // which starts with **ERR: 
      // For example, **ERR: Couldn't open file
       FileString (string filename)
       {
           ifstream infile(filename.c_str());
           int fileSize = 0;
           if (!infile)
           {
               strcpy(buff, "**ERR: Could not open file!");
           }
           else
           {
               infile.seekg(0, ios::end);
               fileSize = infile.tellg();
               cout << fileSize << endl;
               for (int i = 0; i < fileSize; i++)
               {
                   buff[i] = infile.get();
               }
           }
       }
       ~FileString()
       {
       }
    
       // A call to value returns the contents of the file. 
       const char * value()
       {
           return buff;
       }
    };
    
    int main()
    {
        FileString temp("D:\\School\\Semester 3\\CPS271\\File IO Homework last assignment\\main.cpp");
        cout << temp.value();
    
        cin.get();
        return 0;
    }
    but I don't know what I'm doing wrong... any tips would be helpful
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Code:
    infile.seekg(0, ios::end);
    fileSize = infile.tellg();
    cout << fileSize << endl;
    for (int i = 0; i < fileSize; i++)
    {
        buff[i ] = infile.get();
    }
    Where do you think the stream's "get" pointer is when you start to call the get member function. You just set it to be the end of the file stream and then start reading from that point. You need to reset it back to the beginning before you start calling get.
    "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

  3. #3
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    geez, i don't know why i didn't see that. thanks, that helps out
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 11:21 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

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