With this simple C program, we will be able to create a sequential file of numbers and then enter some data into it.
If you haven’t seen these before, the program is basically a text editor that uses C. You enter your data by typing it into the program, then pressing “Enter” to send the information to the out file program.
In C, it is easy to create a sequential file. But what is a sequential file? It’s a text file with the following structure: . This is simple. To create a sequential file, open a text editor and write the following line of code:
#include
#include
#include
void main (int argc, char* argv[])
{
char str[255];
FILE *fp;
fp = fopen (argv[1], "w");
if (fp == NULL) {
perror ("An error occurred in creating the file\n");
exit(1);
}
printf("Enter content for the file\n");
gets(str);
while(strcmp(str, "stop") !=0){
fputs(str,fp);
gets(str);
}
fclose(fp);
}
After creating the program, run it. You’ll see that the program asks you to enter your first name, last name, and date of birth. Then, the program prints out the information you entered.
We can define a file pointer by using the fp. We will open the sequential file, whose name is supplied through the command-line argument, in read-only mode and set the fp file pointer to point at it. If the file cannot be opened in read-only mode, it might be because there aren’t enough permissions or disk space requirements. An error message will be displayed and the program will terminate.