Trouble reading a .txt file with Xcode C++

Discussion in 'Mac Programming' started by aprofetto, Jan 24, 2010.

  1. macrumors 6502a

    aprofetto

    Joined:
    Dec 19, 2008
    Location:
    Hamilton, Ontario
    #1
    Hey guys, the following code simply won't work for me?

    The file is there, I have tried both the way it currently is, and also using the full file path, neither seem to work.

    Code:
    // reading a text file
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main () {
      string line;
      ifstream myfile ("example.txt");
      if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          getline (myfile,line);
          cout << line << endl;
        }
        myfile.close();
      }
    
      else cout << "Unable to open file"; 
    
      return 0;
    }
    Any ideas? Thanks guys.

    EDIT: It keeps saying Program received signal: "SIGABRT".
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    I just ran this without issue. With the code as-is, you need example.txt in the working directory your program is run in. If you are running from terminal, that will just be the current directory. If you are running from XCode it's going to be somewhere nestled deep in the build directory. I think the easiest way to figure it out is to add the line:
    Code:
    system("pwd");
    To your code, and see what directory is printed. This isn't really portable, but it will work on OS X fine.

    -Lee
     
  3. thread starter macrumors 6502a

    aprofetto

    Joined:
    Dec 19, 2008
    Location:
    Hamilton, Ontario
    #3
    Okay, that code gave me the path /Users/Adam, so I added that into the code and now I no longer get that weird error, but it still cannot open the file?

    EDIT: It seems to be going into the 'ELSE' part of the loop which means its either not opening the file, or is having trouble opening it?

    EDIT 2: I don't think the file path was wrong. When I enter the full file path, it gives that SIGABRT error.
     
  4. thread starter macrumors 6502a

    aprofetto

    Joined:
    Dec 19, 2008
    Location:
    Hamilton, Ontario
    #4
    After some searching online, it seems getline doesn't work in xcode?

    The solution I found is to use fgets, but fgets only reads the first line of the text file?

    Any ideas?

    Eventually I want to be able to read a .csv file... I'm starting to lose all hope.
     
  5. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    I ran this from the command line, compiled with g++. It works fine. So there's not reason to lose all hope. I find it essentially impossible that getline doesn't work when you compile something from XCode, because it's just using g++. I just started a new XCode project, command line tool, C++ stdc++. I pasted in your code. I didn't want to bother with figuring out where the pwd was, so i just changed the file path to /Users/xxx/example.txt where i created the one to test earlier. This worked fine. This is the exact code i used, with my username obscured:
    Code:
    // reading a text file
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main (int argc, char *argv[]) {
    	string line;
    	ifstream myfile ("/Users/xxx/example.txt");
    	if (myfile.is_open())
    	{
    		while (! myfile.eof() )
    		{
    			getline (myfile,line);
    			cout << line << endl;
    		}
    		myfile.close();
    	}
    	
    	else cout << "Unable to open file"; 
    	
    	return 0;
    }
    
    Therefore, there is *something* else going on. Your file isn't there, it has a crazy format, etc. for this not to work for you. I suppose some extremely unusual build settings in XCode could cause some problem, but that is unlikely.

    You first posted about 4 hours ago. If that's the amount of time it takes for you to be
    you need to adjust your expectations and attitude if you want to continue programming. I have spent weeks smashing my head against a problem (not consecutively, of course). I would not have a job, or at least not be as successful, if 4 hours was enough to defeat my resolve. Don't worry, things will start making more sense eventually. I would REALLY recommend dropping down to terminal to compile and run your code. I would advise learning vi(m) to edit text, too, but understand that it may be a bit overwhelming. So grab something like TextWrangler, and hit the terminal. To compile and run something like this:
    Code:
    g++ -o myprog myprog.cpp
    ./myprog
    And away you go. you know what directory you're running in, and you learn to use tools that can be used anywhere, not just OS X.

    Post your exact code. Post your exact data file and it's path, and maybe we can help more.

    Good luck.

    -Lee
     
  6. macrumors 603

    Joined:
    Aug 9, 2009
    #6
    To which I would add: also post the exact Xcode version and OS version. They may make a difference, such as described in this thread:
    http://forums.macrumors.com/showthread.php?t=796818
     
  7. macrumors newbie

    Joined:
    Jun 21, 2006
    #7
    I ran into this problem and found out it's a bug in xcode. The easiest solution is to change the configuration from "debug" to "release", which you can do from the drop down menu in the left hand corner of the main xcode window. I ran your code and it didn't work in debug mode but ran correctly in release mode.

    Edit: the thread chown33 links to describes other solutions to this problem.
     
  8. thread starter macrumors 6502a

    aprofetto

    Joined:
    Dec 19, 2008
    Location:
    Hamilton, Ontario
    #8
    I appreciate your help. And it hasn't been just 4 hours. I'm sorry you took what I said as serious as you did, it was not intended.

    But I do appreciate you taking the time to help me, I will try the suggestions and keep trying.
     
  9. thread starter macrumors 6502a

    aprofetto

    Joined:
    Dec 19, 2008
    Location:
    Hamilton, Ontario
    #9
    Strange, my friend's Xcode works.

    He's running 10.5 Xcode, and I'm running 10.6 Xcode, his code works fine, mine is giving the error described in post 1.

    Any ideas?

    EDIT: I should mention we're both running Snow Leopard.
     
  10. macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #10
    Did you follow the link posted by chown33? I see people running into issues with this bug (unknowingly) about every other week. It would be handy if we had a sticky. Of course, maybe Apple will update Xcode on Wednesday.

    (note: there's no such thing as "10.5 Xcode" and "10.6 Xcode." There's "Xcode 3.2," "Xcode 3.1," "Xcode 2.5," etc...)
     
  11. macrumors newbie

    Joined:
    Sep 9, 2010
    #11
    Hi,

    Did you set the working directory to point to the directory your txt file is in? If not right click the executable under Groups & Files, select Get Info, and finally set the correct path under working directory (directory where your txt file is located). You should be all set.

    Good Luck!
     
  12. macrumors newbie

    Joined:
    Jan 15, 2013
    #12
    Thanks for 1000 times.Just copy the .txt file then past it in the same folder with your product(the folder contains an unix executable file).
     

Share This Page