Monday, July 27, 2009

How do I write multiple lines of string to an output file in C?

I have an array of strings (history) that I need to write to an output file.


When I use the following code, the output file contains the contents but without the newlines even when I specified it to write a new line after every entry.


FILE *ofp;


ofp = fopen("history.list", "w");


int temp = cmdnum;


while(temp!=0)


{


fprintf(ofp, "%s\n", history[temp-1]);


temp--;


}





For example if history contains:


a


b


c





The output file that results is:


abc





Thanks for your

How do I write multiple lines of string to an output file in C?
Try opening the file in text-mode: "wt" that should convert all \n to \r\n which is required on some systems to display line breaks.

pear

No comments:

Post a Comment