Thread: Cgywin won't open fstream files...

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    99

    Cgywin won't open fstream files...

    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...

  2. #2
    Registered User
    Join Date
    Dec 2019
    Posts
    99
    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;
    }

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    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>).

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by christop View Post
    If you want the cause of the error, also print strerror(errno) (and add #include <cerrno>).
    fstream don't mess with errno, I believe.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    while ( ! reader.eof())
    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.

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-20-2012, 09:27 PM
  2. fstream open method causing me a few headaches
    By Finchie_88 in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2010, 06:44 PM
  3. fstream open parameters
    By Ancient Dragon in forum C++ Programming
    Replies: 5
    Last Post: 01-27-2006, 11:34 AM
  4. fstream lets you open a file for output twice (?!)
    By DonFiasco in forum C++ Programming
    Replies: 1
    Last Post: 06-09-2005, 07:41 PM
  5. Why won't fstream file open?
    By Brown Drake in forum C++ Programming
    Replies: 4
    Last Post: 11-20-2001, 11:30 AM

Tags for this Thread