Explanation
The program demonstrates file handling in C by saving the values entered by the user (name and age) to a file.
Header
The code begins with including the standard input-output header file and the string header file using #include <stdio.h>
and #include <string.h>
.
Variable Declaration
The main()
function is defined, and variables are declared:
char name[100];
: Array to hold the user's full name.int age;
: Variable to hold the user's age.
Program Description
The program prints a description message for the user to understand its functionality.
User Input
The user is prompted to enter their full name and age:
printf("Enter your full name: ");
: Prompts the user to enter their full name.fgets(name, sizeof(name), stdin);
: Reads the full name including spaces and stores it in the arrayname
.printf("Enter your age: ");
: Prompts the user to enter their age.scanf("%d", &age);
: Reads the age entered by the user and stores it inage
.name[strcspn(name, "\n")] = 0;
: Removes the newline character from the end of the stringname
.
File Handling
The program demonstrates file handling by opening a file, writing data to it, and then closing it:
FILE *fp = fopen("data.txt", "a+");
: Opens a file nameddata.txt
for appending. If the file does not exist, it will be created.fprintf(fp, "Name: %s\n", name);
: Writes the user's name to the file.fprintf(fp, "Age: %d\n", age);
: Writes the user's age to the file.fclose(fp);
: Closes the file.
Output Result
The program prints a message indicating that the data has been written to the file:
printf("Data written to file data.txt");
: Prints a message indicating successful writing of data to the file.
Program End
The program ends with return 0;
to indicate successful execution.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
int main()
{
printf("\n\n Program to demonstrate the file handling by saving the values entered by the user. \n\n");
char name[100];
int age;
printf("Enter your full name: ");
fgets(name, sizeof(name), stdin); // Read the full name, including space
printf("Enter your age: ");
scanf("%d", &age);
name[strcspn(name, "\n")] = 0; // remove the newline character from the end of the string
// Open a file for writing
FILE *fp = fopen("data.txt", "a+");
// Write the input data to the file
fprintf(fp, "Name: %s", name); // The name already includes a newline character
fprintf(fp, "Age: %d\n", age);
// Close the file
fclose(fp);
printf("Data written to file data.txt");
return 0;
}
Output
Program to demonstrate the file handling by saving the values entered by the user.
Enter your full name: dmj.one Pvt. Ltd.
Enter your age: 11
Data written to file data.txt