Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 24, 2010, 04:13 PM   #1
aprofetto
macrumors 6502a
 
aprofetto's Avatar
 
Join Date: Dec 2008
Location: Hamilton, Ontario
Trouble reading a .txt file with Xcode C++

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".
aprofetto is offline   0 Reply With Quote
Old Jan 24, 2010, 04:29 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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
lee1210 is offline   0 Reply With Quote
Old Jan 24, 2010, 04:34 PM   #3
aprofetto
Thread Starter
macrumors 6502a
 
aprofetto's Avatar
 
Join Date: Dec 2008
Location: 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.

Last edited by aprofetto; Jan 24, 2010 at 04:40 PM.
aprofetto is offline   0 Reply With Quote
Old Jan 24, 2010, 07:44 PM   #4
aprofetto
Thread Starter
macrumors 6502a
 
aprofetto's Avatar
 
Join Date: Dec 2008
Location: 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.
aprofetto is offline   0 Reply With Quote
Old Jan 24, 2010, 08:18 PM   #5
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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
Quote:
...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
lee1210 is offline   0 Reply With Quote
Old Jan 25, 2010, 12:01 AM   #6
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by lee1210 View Post
Post your exact code. Post your exact data file and it's path, and maybe we can help more.
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

Last edited by chown33; Jan 25, 2010 at 12:12 AM.
chown33 is offline   0 Reply With Quote
Old Jan 25, 2010, 08:35 AM   #7
squier
macrumors newbie
 
Join Date: Jun 2006
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.
squier is offline   0 Reply With Quote
Old Jan 25, 2010, 09:12 AM   #8
aprofetto
Thread Starter
macrumors 6502a
 
aprofetto's Avatar
 
Join Date: Dec 2008
Location: Hamilton, Ontario
Quote:
Originally Posted by lee1210 View Post
...

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 is offline   0 Reply With Quote
Old Jan 25, 2010, 09:21 AM   #9
aprofetto
Thread Starter
macrumors 6502a
 
aprofetto's Avatar
 
Join Date: Dec 2008
Location: 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.
aprofetto is offline   0 Reply With Quote
Old Jan 25, 2010, 12:13 PM   #10
Detrius
macrumors 68000
 
Join Date: Sep 2008
Location: Asheville, NC
Quote:
Originally Posted by aprofetto View Post
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...)
Detrius is offline   0 Reply With Quote
Old Sep 9, 2010, 10:25 AM   #11
11ama
macrumors newbie
 
Join Date: Sep 2010
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!
11ama is offline   0 Reply With Quote
Old Jan 15, 2013, 11:08 PM   #12
anhkiet
macrumors newbie
 
Join Date: Jan 2013
Quote:
Originally Posted by 11ama View Post
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).
anhkiet is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Applescript - formatting a txt file using write dam316 Mac Applications and Mac App Store 0 May 4, 2014 05:26 PM
Resolved: Trouble reading a .txt file in Xcode with C++ rayonix Mac Programming 2 Jan 4, 2014 04:03 PM
Xcode 4.6. Can't open .txt file thrill4rishabh Mac Programming 6 May 28, 2013 01:22 PM
Apple Hardware Test had trouble reading env file nfear24 MacBook Pro 3 Jan 3, 2013 05:02 PM
Resolved: Applescript: List/txt.file question h1068 Mac Programming 5 Jul 3, 2012 06:30 PM

Forum Jump

All times are GMT -5. The time now is 04:48 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC