Advertisement

Simple File I/O Question

Started by May 23, 2002 10:45 PM
7 comments, last by Feblex 22 years, 3 months ago
I''m trying to load the lines of a ASCII file into an array so each entry in the array (say question[1]) corresponds to a line in the file (in the previous example, to line 1). What is the easiest and cleanest way to do this?
Assuming C++


    #include <cstdlib> // for exit()#include <vector>  // for std::vector#include <string>  // for std::string#include <fstream> // for std::ifstream#include <iostream>  // for std::cerr and std::endlusing namespace std;  // to remove the std:: prefix :)int main(){  vector<string> questions;  ifstream ifs( "questions.txt" );  if( !ifs )  {    cerr << "Could not open question file" << endl;    exit( 1 );  }  while( !ifs.eof() )  {    string line;    getline( ifs, line ); // read a line    questions.push_back( line );  }  cout << "There are " << questions.size() << " questions" << endl;   cout << questions[0] << endl; // print the first question  return 0;}    


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on May 23, 2002 12:12:28 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
I''ve been using C++ for a while now and, unless I''m missed something, I''m not aware of checking that a file has been successfully opened by simply using ! on that input stream object. Shouldn''t it be !ifs.is_open()?

/*=========*/
/* Chem0sh */
/*=========*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
Nope.

When you try to open the file stream - it will set a pointer to the file, otherwise it returns a null. The if statement using the ''!'' returns a true if the pointer is null - thereby showing that the file open failed.

Make sense?


Landsknecht
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.
Yeah, perfectly. I had just never heard of being able to test like that. Hehe, live and learn I guess.
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
Thanks for the quick reply! The code looks great.
Advertisement
uh, Landsknecht, there''re no pointers involved..."ifs" is a instantiation of a class...unless the ! operator is overloaded, Chem0sh is right, and it will always think the file was opened successfully.

(how often have you seen a ctor return a null anyways? )
operator!() is overloaded for fstreams.

it basically returns "!ifs.good()"

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Thank you, Fruny.

The thing is the ctor does not return ANYTHING. Ctors can''t return anything. What is happening is that the ifs is set to a null unless it gets instantiated properly, i.e. if the file is opened successfully. In which case the ctor sets the stream to the file.

When dealing with files, there are ALWAYS pointers involved. Using the streams, you just don''t seem them if you don''t want to.


Landsknecht
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.

This topic is closed to new replies.

Advertisement