Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create file text and write multiple lines in it 1

Status
Not open for further replies.

PaolaS

Programmer
Aug 23, 2001
8
ES
I´m beginner in Visual C++, How can I to create a file text and write multiple lines in it?

Thanks

Paola S
 
#include <stdio.h>

int main( void )
{
FILE* fp = fopen( &quot;test.txt&quot;, &quot;wt&quot; );

if( fp ) {
fprintf( fp, &quot;line1\nline2\n&quot; );
fclose( fp );
}

return 0;
}


file contents will be :

line1
line2
Hee S. Chung
heesc@netian.com
 
You may want \r\n depending on how the text file viewer interprets \n

Matt
 
No. The file was opend in text(translated) mode
and '\r' will be automatically added if only '\n'
is used. And it is more compatible to use only '\n'
for other systems because unix doesn't want '\r' for
a text file.
Hee S. Chung
heesc@netian.com
 
I have had problems with it in notepad. Just looks like a black bar. All other programs such as word and word pad look fine. Notepad is the only place I have found a problem.
 
I think there is no problem when &quot;wt&quot; is specified
for fopen and the created program is executed on
DOS or Windows. Because line end character is
translated properly for each platforms when
text(translated) mode is used.

I tested notepad and there was no problem. Ultra
editor says the saved 'test.txt' is DOS format text
file. If &quot;wb&quot; is specified in my code instead of &quot;wt&quot;
, the saved file will be Unix format text.
Hee S. Chung
heesc@netian.com
 
see in C++ woking with files is much more simplier:
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
char* x[100];
int c;
ofsrteam xx(&quot;xx.txt&quot;);//same as fopen with w
xx<<&quot;hello world&quot;<<endl;
xx<<&quot;1 hello world&quot;<<endl;
xx<<&quot;2 hello world&quot;<<endl;
xx.close();
ifstream yy(&quot;xx.txt&quot;);//same as fopen with r
yy>>x;
cout<<x<<&quot; &quot;<<endl;
yy>>x;
cout<<x<<endl;
yy>>c;
yy.getline(x,100);
cout<<c<<&quot; &quot;<<x<<endl;
return 0;
} John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top