C++ open input file failed

Discussion in 'Mac Programming' started by Jordy664, Dec 7, 2010.

  1. Jordy664, Dec 7, 2010
    Last edited by a moderator: Dec 8, 2010

    Jordy664 macrumors newbie

    Joined:
    Dec 7, 2010
    #1
    This is the beginning of the code i have and it keeps returning the "cannot open file" and i cannot see why...

    Code:
    #include <fstream> 
    #include <cstdlib> 
    #include <iostream> 
    #include <iomanip> 
    #include <math.h>
    
    using namespace std; 
    
    #define MAXD 100 
    
    ifstream in_file; 
    ofstream out_file;
    
    void read_input(int& NJ, double COORD[MAXD][2], int& NS, int MSUP[MAXD][3], int& NMP, double EM[MAXD], int& NC, double CP[MAXD], int& NM, double MPRP[MAXD][4], int& NLJ, int JP[MAXD], double PJ[MAXD][2]);
    void Struct_Stiffness(double KL[4][4], double L, double E, double A, double C, double S);
    
    int main()
    {
    	int NJ, NS, NMP, NC, NM, NLJ;
    	double COORD[MAXD][2]={0};
    	int MSUP[MAXD][3]={0};
    	double EM[MAXD]={0};
    	double CP[MAXD]={0};
    	double MPRP[MAXD][4]={0};
    	int JP[MAXD]={0};
    	double PJ[MAXD][2]={0};
    	double Stiff[MAXD][MAXD]={0};
    
    	int NP[4];
    	
    	int el=0;
    	int i, j;
    	
    	in_file.open("input.txt");
    	out_file.open("output.txt");
    	
    	
    	if(in_file.fail()) 
    	{ 
    		cout << "The file cannot be opened." << endl; 
    		exit(1); 
    	} 	
    	
    	read_input(NJ, COORD, NS, MSUP, NMP, EM, NC, CP, NM, MPRP, NLJ, JP, PJ); 
    	..................
     
  2. chown33, Dec 7, 2010
    Last edited: Dec 7, 2010

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Two thoughts:

    1. Print the working directory. Make sure it's what you expect.
    If it's not what you expect, change the settings of the executable. The working directory is set under the General tab of the Executable's info pane.


    2. Remove these in the build settings:
    • _GLIBCXX_DEBUG=1
    • _GLIBXX_DEBUG_PEDANTIC
    http://forums.macrumors.com/showthread.php?t=867839
    http://forums.macrumors.com/showthread.php?t=863761
    http://forums.macrumors.com/showthread.php?t=837748
    http://forums.macrumors.com/showthread.php?t=796818
    http://forums.macrumors.com/showthread.php?t=780440
    .. 867839 lists specific steps to take.
    .. 796818 notes input problems.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    Since these macros come up so often I made a guide (so I wouldn't have to track down links). It should show up at the top of the forum.
    http://guides.macrumors.com/Why_simple_CPlusPlus_programs_in_XCode_crash_when_using_std::string

    -Lee
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    I forgot about that. I had the URLs in my Frequently Pasted URLs file, so that's what I used.

    The guide says "crash", though, when the problem may not be a crash.

    Also, it doesn't say what the problematic macro definitions are.

    Finally, it doesn't reference any of the articles that describe the problem and solution.
     
  5. Jordy664 thread starter macrumors newbie

    Joined:
    Dec 7, 2010
    #5
    It is in the working directory because when the output file is opened that is where it appears... sorry im quite new to this
     
  6. Jordy664 thread starter macrumors newbie

    Joined:
    Dec 7, 2010
    #6
    I now realize that the program continues to run (loop) as well and i have to stop it manually... does this have something to do with it?
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Your posted code doesn't have a loop. If you want anyone to comment on that code, you'll have to post it.

    I can tell by the posted code that you've got a lot of data being parsed, so it's entirely possible that the parsing is broken. But without seeing code, all anyone can do is guess.

    If you just want to find out if the input file opened, then add a cout << "It Opened" to your code, right after the if() that tests for failure.
     

Share This Page