From the course: C: Data Structures, Pointers, and File Systems
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Writing to a file - C Tutorial
From the course: C: Data Structures, Pointers, and File Systems
Writing to a file
- [Instructor] To write to a file, you use the file open function, fopen, just as you would for opening a file to read, but instead, the file is open for writing, or creation. The first argument is a filename string. The second argument is the mode, which is w for writing or a for appending to a file that already exists. When a is specified and the file doesn't exist, the file is created and opened for writing. Once open, you use file versions of the standard output functions to send data to a file. Here are some of the functions which operate like their standard output counterparts, though they have a file handle argument. And when you're done, you use the fclose function to close the file. Do not forget this step. One of the duties of the fclose function is to finish writing data to the file, as some of the information may sit in a buffer before it's actually written. This code outputs a single line of text to a file, output.txt which it creates. The w mode shown in line eight…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
Reading from a file3m 25s
-
Writing to a file3m 24s
-
Working with raw data2m 53s
-
Using random file access2m 47s
-
Manipulating files2m 57s
-
Exploring the path2m 8s
-
Reading a directory2m 53s
-
Getting file information2m 46s
-
Using command line arguments2m 44s
-
Calling the operating system2m 46s
-
Challenge: Read and store filenames1m 6s
-
Solution: Read and store filenames2m 52s
-
-
-