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

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

    #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;
    		cout << "The file cannot be opened." << endl; 
    	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

    Aug 9, 2009
    Sailing beyond the sunset
    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:
    .. 867839 lists specific steps to take.
    .. 796818 notes input problems.
  3. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    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.

  4. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    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

    Dec 7, 2010
    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

    Dec 7, 2010
    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

    Aug 9, 2009
    Sailing beyond the sunset
    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