Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

aprofetto

macrumors 6502a
Original poster
Dec 19, 2008
544
0
Hamilton, Ontario
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".
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 
  • Like
Reactions: Xfighter32

aprofetto

macrumors 6502a
Original poster
Dec 19, 2008
544
0
Hamilton, Ontario
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.
 

aprofetto

macrumors 6502a
Original poster
Dec 19, 2008
544
0
Hamilton, Ontario
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
...starting to lose all hope
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
 

squier

macrumors newbie
Jun 21, 2006
16
0
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.
 

aprofetto

macrumors 6502a
Original poster
Dec 19, 2008
544
0
Hamilton, Ontario
...

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

aprofetto

macrumors 6502a
Original poster
Dec 19, 2008
544
0
Hamilton, Ontario
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.
 

Detrius

macrumors 68000
Sep 10, 2008
1,623
19
Apex, NC
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.

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

11ama

macrumors newbie
Sep 9, 2010
1
0
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!
 

anhkiet

macrumors newbie
Jan 15, 2013
1
0
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!

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).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.