file i/o


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

using namespace std;

int main() {

//init var
string name;
char adSeen;
int age;
int adScore;
string filePath;
ifstream file;
char ch;

cout << fixed << setprecision(2);

//prompts for file and opens file
cout << "Please enter file path: ";
cin >> filePath;
file.open(filePath.c_str());

//prompts error and exits
if(!file.is_open()) {
cout << "Error: Unable to open input file.";
}

//echos file
while(!file.eof()) {
file.get(ch);
cout << ch << endl;
}


file.close();
//pause and exit
getchar();
getchar();
return 0;

}

This is my program so far. I have a file called 'results.txt' in the same file as my program and I am suppose to read and echo it. My program does not read the file but prompts the error message every time. Any suggestions?
Are you typing in "results.txt" when prompted to? (emphasis on not forgetting the .txt file type). And is your results.txt file in the same folder as your .cpp file?

1
2
3
file.open(filePath.c_str()); // c_str() isn't necessary
// can just do this
file.open (filePath);
Last edited on
Just a small comment to Phil's answer. It is not necessarily the place where your cpp file is. It should be in your "current working directory" when your executable is run. And this may depend on IDE if you use one. For example in Visual Studio you can see or set the directory in the project properties (Configuration Properties -> Debugging -> Working Directory). The results.txt file should be in that directory.
@Phil123
c_str() isn't necessary in C++11. For older compilers it's necessary.

@kgoose
Make sure that results.txt is located in the working directory. When running the program from the IDE the working directory is often not the same as the directory where the executable file is located.

EDIT: Ninjad!
Last edited on
@Phil123
c_str() isn't necessary in C++11. For older compilers it's necessary.


Hmm, so are the new C++11 features included in Microsoft Visual C++ 2010 edition? Seeing as I don't use c_str()...I assume yes? Care to explain a bit about C++11 and how you'd be able to tell if the compiler you're using is current with C++11 features?
Thanks for the help guys I have got it to work :) but now i have another problem. Here is my code:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

int main() {

//init var
string name;
char adSeen;
int age;
int adScore;
int count;
string filePath;
char ch;

cout << fixed << setprecision(2);

//prompts for file and opens file
cout << "Please enter file path: ";
cin >> filePath;
cout << endl;
ifstream file;
file.open(filePath.c_str());

//prompts error and exits
if(!file.is_open()) {
cout << "Error: Unable to open input file.";

getchar();
getchar();
return 0;
}

//echos file
while(!file.eof()) {
file.get(ch);
cout << ch;
}

//restores input file data
file.clear();
file.seekg(0);

cout << endl << endl;

//Average score for subjects under 18 who have not seen the ad
while(!file.eof()) {
file >> name >> adSeen >> age >> adScore;

}
file.seekg(0);

//close input file
file.close();

//pause and exit
getchar();
getchar();
return 0;

}

This is my last resort I would rather figure it out myself than to ask for help, but I am really stumped.

Here is the assignment i was given:

A certain advertising company has commissioned a study to determine if a recent advertising campaign is effective for certain age groups. Researchers have created a data file (results.txt) that contains the results attained by the study. The first column is the subject’s name (you may assume there is no whitespace in any name) and the second column indicates if the subject has seen the advertisement in question (Y or y means yes, N or n means no). The third column specifies the subject's age and the last column (the 'score') specifies how favorably the subject views the product being advertised (from 0 to 100 with a score of 100 being most favorable). A example of the file results.txt is attached as part of this assignment. You should download this file to a convenient place on your hard drive. Please note that the file is a sample only. Your program should work on any data file that is formatted in a similar way.

Your assignment is to prompt the user to enter the full pathname to the data file on disk. If the file does not exist in the specified location, your program should exit with a suitable error message.

The first thing your program should do is output to the screen a copy of the data read in from the disk file. This is known as “echoing” the input data.

Your program should then calculate and display the the following results:
 Average score for subjects under 18 who have not seen the ad.
 Average score for subjects under 18 who have seen the ad.
 Average score for subjects 18 and over who have not seen the ad.
 Average score for subjects 18 and over who have seen the ad.
Display your results to two places of decimals, and write your program to automatically list your four calculated averages one to a line, along with a suitable label for each result.

Finally, your program should calculate and display to two places of decimals the overall average score for all of the subjects surveyed. (Note: Mathematically, this is NOT the average of the four averages you calculated previously).
Topic archived. No new replies allowed.