Skip to content

Commit

Permalink
13 | 1-7 | sharafat
Browse files Browse the repository at this point in the history
  • Loading branch information
SharafatKarim committed Aug 17, 2023
1 parent 53de8eb commit 57b87cc
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 0 deletions.
2 changes: 2 additions & 0 deletions solutions/sharafat/13/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DATA*
*.txt
14 changes: 14 additions & 0 deletions solutions/sharafat/13/1.c
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;
}
38 changes: 38 additions & 0 deletions solutions/sharafat/13/2.c
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;
}
30 changes: 30 additions & 0 deletions solutions/sharafat/13/3.c
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;
}
20 changes: 20 additions & 0 deletions solutions/sharafat/13/4.c
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;
}
23 changes: 23 additions & 0 deletions solutions/sharafat/13/5.c
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;
}
18 changes: 18 additions & 0 deletions solutions/sharafat/13/6.c
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;
}
67 changes: 67 additions & 0 deletions solutions/sharafat/13/7.c
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;
}

0 comments on commit 57b87cc

Please sign in to comment.