» Basic File IO in C by Christopher Hill -TrajectoryLabs.com |
| | Page 3 of 7 | |
| < Previous | | Next > |
|
(Login to remove green text ads)
Write characters to a file using fputc()
Code:
// Write characters to a file using fputc()
#include <stdio.h>
int main()
{
FILE *file_ptr; // 1. declare a file pointer
char text[50] = {"Text, one character at a time."};
int i;
file_ptr = fopen("chars.txt", "w"); // 2. open file
if(file_ptr !=NULL) // 3. test
{
printf("File chars.txt created.\n");
for(i=0 ; text[i] ; i++)
{
fputc( text[i], file_ptr); // 4. write text
}
fclose(file_ptr); // 5. close file
return 0;
}
else { printf("Unable to create file.\n"); return 1; }
}
|
| |
| | Page 3 of 7 | |
| < Previous | | Next > |
|
|