Avinash Babu
Avinash Babu

Reputation: 6252

Write to a file in C

In C programming I have learned about file io and I have run the sample code which is:

#include <stdio.h>

main()

{

 FILE *fp;

 fp = fopen("E:\\tmp\bae.txt", "w+");
 fprintf(fp, "This is testing for fprintf...\n");
 fputs("This is testing for fputs...\n", fp);
 fclose(fp);

 return 0;
}

Here the code works fine and fputs() returns -1 which means the code works fine. I have created a directory tmp on the E: drive, but this code doesn't create the file bae.txt..'

Can anyone tell me why this is happening?

Upvotes: 2

Views: 173

Answers (1)

Sourav Kanta
Sourav Kanta

Reputation: 2757

Instead of

 fp = fopen("E:\\tmp\bae.txt", "w+");

use

 fp = fopen("E:\\tmp\\bae.txt", "w+");  

as \ has specific meaning in a string.

Upvotes: 5

Related Questions