1/**
2 * C program to create a file and write data into file.
3 */
4
5#include <stdio.h>
6#include <stdlib.h>
7
8#define DATA_SIZE 1000
9
10int main()
11{
12 /* Variable to store user content */
13 char data[DATA_SIZE];
14
15 /* File pointer to hold reference to our file */
16 FILE * fPtr;
17
18
19 /*
20 * Open file in w (write) mode.
21 * "data/file1.txt" is complete path to create file
22 */
23 fPtr = fopen("data/file1.txt", "w");
24
25
26 /* fopen() return NULL if last operation was unsuccessful */
27 if(fPtr == NULL)
28 {
29 /* File not created hence exit */
30 printf("Unable to create file.\n");
31 exit(EXIT_FAILURE);
32 }
33
34
35 /* Input contents from user to store in file */
36 printf("Enter contents to store in file : \n");
37 fgets(data, DATA_SIZE, stdin);
38
39
40 /* Write data to file */
41 fputs(data, fPtr);
42
43
44 /* Close file to save file data */
45 fclose(fPtr);
46
47
48 /* Success message */
49 printf("File created and saved successfully. :) \n");
50
51
52 return 0;
53}