How it works...

We define a file pointer by the name fp. We will open the sequential file, whose name is supplied through the command-line argument, in write-only mode and set the fp file pointer to point at it. If the file cannot be opened in write-only mode, it might be because there are not enough permissions or disk space constraints. An error message will be displayed and the program will terminate.

If the file opens successfully in write-only mode, you will be prompted to enter the contents for the file. All the text you enter will be assigned to the str string variable, which is then written into the file. You should enter stop when you have completed entering the content into the file. Finally, we will close the file pointer.

Let's use GCC to compile the createtextfile.c program, as shown in the following statement:

D:CBook>gcc createtextfile.c -o createtextfile

If you get no errors or warnings, it means that the createtextfile.c program has been compiled into an executable file, createtextfile.exe. Let's run this executable file:

D:CBook>createtextfile textfile.txt
Enter content for the file
I am trying to create a sequential file. it is through C programming. It is very hot today
I have a cat. do you like animals? It might rain
Thank you. bye
stop

Voila! We've successfully created a sequential file and entered data in it.

Now let's move on to the next recipe!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset