Thread: Reading a file

  1. #1
    Registered User dan20n's Avatar
    Join Date
    Jan 2005
    Posts
    24

    Reading a file

    I have a program that has an array of structs. I want to read a file which can look something like this:
    Code:
    one
    two
    three four
    five
    Now i want to read these files into the array of structs but after searching and looking in my book i cant find an example of how this can be done. Below is the code slightly modified.

    Code:
    void ReadFile() {
         ofstream PetFile;
         PetFile.open ("test.txt");
         if ( !PetFile.good() ) {
            cerr << "An error occured while opening pet data base." << endl;
            exit (1);
         }
         
         int PetIndex = 0;
         while ( !PetFile.eof() ) {
               //getline(PetFile, PetID[PetIndex].Status);
               cout << PetID[PetIndex].Status;
               //system("PAUSE");
               
               ++NumRecords;
               cout << NumRecords;
               ++Index;
               cout << Index;
               //system("PAUSE");
         }
    }
    i have commented a line out so it will compile, what i have here is my best attempt at getting this to work, i only wish to read one line at a time, then assign it to a variable in the array, im also not sure if i can do this directly or i may need a temporary variable. cheers.

    I get this error msg when the getline function is not commented.
    47 no matching function for call to `getline(std:fstream&, eStatus&)'
    Meg: Everybody! Guess what I am?
    Stewie: Hm, the end result of a drunken back-seat grope-fest and a broken prophylactic?

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    There are a number of mistakes in your code.

    1. If you want to read a file then why use ofstream? You should use ifstream (i is for input, you want something to input to your program). When writing you should use output file stream.
    2. You say array of structs and you're using text files. So I can conclude that you want to write string to a file and then read it back and add to your structure. so variable Status is char*
    first
    second
    third
    ...
    it is sufficient to use only char buf[BUFFER_SIZE]
    I'll show you this on a simple example and you need to figure out how to use this to solve your problem.
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    void write_file()
    {
    	ofstream out("data.txt");
    	out<<"first\n";
    	out<<"second\n";
    	out<<"third\n";
    	out.close();
    }
    
    void read_file(char* buffer,int len)
    {
    	ifstream in("data.txt");
    	while(!in.eof())
    	{
    		in.getline(buffer, len);
    		cout<< buffer<<endl;
    		// now you can do whatever you like with buffer
    	}
    }
    const int BUFFER_SIZE = 1024; 
    int main()
    {
    	char buffer[BUFFER_SIZE];
    	write_file();
    	read_file(buffer,sizeof buffer);
    }
    If you want to write whole structure to a file then you'll need to use binary files and functions read() and write()

    And prototype of getline is:

    Code:
    istream & getline(char *, int, char = '\n');
    So you see now why you're getting a compiler error

  3. #3
    Registered User dan20n's Avatar
    Join Date
    Jan 2005
    Posts
    24
    Thanks for your detailed reply, everything is running smoooth
    Meg: Everybody! Guess what I am?
    Stewie: Hm, the end result of a drunken back-seat grope-fest and a broken prophylactic?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Woop?

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by prog-bman
    I didn't realize that what is said for feof() in C can be applied to C++ method eof().
    My logic was:

    When a cin operation reaches the end of a file, it sets the eofbit
    and eof() method returns true if eofbit is set.
    Can you explain in more detail why it si bad to use eof() also?

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Woop?

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Thanks
    so correct solution is:
    Code:
    while(in.getline(buffer, len))
    	{
    		cout<< buffer<<endl;
    		// now you can do whatever you like
    	}

  8. #8
    Registered User dan20n's Avatar
    Join Date
    Jan 2005
    Posts
    24
    Makes a little more sence, ill have do to some more readying, thanks for clarifying that for me. When you posted that FAQ i was like, huh? Thanks again
    Meg: Everybody! Guess what I am?
    Stewie: Hm, the end result of a drunken back-seat grope-fest and a broken prophylactic?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM