Basics of Files in C Programming

The C programming library offers functions for making a new file, writing to that file, and reading data from any file. To bolster those basic file functions are a suite of file manipulation functions. They allow your programs to rename, copy, and delete files. The functions work on any file, not just those you create, so be careful!

How to rename a file in C programming

The rename() function is not only appropriately named but it’s also pretty simple to figure out:

x = rename(oldname,newname);

oldname is the name of a file already present; newname is the file’s new name. Both values can be immediate or variables. The return value is 0 upon success; -1 otherwise.

The rename() function is prototyped in the stdio.h header file.

The source code shown in Creating and Renaming a File creates a file named blorfus and then renames that file to wambooli.

Lines 9 through 15 create the file blorfus. The file is empty; nothing is written to it.

The rename() function at Line 17 renames the file. The return value is compared with -1 in Line 18 to see whether the operation was successful.

Exercise 1: Create a new program by using the source code shown in Creating and Renaming a File. Build and run.

How to copy a file in C programming

The C library features no function that duplicates a file. Instead, you have to craft your own: Write code that reads in a file, one chunk at a time, and then writes that chunk out to a duplicate file. That’s how files are copied.

Duplicate That File demonstrates how a file can be duplicated, or copied. The two files are specified in Lines 9 and 10. In fact, Line 9 uses the name of the Exercise file, the source code from Duplicate That File. The destination file, which contains the copy, is simply the same filename, but with a bak extension.

The copying work is done by the while loop at Line 16. One character is read by the fgetc() function, and it’s immediately copied to the destination by the fputc() function in Line 17. The loop keeps spinning until the EOF, or end-of-file, is encountered.

Exercise 2: Copy the source code form Duplicate That File into your editor. Save the file as ex2308.c, build, and run. You’ll need to use your computer operating system to view the resulting file in a folder window. Or you can view the results in a terminal or command prompt window.

How to delete a file in C programming

Programs delete files all the time, although the files are mostly temporary anyway. Back in the bad old days, many programmers complained about programs that didn’t “clean up their mess.” If your code creates temporary files, remember to remove them before the program quits. The way to do that is via the unlink() function.

Yes, the function is named unlink and not delete or remove or erase or whatever operating system command you’re otherwise used to. In Unix, the unlink command can be used in the terminal window to zap files, although the rm command is more popular.

The unlink() function requires the presence of the unistd.h header file, which you see at Line 3 in File Be Gone!