-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject_first.C
More file actions
130 lines (110 loc) · 2.75 KB
/
Project_first.C
File metadata and controls
130 lines (110 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int Merge();
int Split();
int main()
{
int c;
char ch;
printf("\n\t\tEnter your Choice");
printf("\n\t1.Split text file");
printf("\n\t2.Merge the splitted file");
printf("\n\t");
scanf("%d",&c);
fflush(stdin);
switch(c)
{
case 1:
Split();
break;
case 2:
Merge();
}
if(c == 1) {
printf("\n\tDo you want to merge the file [Y/N]");
scanf("%c",&ch);
if(ch == 'Y' || ch == 'y') Merge();
else printf("\n\tFile merged successfully");
}
return 0;
}
int Split()
{
FILE *fp;
FILE fp1;
char str1[10000];
char str2[2500];
char fname[20];
int i,j,len,a;
printf("\n\tEnter the file name with (.txt) extension \"eg.file_name.txt\" : ");
scanf("%[^\n]s",fname);
fp=fopen(fname,"r");
if (fp==NULL)
{
printf("\n\tFile not found");
return 0 ;
}
fscanf(fp,"%[]s",str1); //Reading whole data from file.
fflush(stdin);
len = strlen(str1); //Calculating original string length.
if(len%4==0) a=len/4;
else a=((len/4)+(len%4)); //Dividing length into four parts.
j=0;
for(i=0;i<a;i++,j++) str2[j] = str1[i];
str2[j] = '\0';
fp = fopen("file1.txt","w");
fprintf(fp,"%s",str2);
fclose(fp);
for(i=0;i<a;i++,j++) str2[i] = str1[j];
str2[j] = '\0';
fp = fopen("file2.txt","w");
fprintf(fp,"%s",str2);
fclose(fp);
for(i=0;i<a;i++,j++) str2[i] = str1[j];
str2[j] = '\0';
fp = fopen("file3.txt","w");
fprintf(fp,"%s",str2);
fclose(fp);
for(i=0;i<a;i++,j++) str2[i] = str1[j];
str2[j] = '\0';
fp = fopen("file4.txt","w");
fprintf(fp,"%s",str2);
fclose(fp);
return 0;
}
int Merge()
{
char str1[10000], str2[2500];
FILE *fp;
int i,j=0,l;
printf("\n\tPress Enter to merge the file");
getchar();
fp=fopen("file1.txt","r");
fscanf(fp,"%[]s",str2);
l=strlen(str2);
for(i=0;i<l;i++,j++) str1[j] = str2[i];
fclose(fp);
fp=fopen("file2.txt","r");
fscanf(fp,"%[]s",str2);
for(i=0;i<l;i++,j++) str1[j] = str2[i];
fclose(fp);
fp=fopen("file3.txt","r");
fscanf(fp,"%[]s",str2);
for(i=0;i<l;i++,j++) str1[j] = str2[i];
fclose(fp);
fp = fopen("file4.txt","r");
fscanf(fp,"%[]s",str2);
for(i=0;i<l;i++,j++) str1[j] = str2[i];
fclose(fp);
str1[j] = '\0';
fp = fopen("MergeNew.txt","w");
if(fp==NULL) {
printf("\n\tUnable to open the file");
return 0;
}
fprintf(fp,"%s",str1);
fclose(fp);
printf("\n\tFile Successfully Merged");
return 0;
}