00PS
- 30
- 0
I am in a C++ class, programming a command line utility, and I can't figure out how to open and read from an input file in xcode. any help?
The discussion focuses on how to open and read input files in a C++ command line utility using Xcode. Users are advised to utilize the ifstream class for file handling and to ensure the correct "present working directory" is set, as this often leads to issues when files cannot be found. Solutions include specifying the full path of the file or moving the file into the appropriate directory. Additionally, using cin.getline is recommended for filenames with spaces, and the inclusion of int argc, char * const argv[] in the main function signature is noted as a potential requirement for proper execution.
ifstreamifstream and its methods for file input in C++cin.getline for reading input with spacesargc and argv in C++ command line applicationsC++ developers, students in programming courses, and anyone looking to enhance their command line utility skills in Xcode.
Coin said:Is this a console app or an OS X (Carbon/Cocoa) app?
If it's a console app you want to use the ifstream classes. Look for something like "ifstream example" on google. (Alternately you can use fopen/fclose and the related C methods.)
Coin said:The problem is most likely that the "present working directory" when your program runs is not what you think it is. You need to either:
- Specify the full path of the file, i.e. something like /Users/oops/filename.txt
- Move the file you want to open into the "present working directory" of your program. I believe you can find this by looking in the "build" directory which is in the same directory as your xcode file, then looking inside "debug".
chroot said:Learn how to use your shell.
- Warren
[Jons-Mac-Pro:~/Documents/c++] jtbell% ls
csv.cpp csv.dat
[Jons-Mac-Pro:~/Documents/c++] jtbell% cat csv.cpp
// Demonstrates using getline() and a stringstream to read a file
// of comma-separated values.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main ()
{
ifstream inFile ("csv.dat");
string line;
int linenum = 0;
while (getline (inFile, line))
{
linenum++;
cout << "\nLine #" << linenum << ":" << endl;
istringstream linestream(line);
string item;
int itemnum = 0;
while (getline (linestream, item, ','))
{
itemnum++;
cout << "Item #" << itemnum << ": " << item << endl;
}
}
return 0;
}
[Jons-Mac-Pro:~/Documents/c++] jtbell% cat csv.dat
8/29/2008,19.54,19.6,19.28,19.38,11204900,19.38
8/28/2008,19.48,19.76,19.38,19.65,11729500,19.65
[Jons-Mac-Pro:~/Documents/c++] jtbell% g++ csv.cpp -o csv
[Jons-Mac-Pro:~/Documents/c++] jtbell% ls
csv csv.cpp csv.dat
[Jons-Mac-Pro:~/Documents/c++] jtbell% ./csv
Line #1:
Item #1: 8/29/2008
Item #2: 19.54
Item #3: 19.6
Item #4: 19.28
Item #5: 19.38
Item #6: 11204900
Item #7: 19.38
Line #2:
Item #1: 8/28/2008
Item #2: 19.48
Item #3: 19.76
Item #4: 19.38
Item #5: 19.65
Item #6: 11729500
Item #7: 19.65
[Jons-Mac-Pro:~/Documents/c++] jtbell%
What should I do about the spaces in the directory?
00PS said:Apparently, you need to put the argument 'int argc, char * const argv[]' in int main.