I code the way it is suppose to, but cgywin closes the window after compiling, and I don't understand why? Does anyone know how to troubleshoot cgywin for fstream?
It's mostly a hit or a miss...
I code the way it is suppose to, but cgywin closes the window after compiling, and I don't understand why? Does anyone know how to troubleshoot cgywin for fstream?
It's mostly a hit or a miss...
Code:#include<fstream>#include <string> #include <iostream> using namespace std; int main() { const int RANGE = 12; string tab[ RANGE ]; int i = 0, j = 0; ifstream reader( "records.txt"); if ( ! reader ) { cout << "Error opening file" << endl; return -1; } while ( ! reader.eof()) { if ( ( i + 1) % 4 == 0 ) getline( reader, tab[ i++ ], '\n' ); else getline( reader, tab[ i++ ], '\t' ); } reader.close(); i = 0; while ( i < RANGE ) { cout << endl << "Record Number: " << ++j << endl; cout << "Forname: " << tab[ i++ ] << endl; cout << "Surname: " << tab[ i++ ] << endl; cout << "Department: " << tab[ i++ ] << endl; cout << "Telephone: " << tab[ i++ ] << endl; } return 0; }
Can you run it from a Cygwin terminal directly rather than (I'm guessing, since you didn't say) from your IDE? I imagine you'll see the message "Error opening file".
If you want the cause of the error, also print strerror(errno) (and add #include <cerrno>).
The above line needs to prevent i from getting too large or you will get random crashes if the file is different from what you think it is.Code:while ( ! reader.eof())
Tim S.
Last edited by stahta01; 10-25-2022 at 10:03 PM.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson