They gave two examples:
The 1st which they taught me how to create a file and put strings into the file:
Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int main() {
char filename[81];
cout << "Enter a file name and press ENTER: ";
cin.getline(filename, 80);
ofstream file_out(filename);
if (! file_out) {
cout << "File " << filename << " could not be opened.";
return -1;
}
cout << "File " << filename << " was opened.";
file_out << "I am Blaxxon," << endl;
file_out << "the cosmic computer." << endl;
file_out << "Fear me.";
file_out.close();
return 0;
}
...in the next example they gave me, (according to the book) I am suppose to be able to open that same file with whatever name/directory i gave it and, if i choose, put more strings into the file:
Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int c; // input character
int i; // loop counter
char filename[81];
char input_line[81];
cout << "Enter a file name and press ENTER: ";
cin.getline(filename, 80);
ifstream file_in(filename);
if (! file_in) {
cout << "File " << filename << " could not be opened.";
return -1;
}
while (1) {
for (i = 1; i <= 24 && ! file_in.eof(); i++) {
file_in.getline(input_line, 80);
cout << input_line << endl;
}
if (file_in.eof())
break;
cout << "More? (Press 'Q' and ENTER to quit.)";
cin.getline(input_line, 80);
c = input_line[0];
if (c == 'Q' || c == 'q')
break;
}
return 0;
}
However, all it does is opens the file and tells me to press any key to exit.
Is their a problem with my compiler? I am currently using a trial version of Visual C++ 2008 express.
Any help would be great thank you!