File Handling in C — How to Open, Close, and Write to Files (2024)

/ #C
File Handling in C — How to Open, Close, and Write to Files (1)

If you’ve written the C helloworld program before, you already know basic file I/O in C:

/* A simple hello world in C. */#include <stdlib.h>// Import IO functions.#include <stdio.h>int main() { // This printf is where all the file IO magic happens! // How exciting! printf("Hello, world!\n"); return EXIT_SUCCESS;}

File handling is one of the most important parts of programming. In C, we use a structure pointer of a file type to declare a file:

FILE *fp;

C provides a number of build-in function to perform basic file operations:

  • fopen() - create a new file or open a existing file
  • fclose() - close a file
  • getc() - reads a character from a file
  • putc() - writes a character to a file
  • fscanf() - reads a set of data from a file
  • fprintf() - writes a set of data to a file
  • getw() - reads a integer from a file
  • putw() - writes a integer to a file
  • fseek() - set the position to desire point
  • ftell() - gives current position in the file
  • rewind() - set the position to the beginning point

Opening a file

The fopen() function is used to create a file or open an existing file:

fp = fopen(const char filename,const char mode);

There are many modes for opening a file:

  • r - open a file in read mode
  • w - opens or create a text file in write mode
  • a - opens a file in append mode
  • r+ - opens a file in both read and write mode
  • a+ - opens a file in both read and write mode
  • w+ - opens a file in both read and write mode

Here’s an example of reading data from a file and writing to it:

#include<stdio.h>#include<conio.h>main(){FILE *fp;char ch;fp = fopen("hello.txt", "w");printf("Enter data");while( (ch = getchar()) != EOF) { putc(ch,fp);}fclose(fp);fp = fopen("hello.txt", "r");while( (ch = getc(fp)! = EOF) printf("%c",ch);fclose(fp);}

Now you might be thinking, "This just prints text to the screen. How is this file IO?”

The answer isn’t obvious at first, and needs some understanding about the UNIX system. In a UNIX system, everything is treated as a file, meaning you can read from and write to it.

This means that your printer can be abstracted as a file since all you do with a printer is write with it. It is also useful to think of these files as streams, since as you’ll see later, you can redirect them with the shell.

So how does this relate to helloworld and file IO?

When you call printf, you are really just writing to a special file called stdout, short for standard output. stdout represents the standard output as decided by your shell, which is usually the terminal. This explains why it printed to your screen.

There are two other streams (i.e. files) that are available to you with effort, stdin and stderr. stdin represents the standard input, which your shell usually attaches to the keyboard. stderr represents the standard error output, which your shell usually attaches to the terminal.

Rudimentary File IO, or How I Learned to Lay Pipes

Enough theory, let’s get down to business by writing some code! The easiest way to write to a file is to redirect the output stream using the output redirect tool, >.

If you want to append, you can use >>:

# This will output to the screen..../helloworld# ...but this will write to a file!./helloworld > hello.txt

The contents of hello.txt will, not surprisingly, be

Hello, world!

Say we have another program called greet, similar to helloworld, that greets you with a given name:

#include <stdio.h>#include <stdlib.h>int main() { // Initialize an array to hold the name. char name[20]; // Read a string and save it to name. scanf("%s", name); // Print the greeting. printf("Hello, %s!", name); return EXIT_SUCCESS;}

Instead of reading from the keyboard, we can redirect stdin to read from a file using the < tool:

# Write a file containing a name.echo Kamala > name.txt# This will read the name from the file and print out the greeting to the screen../greet < name.txt# ==> Hello, Kamala!# If you wanted to also write the greeting to a file, you could do so using ">".

Note: these redirection operators are in bash and similar shells.

The Real Deal

The above methods only worked for the most basic of cases. If you wanted to do bigger and better things, you will probably want to work with files from within C instead of through the shell.

To accomplish this, you will use a function called fopen. This function takes two string parameters, the first being the file name and the second being the mode.

The mode are basically permissions, so r for read, w for write, a for append. You can also combine them, so rw would mean you could read and write to the file. There are more modes, but these are the most commonly used.

After you have a FILE pointer, you can use basically the same IO commands you would’ve used, except that you have to prefix them with f and the first argument will be the file pointer. For example, printf’s file version is fprintf.

Here’s a program called greetings that reads a from a file containing a list of names and write the greetings to another file:

#include <stdio.h>#include <stdlib.h>int main() { // Create file pointers. FILE *names = fopen("names.txt", "r"); FILE *greet = fopen("greet.txt", "w"); // Check that everything is OK. if (!names || !greet) { fprintf(stderr, "File opening failed!\n"); return EXIT_FAILURE; } // Greetings time! char name[20]; // Basically keep on reading untill there's nothing left. while (fscanf(names, "%s\n", name) > 0) { fprintf(greet, "Hello, %s!\n", name); } // When reached the end, print a message to the terminal to inform the user. if (feof(names)) { printf("Greetings are done!\n"); } return EXIT_SUCCESS;}

Suppose names.txt contains the following:

KamalaLoganCarol

Then after running greetings the file greet.txt will contain:

Hello, Kamala!Hello, Logan!Hello, Carol!

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

If you read this far, thank the author to show them you care.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTIsem*nT

File Handling in C — How to Open, Close, and Write to Files (2024)
Top Articles
How to Invest in an IRA as a Beginner
Binance Coin (BNB) Price Prediction 2024, 2025, 2030 — Can BNB Hit $10000? - BTCC
Hairy Creator Collective
Stellaris Piracy Suppression
Baldur’s Gate 3 difficulty settings and options
Print With Me Discount Code
Union Corners Obgyn
2065660072
International Medical Insurance for Employers from Aetna - Global Health Plans for Expatriates & Business Travel Employees | Aetna
Seafarers: Working conditions, health assessments and training requirements - Your Europe
Rs3 Bring Leela To The Tomb
Blood Dk Primordial Stones
10 Overhyped Movies That Lived Up To Their Ridiculously High Expectations
spanien-reisemagazin: Pulpo Rezept
Google Reviews Molly Maid Tinley Park Il
Unity Webgl Car Tag
Rachaelrayshow Com Recipes
Chrome Hearts Schmuck und Uhren – 15 im Angebot bei 1stDibs
The Use of the BSRN Data as A Benchmark for the POWER Hourly DHI and DNI and In Validating Derived Hourly GTI
Daves Supermarket Weekly Ad
Honeywell V8043E1012 Wiring Diagram
Netherlands Toys, Games & Hobbies | ExpatINFO Holland
Nikon in 2022: What happened, why, and where next for the Big N?
Raiders Bane Enchantment
Sony X85K Vs Samsung Q70A
Tom Wages Stone Mountain
Ephesians 6 New Living Translation
159R Bus Schedule Pdf
Federal Express Drop Off Center Near Me
Dr. John J. Carroll - California Sports Psychology | LinkedIn
Caroline Cps.powerschool.com
Paul Mccombs Nashville
Munis Self Service Cumberland County
Feeling lucky? Top prizes still available for these Ohio Lottery scratch-off games
What Is The Best Center Build In 2K23
Moparts Com Forum
Copper Grooming Report
Cocaine Bear Showtimes Near Richland Cinemas
Kamado Joe 457 Manual
Rubmaps Boston Ma
Iowa State Map Campus
Nizhoni Massage Gun
Navy Female Prt Standards 30 34
Resortcom.
Norwegian Luna | Cruise Ship
Eufy Update Firmware
Chicago PD Season 12's Upton Replacement Is Already Great (Despite No One Official Yet)
Campers Inn Rv Of Hu
Linkedin.comnk
What is the Vintage Aesthetic | Aesthetics Wiki
Craigslist Greencastle
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 6525

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.