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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write/read strings with spaces and line breaks to/from a text file

Status
Not open for further replies.

chaco

Programmer
Jan 2, 2002
1
NL
I have a question about C++.

Assume

char timestamp[255];
char title[255];
etc..

and examples of the two strings:

Mon Oct 22 22:47:25 2001
My title
etc..

I want to save the data in a neat text file, and then read it in again.

I save the data by using:

fprintf (fp, "%s\n", timestamp);
fprintf (fp, "%s\n", title);
etc..

(I need the \n to have things presented properly in a text file)

and then I'm trying to read the data back by using:

fscanf (fp, "%s", timestamp);
fscanf (fp, "%s", title);
etc..

Because of the way the function fscanf works, it stops reading at the first space in either. I also don't want the /n character to be read as well, otherwise when I write all back again, extra empty lines are inserted in the text file.

I cannot find a trick to to it correctly. Can you help?

Regards,
Chaco van der Sijp
 
Hey,

look at the offline MSDN (comes with vc++) for:

fgets: reads the number of chars you specified, or until the \n character if it comes before it reaches the number you specified.
fgetc: just read one single character
fread: reads the number of characters you specify. returns the number of chars read, so you can check if it comes to end-of-file.

i recommend using fread, read all data in file or some lines, whatever you want, the format them, find for \n chras and seperate lines etc...

Daniel Cohen Gindi
dcgsmix@gmx.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top