C – File Operations, Open, Write and Close Files

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/c-file-operations-open-write-close-files/
PINTEREST
PINTEREST
INSTAGRAM

C Programming Tutorial Index

  1. Introduction To Programming
  2. C Setup Development Environment
  3. C Hello World and Compile First Application
  4. C Syntax and Basics
  5. C Variables
  6. C Numeric Variable Types (Integer, Long, Float)
  7. C String Variables
  8. C Constants and Define Processor
  9. C Operators
  10. C If Else and Switch Case Conditionals
  11. C While Loops
  12. C For Loops
  13. C Arrays
  14. C Functions
  15. C Structure
  16. C Union
  17. C File Operations (Open, Read, Write)
  18. C Pointers
  19. C Type Casting
  20. C Header Files and Include

While learning C programming language one of the most exciting part is writing and reading a file. Because this operations creates something on the operating system we can see which is different from other examples. In this tutorial we will look different aspects of file operations.

stdio.h Library

As  we know C provides different type of features with libraries. Input and output related features are provided by the library named stdio.h . In order to run related file operations we should include this library like below. We generally put include like to the start of the code file like below.

Opening File

The first step to work with a file is opening it. Files can be opened by using fopen function. fopen function generally gets the filename and mode parameters.

fopen  function returns a handler back where we use FILE type variable for this.

Below we will create a file pointer named fp and open file named test.txt with w+ write and read mode .

Closing File

In previous part we have opened a file with fopen function. But the code provided there is not an efficient code because file handler do not closed which means file is not closed. Not closing a file can create performance or write problems. So after our operation is completed we should close the file with fclose function.

LEARN MORE  How To Use Psexec Tools To Run Commands and Get Shell Remote Windows Systems

and complete code will be like below.

Reading File

One of the fundamental step for file operation is reading a file. There are different ways and modes to read a file but in this step we simply read a line. We will put this in a while loop and read to the end of file. Because we will read file so we will use read mode while opening the file with fopen function.

We will provide the variable, str , we want to put grabbed string and the size to read which is 80 and the last one is the file pointer fp

And here fully working code where we use while to read line by line to the end of file. If end of file arrived the NULL value will be returned.

Writing File

In previous steps we have learned how to open and close file. But the ultimate goal is not opening and closing files. We generally read or write to file. There are different ways to write a file but in this tutorial we will simply put some line to the file. We will use fputs function by providing the string and file pointer like below.

LEARN MORE  Git - File Operations

We can see the whole working example below.

C Programming Tutorial Index

  1. Introduction To Programming
  2. C Setup Development Environment
  3. C Hello World and Compile First Application
  4. C Syntax and Basics
  5. C Variables
  6. C Numeric Variable Types (Integer, Long, Float)
  7. C String Variables
  8. C Constants and Define Processor
  9. C Operators
  10. C If Else and Switch Case Conditionals
  11. C While Loops
  12. C For Loops
  13. C Arrays
  14. C Functions
  15. C Structure
  16. C Union
  17. C File Operations (Open, Read, Write)
  18. C Pointers
  19. C Type Casting
  20. C Header Files and Include
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/c-file-operations-open-write-close-files/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)