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

Fawn2003

macrumors newbie
Original poster
Feb 16, 2008
4
0
I am a newbie to programming learning from Malik's Book C++ . My issue is with how to open files from my mac, the book uses examples and programs from Windows PC, no good for me. So far i have had no problems using C++ on Xcode. I am stumped on the language for inputing/outputting a file. I am not even exactly sure what question to ask.I copied the program from the book and pasted it here, as it states my confusion is how to format the file after .open. I am not even sure, can any file be accessed for this process? I just created a simple word doc but doesnt work. Any help would be appreciated, searched the net for solutions

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>


using namespace std;

int main ()
{
ifstream inFile;
ofstream outFile;

double test1, test2, test3, test4, test5;
double average;

string firstName;
string lastName;

inFile.open("confused right here");
outFile.open("confused right here");

outFile << fixed << showpoint;
outFile << setprecision(2);

cout << "Processing data" << endl;

inFile >> firstName >> lastName;
outFile << "Student Name: " << firstName
<< " " << lastName << endl;

inFile >> test1 >> test2 >> test3
>> test4 >> test5;
outFile << "Test scores: " << setw(6) << test1
<< setw(6) << test2 << setw(6) << test3
<< setw(6) << test4 << setw(6) << test5
<< endl;

average = (test1 + test2 + test3 + test4 +test5) / 5.0;

outFile << "Average test score: " << setw(6)
<< average << endl;

inFile.close();
outFile.close();


return 0;
}
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
You need to use Unix style paths. For example, your Desktop would be "/Users/you/Desktop". Files should be plain text (.txt). Word files will not work for plain text. Use TextEdit to edit plain text files (Format > Make Plain Text).

If you're running from Xcode you need to learn about the current working directory. I'm not sure what Xcode uses, but if you put in for the path "file.txt" it would use the current working directory. So it is safest to use an absolute path, such as "/Users/you/Desktop/file.txt".
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Yes, with Xcode, the current directory depends on the current Build settings. If the build is DEBUG (the default), the current working directory will be the /debug folder inside your C++ project. As kainjow mentions, it's just easier to use full paths.

Inside the .open() call, first you specify the file name, as a c-style string. After the comma, you can use ios flags, like ios::in or ios::out, depending on the intended use.

See here: http://www.cppreference.com/cppio/open.html

Todd
 

Fawn2003

macrumors newbie
Original poster
Feb 16, 2008
4
0
ok

i did what you said with TextEdit and placed the following

inFile.open("/Users/edwardhanes/Desktop/testscores1.txt", ios::in);
outFile.open("/Users/edwardhanes/Desktop/testscores2.txt", ios::eek:ut);

and without ios::in , ios::eek:ut
no go

then i got an input stream error with your suggestion
 

Fawn2003

macrumors newbie
Original poster
Feb 16, 2008
4
0
nevermind, i got it, didnt realize that the data would not show up on the terminal, its in the file

thanks!!!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.