Click to See Complete Forum and Search --> : code doesn't run right


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?

Dun'kalis
12-16-2002, 11:12 PM
I'm not a C++ guru, but you probably need a few lines for the iostream functions

using std::cin
using std::cout

and so forth.

Rüpel
12-17-2002, 04:21 AM
your error is here:

while (fin.get(ch));
cout << ch;

remove the semicolon at the end of the first line. the cout is only called once because the statement of the while-loop is the empty statement. write it on one line and you see

while (fin.get(ch)) ; cout << ch;

this way, cout is not in the loop.

btw: if this was not a typo and you want that empty statement there (although the compiler will opt it out anyway, the following strange code compiles and runs fine:

while (fin.get(ch))
{
;
cout << ch;
}

:p

yawningdog
12-17-2002, 07:15 AM
Thanks. :)

bwkaz
12-17-2002, 11:19 AM
Originally posted by Dun'kalis
using std::cin
using std::cout This is nothing more than the clean way to do what he did with using namespace std;. Importing an entire namespace does work, but it clutters the default namespace quite a bit, and completely defeats the purpose of putting all Standard C++ Library stuff in its own namespace.

Separate using statements are cleaner. The only downside is that if you need another class or object from std, then you need to add another using, but I don't think that's all bad.

Rüpel
12-17-2002, 04:18 PM
bwkaz:
you should add something like: "brave fighter against using namespace std;" in your sig.

i think this is the 3rd time, you post a statement like this. of course you're right! but the funny thing is, i knew who postet that text before reading the name at the left... ;)

bwkaz
12-17-2002, 06:49 PM
:D

This probably wasn't the greatest thread to bring it up in, either, but I thought ah, what the heck.

PS: Like the sig now? :p

Rüpel
12-18-2002, 06:21 AM
perfect! :D

bwkaz
12-18-2002, 09:44 AM
:D