C++ Printing Vector Adding New Line

I have a vector of structs.

1
2
3
4
5
6
struct myStruct{
string text;
int num;
};

vector<myStruct> foo;

And I am attempting to print the text followed by a space, then the number. Like so:

foobar 5

But when trying to print my vector using

1
2
3
4
5
6
ofstream outputFile;
outputFile.open ("file.txt");

for(int i = 0; i < foo.size(); i++) {
        outputFile << foo[x].text << " " << foo[x].num << endl;
    }

It prints like

1
2
3
4
foobar  
 5 
moretext  
 8

With an extra newline and space. I can't figure out how to get rid of it and print it on the same line. Any suggestions?

I have checked that text does not include new line character at the end. Also, it seems to print correctly using cout, but not print correctly to output file...
I have checked that text does not include new line character at the end.


It must... as that is the only way this could happen. How did you check? With a debugger?

Also, it seems to print correctly using cout, but not print correctly to output file...


You might have a stray \r at the end of your string... but not a stray \n. I don't think cout will treat \r as a newline... but many text editors might.
Topic archived. No new replies allowed.