Ran
Ran

Reputation: 163

How to add data into a new line of a txt file

So, if I have something like this on a txt file:

file.txt;5;12:40

file2.txt;6;13:40

I want in the program to add another line such as

"file3.txt;7;12:40"

To be like:

file.txt;5;12:40

file2.txt;6;13:40

file3.txt;7;12:40

Yet when I do it with my code, it stays like:

file.txt;5;12:40

file2.txt;6;13:40file3.txt;7;12:40

My code (I took the if's about if the file exists or not to be more simple to see the real code):

printf("Insert the name of the file\n");
printf("[Name] ");
getchar();
scanf("%[^\n]s",&orderNameFile);
printf("\n");
                
orderFile = fopen("order.txt","a");
contentFile = fopen(orderNameFile,"r");

// This is supposed to be into an if
printf("Insert the seconds for presentation\n");
printf("[Time in seconds] ");
scanf("%d",&orderSecondsFile);

printf("Insert the time\n");
printf("[Time HH:MM (Hour:Minutes)] ");
getchar();
scanf("%[^\n]s",&orderTimeFile);
printf("\n");

fprintf(orderFile,"%s;%d;%s\n", orderNameFile, orderSecondsFile, orderTimeFile);
opOrder = 1;

fclose(contentFile);

I've tried to take the \n out of the fprintf and make another, put it on the beginning, on the ending, but it won't make an enter into a txt file.

Any idea of how to do it?

Output:

file.txt;5;12:40

file2.txt;6;13:40file3.txt;7;12:40

What I want:

file.txt;5;12:40

file2.txt;6;13:40

file3.txt;7;12:40

Upvotes: 1

Views: 160

Answers (1)

Gopi
Gopi

Reputation: 19864

orderFile = fopen("order.txt","wt");

or

fprintf(orderFile,"\r\n");

Upvotes: 1

Related Questions