Write a C program to split a file to multiple files.
#include <stdio.h>
#include <string.h>
#define MAX 256
int main() {
int i = 0, line = 0;
FILE *fp1, *fp2, *fp3;
char file1[MAX], file2[MAX], file3[MAX], str[MAX];
/* get the name without file format(.txt) from user */
printf("Enter your input file name:");
scanf("%s", file1);
/* form the output file names */
sprintf(file2, "%s_part_%d.txt", file1, 1);
sprintf(file3, "%s_part_%d.txt", file1, 2);
strcat(file1, ".txt");
/* open the input file in read mode */
fp1 = fopen(file1, "r");
/* error handling */
if (!fp1) {
printf("Unable to open input file in read mode!!\n");
return 0;
}
/* open the output file in write mode */
fp2 = fopen(file2, "w");
/* error handling */
if (!fp2) {
printf("Unable to open output file in write mode\n");
fclose(fp1);
return 0;
}
#include <string.h>
#define MAX 256
int main() {
int i = 0, line = 0;
FILE *fp1, *fp2, *fp3;
char file1[MAX], file2[MAX], file3[MAX], str[MAX];
/* get the name without file format(.txt) from user */
printf("Enter your input file name:");
scanf("%s", file1);
/* form the output file names */
sprintf(file2, "%s_part_%d.txt", file1, 1);
sprintf(file3, "%s_part_%d.txt", file1, 2);
strcat(file1, ".txt");
/* open the input file in read mode */
fp1 = fopen(file1, "r");
/* error handling */
if (!fp1) {
printf("Unable to open input file in read mode!!\n");
return 0;
}
/* open the output file in write mode */
fp2 = fopen(file2, "w");
/* error handling */
if (!fp2) {
printf("Unable to open output file in write mode\n");
fclose(fp1);
return 0;
}
/* open the output file in write mode */
fp3 = fopen(file3, "w");
/* error handling */
if (!fp3) {
printf("Unable to open output file2 in write mode\n");
fclose(fp1);
fclose(fp2);
return 0;
}
/* calculate the number of lines in input file */
while (!feof(fp1)) {
fgets(str, MAX, fp1);
if (!feof(fp1))
line++;
}
/* moving the file pointer to the start of file */
rewind(fp1);
/* writing first half of data to first output file */
while (i < line/2) {
fgets(str, MAX, fp1);
if (!feof(fp1)) {
fputs(str, fp2);
}
i++;
}
/* writing second half to second output file */
while (!feof(fp1)) {
fgets(str, MAX, fp1);
if (!feof(fp1)) {
fputs(str, fp3);
}
}
/* close all opened files */
fclose(fp1);
fclose(fp2);
fclose(fp3);
/* remove the input file */
remove(file1);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ cat data.txt
abcdefg1234
56789hijklm
123456abcde
fghijk12345
jp@jp-VirtualBox:~/$ ls
a.out data.txt
jp@jp-VirtualBox:~/$ ./a.out
Enter your input file name:data
jp@jp-VirtualBox:~/$ ls
a.out data_part_1.txt data_part_2.txt
jp@jp-VirtualBox:~/$ cat data_part_1.txt
abcdefg1234
56789hijklm
jp@jp-VirtualBox:~/$ cat data_part_2.txt
123456abcde
fghijk12345
abcdefg1234
56789hijklm
123456abcde
fghijk12345
jp@jp-VirtualBox:~/$ ls
a.out data.txt
jp@jp-VirtualBox:~/$ ./a.out
Enter your input file name:data
jp@jp-VirtualBox:~/$ ls
a.out data_part_1.txt data_part_2.txt
jp@jp-VirtualBox:~/$ cat data_part_1.txt
abcdefg1234
56789hijklm
jp@jp-VirtualBox:~/$ cat data_part_2.txt
123456abcde
fghijk12345
SEE ALSO
- c program to write a string into a file
- c program to read numbers from a file and write even, odd and prime numbers in separate files
- c program to create a file and store "hello world" in it
- c program to read the contents of the given file
- c program to delete all blank lines in a file
- c program to check whether a directory exists or not
- c program to check whether a file exists or not
- c program to copy a file from one location to another
- c program to create, read, edit and close a file
- c program to create a file with input content
- c program to read a file line by line
- c program to compare two files character by character
- c program to concatenate two files
- c program to convert lowercase characters in a file to uppercase
- c program to convert uppercase characters in a file to lowercase
- c program to list all files in a directory
- c program to list all files in a directory recursively
- c program to append data into a file
- c program to count number of lines in a file
- c program to delete a file or directory
- c program to print the source code of itself as output
- c program to convert the contents in a file from lowercase to uppercase and vice versa
- c program to merge two files
- c program to move a file to different location
- c program to replace articles with space in a text file
- c program to print the words in a file starting with the given character
- c program to delete given word in a file
- c program to replace a word in a file
- c program to merge alternate lines from two files
- c program to remove numbers in a file
- c program to check end of file
- c program to compare two files line by line
- c program to delete specific line from a file
- c program to replace specific line in a file
- c program to take input from a file
- c program to delete a record from a file
- c program to find the number of character, words and lines in a file
- c program to sort characters in each words of a file
- c program to convert text file to binary
- c program to split a file to multiple files
- c program to find the file type, permission, size and last modification date of the given file
- c program to encrypt and decrypt contents of a file
- c program to eliminate comments from a file
Nice article post ! thanks for sharing this news with us !
ReplyDeleteWord Files Splitter Software