Thread: File Input from structue???

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    South Africa
    Posts
    20
    This will work. I don't know how to do it using c++ streams though. You have to open the file in binary mode and read a chunk of data the size of your structure. If you pass fread() a pointer to your structure it will populate your structure (assuming the file has the correct format).

    Code:
    int main(int argc, char *argv[])
    {
        FILE * sFile;
        struct data student[10];
        
        if (NULL == (sFile = fopen("student.data", "rb")))
        {
            cout << "Could not open file" << endl;
            system("PAUSE");
            return -1;
        }
        cout << "name\tsex\tage\tweight:" << endl;
        for (int i=0; i<10; i++)
        {
            fread(&student[i], sizeof(data), 1, sFile);
            cout << student[i].name << "\t";
            student[i].MF?(cout << "female\t"):(cout << "male\t");     
            cout << student[i].age << "\t" << student[i].weight << endl;
        }
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    kyle

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A structure can be read by writing your own operator>> and operator<< functions that takes that structure as an argument (and a istream/ostream as well, of course).

    You can use >> and << inside the structure reading function, if that's what you want to do.

    Note that this will read/write data in text form (assuming you use the >>/<< operators), not in binary form like the fread/fwrite functions do - there are good and bad things about that.


    If you want to use binary form, use the fstream::read and fstream::write
    http://www.cplusplus.com/reference/i...ream/read.html
    http://www.cplusplus.com/reference/i...eam/write.html

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Location
    South Africa
    Posts
    20
    Thanks for the info. So heres the revised example which contains both the c method and the c++ file stream method:
    Code:
    #define USE_STREAMS
    
    enum sex {male, female};
    struct data
    {
    	char name[30];
    	sex MF;
    	int age;
    	double weight;
    };
    
    using namespace std;
    int main(int argc, char *argv[])
    {
        int length;
        struct data student[10];
        
        #ifdef USE_STREAMS
        ifstream isFile;
        isFile.open ("student.data", ios::binary );
    
        isFile.seekg (0, ios::beg);
        // read data 
        for (int i=0; i<10; i++)
        {            
            isFile.read ((char *)(&student[i]),sizeof(data));
        }
        isFile.close();
        cout << "name\tsex\tage\tweight:" << endl;
        for (int i=0; i<10; i++)
        {
            cout << student[i].name << "\t";
            student[i].MF?(cout << "female\t"):(cout << "male\t");     
            cout << student[i].age << "\t" << student[i].weight << endl;
        }
        #else
        FILE * sFile;
         
        if (NULL == (sFile = fopen("student.data", "rb")))
        {
            cout << "Could not open file" << endl;
            system("PAUSE");
            return -1;
        }
        cout << "name\tsex\tage\tweight:" << endl;
        for (int i=0; i<10; i++)
        {
            fread(&student[i], sizeof(data), 1, sFile);
            cout << student[i].name << "\t";
            student[i].MF?(cout << "female\t"):(cout << "male\t");     
            cout << student[i].age << "\t" << student[i].weight << endl;
        }
        #endif //USE_STREAMS
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    kyle

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  4. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM