How to write to a file in C Programming

/* 
* Program that writes to a text file
* From the book: Visual Quickstart Guide C Programming
*
* The record_weather application takes three user input values
* (a date, the day's high tempature, and the day's low tempature)
* and writes them to a text file.
*/



#include <stdio.h>

/*
* Set the file path and name as a C preprocessor macro.
*/

#define THEFILE "weather.txt"

/* 
* Begin main function.
*/

int main(void) {

/*
* Create a file pointer.
*/

FILE *fp;

/*
* Define the required variables. The first two variables are
* integers that will store the entered high and low tempatures.
* The data variable is a character array to store dates in the 
* format. YYYY-MM-DD.
*/

int high, low;

char date[11];

/*
* Open the file for appended writing. By using an a, each new
* record will be appended to the end of the file, rather than 
* wipping out any existing data.
*/

fp = fopen(THEFILE, "a");

/*
* Start a condional bases on the file pointer and prompt the user. 
* If the file can be opened, then the user will be promplted for
* the required information.
*/

if (fp != NULL) {

printf("Enter a date, the high tempature, and the low tempature.\n(YYYY-MM-DD ## ###): ");

/*
* Handle the keyed input. Using the scanf() function, the 
* application attempts to read one string (of up to 10
* chracters long) and two integers from the standard input. 
* The string will be stored in date, which as a character
* array, can be listed directly. The two integer values go
* into high and low, which as numbers must be referenced in
* their address form (&). If scanf() successfully read in
* three items, the user entered the information properly,
* so the conditional checks if the returned value is equal 
* to 3. 
*/

if (scanf ("%10s %d %d", date, &high, &low) == 3) {

/*
* Write the data  the text file. The fprintf() function
* will write the values to the text file. The formatting
* is simple: seperate eac value by a space and ending with
* a newline.
*/

fprintf(fp, "%s %d %d\n", date, high, low);

/*
* Complete the scanf() conditional. The first message
* completes the condtional if scanf() worked, 
* indicating that the data was written to the file. 
* If scanf() did not return a value of 3, the input was
* not of the proper format and the second message is
* printed on the screen.
*/

printf ("The data has been written.\n");

/*
* Complete the fp conditional. If the application could
* not open the file for writing, there's no reason to
* continue. Because functions always stop running after
* a return staion this effectively stops the application. 
*

} else {

printf("The data was not in the promper format.\n");

}

} else {

printf("The file cound not be opened.\n");

return 1;

}

/*
* Close the file.
*/

if (fclose(fp) != 0) {

printf ("The file count not be close.\n");

} 

/*
* Complete main function
*/

getchar();
getchar();
return 0;

}

Leave a Reply

Your email address will not be published. Required fields are marked *