|
|
#1 |
|
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;
}
EDIT: It keeps saying Program received signal: "SIGABRT". |
|
|
|
0
|
|
|
#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");
-Lee |
|
|
|
0
|
|
|
#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. Last edited by aprofetto; Jan 24, 2010 at 04:40 PM. |
|
|
|
0
|
|
|
#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. |
|
|
|
0
|
|
|
#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;
}
You first posted about 4 hours ago. If that's the amount of time it takes for you to be Quote:
Code:
g++ -o myprog myprog.cpp ./myprog Post your exact code. Post your exact data file and it's path, and maybe we can help more. Good luck. -Lee |
||
|
|
0
|
|
|
#6 | |
|
Quote:
http://forums.macrumors.com/showthread.php?t=796818 Last edited by chown33; Jan 25, 2010 at 12:12 AM. |
||
|
|
0
|
|
|
#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. |
|
|
|
0
|
|
|
#8 | |
|
Quote:
But I do appreciate you taking the time to help me, I will try the suggestions and keep trying. |
||
|
|
0
|
|
|
#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. |
|
|
|
0
|
|
|
#10 | |
|
Quote:
(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...) |
||
|
|
0
|
|
|
#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! |
|
|
|
0
|
|
|
#12 | |
|
Quote:
|
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| thread | Thread Starter | Forum | Replies | Last Post |
| How to create and use input/output files with Xcode C++ | hotglass | Mac Programming | 14 | Feb 6, 2012 10:53 PM |
| Reading and writing files and storage locations xcode | portreathbeach | iPhone/iPad Programming | 1 | Apr 2, 2011 06:22 PM |
| Problem opening a *.txt file in Xcode Command Line Application | Averello | Mac Programming | 2 | Dec 23, 2009 08:46 AM |
| Can MacBook read chinese .txt files? | Sasuke-chan | MacBook | 15 | Nov 9, 2009 08:41 PM |
| Multiline txt file with Geektool | robsilverii | Mac Applications and Mac App Store | 0 | May 2, 2009 12:04 AM |
All times are GMT -5. The time now is 12:26 AM.







Linear Mode

