yawningdog
12-16-2002, 06:36 PM
Here's my code.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char fileName[80];
char buffer[255]; // for user input
cout << "File name: ";
cin >> fileName;
ofstream fout(fileName); // open for writing
fout << "Thhis line written directly to the file...\n";
cout << "Enter text for the file: ";
cin.ignore(1,'\n'); // eat the newline after the file name
cin.getline(buffer,255); // get the user's input
fout << buffer<< "\n"; // and write it to the file
fout.close(); // close the file, ready for reopen
ifstream fin(fileName); // reopen, redy for reading
cout << "Here's the contents of the file:\n";
char ch;
while (fin.get(ch));
cout << ch;
cout << "\n***End of file contents***\n";
fin.close(); // always pays to be tidy
return 0;
}
There is no output after "Here's the contents of the file". If I open the created file with vi, the text entered is there.
This works on my MS compiler, but I re-typed the code instead of just cutting and pasting, so I could have an error in syntax somewhere. I didn't use a makefile.
Can someone help with why this won't run properly?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char fileName[80];
char buffer[255]; // for user input
cout << "File name: ";
cin >> fileName;
ofstream fout(fileName); // open for writing
fout << "Thhis line written directly to the file...\n";
cout << "Enter text for the file: ";
cin.ignore(1,'\n'); // eat the newline after the file name
cin.getline(buffer,255); // get the user's input
fout << buffer<< "\n"; // and write it to the file
fout.close(); // close the file, ready for reopen
ifstream fin(fileName); // reopen, redy for reading
cout << "Here's the contents of the file:\n";
char ch;
while (fin.get(ch));
cout << ch;
cout << "\n***End of file contents***\n";
fin.close(); // always pays to be tidy
return 0;
}
There is no output after "Here's the contents of the file". If I open the created file with vi, the text entered is there.
This works on my MS compiler, but I re-typed the code instead of just cutting and pasting, so I could have an error in syntax somewhere. I didn't use a makefile.
Can someone help with why this won't run properly?