-
Files...
I'm using the following code to read in data to a file.
When I read out the file, the data is being outputed underneath each other like this:
1
2
3
I want to read it out like this:
1 2 3
This is my code:
Code:
#include <iostream.h>
#include <fstream.h>
void main(){
ofstream OutFile("test.txt");
OutFile << "Hello world!";
OutFile.close();
OutFile.open("numbers.txt");
OutFile << 15 << " " << 42 << " " << 1;
OutFile.close();
ifstream InFile;
InFile.open("test.txt");
char p[50];
InFile >> p;
cout << p << endl;
InFile >> p;
cout << p << endl;
InFile.close();
int TempNum;
InFile.open("numbers.txt");
while (!InFile.eof()) {
InFile >> TempNum;
if (!InFile.eof())
cout<< TempNum << endl;
}
InFile.close();
InFile.open("test.txt");
while (!InFile.eof()) {
InFile >> p;
cout << p << endl;
}
}
-
Remove the endl from the cout lines and see what happens.
-
Instead of endl, you'll need ' ' (a space)
-
like this maybe:
Code:
cout<<val1<<" "<<val2<<" "<<val3<<endl;
-
Err.... surely that's not what you meant, that's far too obvious... *confused*
-
Yes, i know...I should be asking those questions, not answering them! heheh:p
-
Hmm maybe he should spend more time debugging simple errors and less time posting questions :rolleyes: Takes 2 mins to find the error, or you can wait a few hours for an answer.