Moez Hirani
Moez Hirani

Reputation: 141

using fstream object to store information from a file into variables

I have the following block of code that i am using to read a text file of the following format:

firstname lastname id mark
firstname lastname id mark

Following is the block of code.

void DBManager::ReadFile(void){
fstream myfile; /*fstream object that will be used for file input and output operations*/
char* fn;       /*pointer to the storage which will hold firstname*/
char* ln;       /*pointer to the storage which will hold lastname*/
int id;         /*integer var to hold the id*/
float mark;     /*float var to hold the mark*/

/*read in the filename*/
g_FileName = new char[1024];                /*allocate memory on the heap to store filename*/
cout << "Please enter the filename:";
    cin >> g_FileName;

/*open file*/
myfile.open(g_FileName, ios::in | ios::out);

if(myfile.is_open()){   /*check if the file opening is successful*/
    cout << "File reading successful !\n";

    /*read information from the file into temporary variables before passing them onto the heap*/
    while (!myfile.eof()) {

        fn=(char*) new char[1024];
        ln=(char*) new char[1024];
        myfile >> fn >> ln >> id >> mark;
        cout << fn << " " << ln << " " << id << " " << mark << " " << endl;

    }
    myfile.close();
}
else{                   /*else print error and return*/
    perror("");
    return;
}

}

The above block of code works ! :) But I am surprised as to how myfile knows it is supposed to hold one line at a time and how its being smart enough about setting the four variables.

I am new to C++ , and hence this might be covered in some sort of documentation. But i would be happy to have some insight from you'll or a link to somewhere i can understand fstream objects better.

Upvotes: 0

Views: 2957

Answers (2)

Ryan
Ryan

Reputation: 2404

In C++, std::fstream is a type of stream which works specifically for files. When reading from a file, the interface for std::fstream is almost identical to std::cin. Input streams are programmed to read the next word or number when asked with the >> operator. They know where words and numbers are because they are separated by white space. In the default locale, spaces, tabs and newlines are considered to be white space. You can change the locale to include other characters, like commas, and have those be skipped while reading from a file. Basically, when reading with input streams, newlines and spaces are treated the same.

Some nice explanation for learning about streams is here: http://www.cprogramming.com/tutorial/c++-iostreams.html

Upvotes: 2

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 154025

I'm not sure what the question is. However, the code has several problems:

  1. You should always check input after having tried to read.
  2. Testing for eof() to determine if there is more to read doesn't work.
  3. You have a memory leak, allocating memory in every iterator.
  4. Reading without a constraint into a char array is unsafe, i.e., it is prone to buffer overrides (one of the major attack vectors).

You want to use a loop looking something like this:

std::string fn, ln;
while (myfile >> fn >> ln >> id >> mark) {
     ...
}

Upvotes: 2

Related Questions