C Tutorial – Splitting a Text File into Multiple Files
A new year (have a good new year by the way) a new C language tutorial. We start the year with a C language tutorial where we will look at how to split a text file into multiple files. In previous tutorials we already looked at reading and writing text files in C examples and searching in text files, so in this tutorial we will build on these tutorials.
Splitting Text Files
Let’s first look at the source code of our split text program, before we look at what we are doing in the example:
#include <stdio.h>
int main() {
FILE *ptr_readfile;
FILE *ptr_writefile;
char line [128]; /* or some other suitable maximum line size */
char fileoutputname[15];
int filecounter=1, linecounter=1;
ptr_readfile = fopen("test.txt","r");
if (!ptr_readfile)
return 1;
sprintf(fileoutputname, "file_part%d", filecounter);
ptr_writefile = fopen(fileoutputname, "w");
while (fgets(line, sizeof line, ptr_readfile)!=NULL) {
if (linecounter == 5) {
fclose(ptr_writefile);
linecounter = 1;
filecounter++;
sprintf(fileoutputname, "file_part%d", filecounter);
ptr_writefile = fopen(fileoutputname, "w");
if (!ptr_writefile)
return 1;
}
fprintf(ptr_writefile,"%s\n", line);
linecounter++;
}
fclose(ptr_readfile);
return 0;
}
The example starts by including stdio.h header file. In the main() function we declare two file-pointers, one for reading and one for writing. Then a buffer is created with a maximum line size of 128 characters (or change it to any other suitable maximum line size). An array of characters is created to hold the file output name of the files that are split and there are some counters created (a filecounter and a linecounter).
After the declaration of some variable we need, we start by open a file for reading. In the if statement we check if the open went correctly.
In the next statements we create an output file name by adding the value of filecounter to the string file_part, so you get file_part1, file_part2 and so on. Then we open the first output file for writing in the next statement.
Now that we have done all the initializations, we can take a look at the hard of program, the while loop. The while loop will run until there are no lines in the source file. The fgets function get those line (one by one) from the source file. In the if statement we check for the number of lines we have handled, if it equals 5 then the block of code is handled, otherwise the lines are written to the output file. If the if statement is true we want to switch to a new output file.
In the if statement we first close our output file and we reset the line counter. We then do a plus one of the file counter. Then an new output file name is created in the sprintf statement. We open the new file for writing and do some simple error checking in the if statement. As stated before, there will be files created until there are no more lines in the input file.
NOTE: don’t add to much lines to your test.txt input file, otherwise number of split files can get very large! You’ve be warned ;-).
Cleaning Up the Source Code
Last thing we need to clean up our c splitting files example, by removing the double parts of the program and moving them to a function we can call. First take a look at the result of this cleanup action:
#include <stdio.h>
FILE *openforwrite(int filecounter) {
char fileoutputname[15];
sprintf(fileoutputname, "file_part%d", filecounter);
return fopen(fileoutputname, "w");
}
int main() {
FILE *ptr_readfile;
FILE *ptr_writefile;
char line [128]; /* or some other suitable maximum line size */
int filecounter=1, linecounter=1;
ptr_readfile = fopen("test.txt","r");
if (!ptr_readfile)
return 1;
ptr_writefile = openforwrite(filecounter);
while (fgets(line, sizeof line, ptr_readfile)!=NULL) {
if (linecounter == 5) {
fclose(ptr_writefile);
linecounter = 1;
filecounter++;
ptr_writefile = openforwrite(filecounter);
if (!ptr_writefile)
return 1;
}
fprintf(ptr_writefile,"%s\n", line);
linecounter++;
}
fclose(ptr_readfile);
return 0;
}
If you compare the first source code example with the second example, you’ll see that the only thing that we’ve done is moving the creation and opening of the output file to a function. Let’s take a look at the function openforwrite().
The function openforwrite() takes an integer (filecounter) and will return a file pointer to an open file with a filename that was created in the sprintf statement.
The rest of the program is almost the same as the description of the previous example. Only the two calls to openforwrite() are added to replace the sprintf, open file and if control statements.
That’s all for this C tutorials, until next time!
Hi,
great tutorials. been working them through. I’ve found a little bug in this tutorial.
in the part ‘sprintf(fileoutputname, “file_part%d”, filecounter)’ it would be more convenient to write “file_part%d.txt” so then after the program runs, there would be .txt files.
but great tutorial otherwise. keep up the good work
@paplaukias: yes, that is possible to write “file_part%d.txt” instead of “file_part%d” (and especially true on windows systems). But on Unix or Linux systems usually we don’t use extensions (in this case I created the source code on a Red Hat system). Both are fine, so use which ever is more clear to you. Good luck!.
Thanks a lot for your tutorials.
I used to work with C when I was at university. Some years passed and I need to make a program again (find a string in a line, remove unwanted words and put it on file). I’m spending this weekend trying to remember some C stuff. Tutorials here are really well explained and helped me to get my initial code better.
Hi i m beginner in c language, i want code for one program…please help
WAP to read a string from text file which contains:-
place=usa
place=japan
place=canada
bird=parrot
bird=peacock
———————————————
In the output we should get like
place=usa,japan,canada
bird=parrot,peacock