Jump to content

How To Write To a Text File

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 297 posts
Hello All,

I posted a thread on this ages ago, but I didn't get it and the code didn't work, anyway the code I was given was as follows:

[FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]
using[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] std;
ofstream outstream([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515]"loada.txt"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]);
outstream << firstName;
outstream << secondName;
[/SIZE][/FONT][/SIZE][/FONT] 
[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]ofstream myfile ([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515]"loada.txt"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], ios::app);
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (myfile.is_open())
{
myfile <<[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515]"\n"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]<<firstName<<[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515]" | "[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]<< secondName;
myfile.close();
}
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] cout << [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515]"Unable to open file"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2];
}
[/SIZE][/FONT][/SIZE][/FONT]

I'd like to know if there is a simpler code that works, or what to put (like replace loada.txt with the name of the text file I want).

Thanks.


#2
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
There are a lot of things fishy with this code man ^_^
First of all, did you remember to #include <iostream>
and #include <fstream>?
second, you forgot a space between using and namespace... not a big one, but thought I'd put it out there...
Now... You write to the text file twice... Once here:
outstream << firstName;
outstream << secondName;
and once here:
myfile <<"\n"<<firstName<<" | "<< secondName;
myfile.close();

And I'm guessing that is why your program didn't work?

#3
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 297 posts
So I take out
myfile <<"\n"<<firstName<<" | "<< secondName;
myfile.close();

Also, I had an error when putting the code up there, in the real source it did have a space in between the using namespace std and I did inlude those libraries, any others I should of included?

Thanks.

#4
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
If I were you, I'd rather take out:

ofstream outstream("loada.txt");
outstream << firstName;
outstream << secondName;