ifstream

Started by
5 comments, last by Fruny 18 years, 4 months ago
Hey I'm back, this time with a problem with ifstream(). If you don't know already, I'm using c++ on Windows XP. I don't understand exactly how ifstream works. I tried doing this to open a file using the ifstream::open constructor... void open(const char* szName, int nMode = ios::in, int nProt = filebuf::openprot); ------------------------------------------------------------------------- #include <iostream> #include <fstream> using namespace std; ifstream infile; char FILENAME[255]; // Program code... cin >> FILENAME; infile.open(FILENAME,ios::in | ios::nocreate,filebuf::sh_compat); -------------------------------------------------------------------------- but I'm getting back errors that say `nocreate' is not a member of `std::ios' `sh_compat' is not a member of `std::filebuf' I checked the avaliable parameters for the ifstream::open constructor and they are all avaliable arguments to pass, but the compiler gives me errors. Can anyone tell me what is going on?
Advertisement
What documentation are you using, because as far as I know, ios::nocreate is pre-standard? open() will fail if the file does not exist.


jfl.
Documentation?
include

int main()
{
std::fstream outFile;
outFile.open("someStupidFile.csv", std::ios_base::out);

if(!outFile.is_open())
{
// Error
return -1;
}

outFile <
It removed some stuff.

#include

outFile <
Quote:Original post by GuitarPlayer0912
Documentation?


Where did you check for valid parameters for open()? By the way, you shouldn't have to worry about the third parameter of open(). Just omit it.

[edit] I'll just give you an example, relating to your previous thread:
#include <iostream>#include <fstream>#include <string>int main() {  using namespace std;  string filename;  cout << "Enter filename: ";  cin >> filename; // You may prefer "getline( cin, filename );"  ifstream fin( filename.c_str() );  if( fin.fail() ) {    // File does not exist.  }  else {    // File exists.    // Ask user if he/she wants to overwrite  }}


[Edited by - jflanglois on December 16, 2005 10:01:24 PM]
I think jflanglois has it right. You are relying on documentation that refers to the old, pre-standard fstream classes. Here, infile.open(FILENAME) is all you really need to write.
"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

This topic is closed to new replies.

Advertisement