-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53de8eb
commit 57b87cc
Showing
8 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DATA* | ||
*.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include <stdio.h> | ||
|
||
int main() | ||
{ | ||
FILE *fp1, *fp2; | ||
char ch; | ||
fp1 = fopen("file1.txt", "r"); | ||
fp2 = fopen("file2.txt", "w"); | ||
while ((ch = fgetc(fp1)) != EOF) | ||
{ | ||
fputc(ch, fp2); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <stdio.h> | ||
|
||
int main() | ||
{ | ||
FILE *fp1, *fp2, *fp3; | ||
int num1, num2; | ||
fp1 = fopen("DATA1", "r"); | ||
fp2 = fopen("DATA2", "r"); | ||
fp3 = fopen("DATA", "w"); | ||
|
||
fscanf(fp1, "%d", &num1); | ||
fscanf(fp2, "%d", &num2); | ||
|
||
while (!feof(fp1) && !feof(fp2)) | ||
{ | ||
if (num1 < num2) | ||
{ | ||
fprintf(fp3, "%d\n", num1); | ||
fscanf(fp1, "%d", &num1); | ||
} | ||
else | ||
{ | ||
fprintf(fp3, "%d\n", num2); | ||
fscanf(fp2, "%d", &num2); | ||
} | ||
} | ||
while (!feof(fp1)) | ||
{ | ||
fprintf(fp3, "%d\n", num1); | ||
fscanf(fp1, "%d", &num1); | ||
} | ||
while (!feof(fp2)) | ||
{ | ||
fprintf(fp3, "%d\n", num2); | ||
fscanf(fp2, "%d", &num2); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <stdio.h> | ||
|
||
int main() | ||
{ | ||
FILE *fp1, *fp2; | ||
int num1, num2; | ||
fp1 = fopen("DATA1", "r"); | ||
fp2 = fopen("DATA2", "r"); | ||
|
||
fscanf(fp1, "%d", &num1); | ||
fscanf(fp2, "%d", &num2); | ||
|
||
while (!feof(fp1) || !feof(fp2)) | ||
{ | ||
if (num1 != num2) | ||
{ | ||
printf("Not same"); | ||
return 1; | ||
} | ||
fscanf(fp1, "%d", &num1); | ||
fscanf(fp2, "%d", &num2); | ||
} | ||
if (feof(fp1) || feof(fp2)) | ||
{ | ||
printf("Not same"); | ||
return 1; | ||
} | ||
printf("Same"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <stdio.h> | ||
|
||
int main() | ||
{ | ||
FILE *fp1, *fp2; | ||
int num; | ||
fp1 = fopen("DATA1", "r"); | ||
fp2 = fopen("DATA2", "a"); | ||
|
||
// fseek(fp1, 0, 2); | ||
fseek(fp2, 0, 2); | ||
|
||
fscanf( fp1, "%d", &num); | ||
while ( !feof(fp1) ) | ||
{ | ||
fprintf( fp2, "%d\n", num); | ||
fscanf( fp1, "%d", &num); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <stdio.h> | ||
|
||
int main() | ||
{ | ||
FILE *fp1, *fp2; | ||
int num, sum = 0; | ||
fp1 = fopen("DATA", "r"); | ||
fp2 = fopen("DATA", "a"); | ||
|
||
fscanf(fp1, "%d", &num); | ||
while (!feof(fp1)) | ||
{ | ||
printf("%d + ", num); | ||
sum += num; | ||
fscanf(fp1, "%d", &num); | ||
} | ||
printf("= %d\n", sum); | ||
|
||
fseek(fp2, 0, 2); | ||
fprintf(fp2, "%d\n", sum); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <stdio.h> | ||
|
||
int main() | ||
{ | ||
FILE *fp1, *fp2; | ||
char ch, exclude; | ||
fp1 = fopen("file1.txt", "r"); | ||
fp2 = fopen("file2.txt", "w"); | ||
printf("What to exclude? "); | ||
scanf("%c", &exclude); | ||
while ((ch = fgetc(fp1)) != EOF) | ||
{ | ||
if (ch == exclude) | ||
continue; | ||
fputc(ch, fp2); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <stdio.h> | ||
#include <math.h> | ||
|
||
int main() | ||
{ | ||
FILE *fp; | ||
char file_name[100]; | ||
int offset; | ||
|
||
printf("Enter file name: "); | ||
scanf("%s", file_name); | ||
printf("Enter offset: "); | ||
scanf("%d", &offset); | ||
|
||
fp = fopen(file_name, "r"); | ||
|
||
if (fp == NULL) | ||
{ | ||
printf("File not found\n"); | ||
return 1; | ||
} | ||
|
||
char ch; | ||
int i = 0; | ||
if (offset >= 0) | ||
{ | ||
while ((ch = fgetc(fp)) != EOF) | ||
{ | ||
if (i >= offset) | ||
{ | ||
break; | ||
} | ||
printf("%c", ch); | ||
if (ch == '\n') | ||
{ | ||
i++; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
int total_lines = 0; | ||
while ((ch = fgetc(fp)) != EOF) | ||
{ | ||
if (ch == '\n') | ||
{ | ||
total_lines++; | ||
} | ||
} | ||
// printf("Total lines: %d\n", total_lines); | ||
rewind(fp); | ||
i = 0; | ||
while ((ch = fgetc(fp)) != EOF) | ||
{ | ||
if (total_lines + offset <= i) | ||
{ | ||
printf("%c", ch); | ||
// break; | ||
} | ||
if (ch == '\n') | ||
{ | ||
i++; | ||
} | ||
} | ||
} | ||
return 0; | ||
} |