Skip to content

Commit

Permalink
Exerc 7_6.
Browse files Browse the repository at this point in the history
  • Loading branch information
rsm-lisper committed Jul 25, 2024
1 parent 6169a2f commit 841ad94
Show file tree
Hide file tree
Showing 22 changed files with 263 additions and 0 deletions.
3 changes: 3 additions & 0 deletions chapter_7.input_output/7_6.fcompare/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BINARY=fcompare

include ../../Exercise.mk
57 changes: 57 additions & 0 deletions chapter_7.input_output/7_6.fcompare/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# include <stdio.h>
# include <stdlib.h>
# include <string.h>

/* file_open: open a file, and handle errors */
FILE *file_open (char *name, char *mode, char *prog)
{
FILE *fp;
if ((fp = fopen(name, mode)) == NULL) {
fprintf(stderr, "%s ERROR! Can't open %s.\n", prog, name);
exit(1);
}
return fp;
}

/* fgetline: read a line from a file, return length */
int fgetline (FILE *fp, char *line, int max)
{
return
fgets(line, max, fp) == NULL ?
0 : strlen(line);
}

# define MAXLINE 1024

int main (int argc, char **argv)
{
FILE *fp0, *fp1;
char line0[MAXLINE], line1[MAXLINE];
int len0, len1, n;

if (argc != 3) {
printf("%s syntax: <file-a> <file-b>\n", argv[0]);
return 0;
}
fp0 = file_open(argv[1], "r", argv[0]);
fp1 = file_open(argv[2], "r", argv[0]);

n = -1;
do {
n++;
len0 = fgetline(fp0, line0, MAXLINE);
len1 = fgetline(fp1, line1, MAXLINE);
} while (len0 > 0 && len1 > 0 && len0 == len1 && strcmp(line0, line1) == 0);

if (len0 != len1 || strcmp(line0, line1) != 0)
printf("Files differ, line %d:\n"
"%s: %s\n"
"%s: %s\n",
n,
argv[1], len0 == 0 ? "[EOF]" : line0,
argv[2], len1 == 0 ? "[EOF]" : line1);

fclose(fp0);
fclose(fp1);
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0_file0.c 0_file1.c 0_file2.c
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./fcompare syntax: <file-a> <file-b>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1_file0.c 1_file1.c
1 change: 1 addition & 0 deletions chapter_7.input_output/7_6.fcompare/tests/1_err_fopen.terr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./fcompare ERROR! Can't open 1_file0.c.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions chapter_7.input_output/7_6.fcompare/tests/2_same.targs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main.c main.c
Empty file.
Empty file.
1 change: 1 addition & 0 deletions chapter_7.input_output/7_6.fcompare/tests/3_diff.targs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/3_file0.c tests/3_file1.c
Empty file.
5 changes: 5 additions & 0 deletions chapter_7.input_output/7_6.fcompare/tests/3_diff.tout
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Files differ, line 25:
tests/3_file0.c: int main (int argc, char **argv)

tests/3_file1.c: int main (int argc, char **argv)

57 changes: 57 additions & 0 deletions chapter_7.input_output/7_6.fcompare/tests/3_file0.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# include <stdio.h>
# include <stdlib.h>
# include <string.h>

/* file_open: open a file, and handle errors */
FILE *file_open (char *name, char *mode, char *prog)
{
FILE *fp;
if ((fp = fopen(name, mode)) == NULL) {
fprintf(stderr, "%s ERROR! Can't open %s.\n", prog, name);
exit(1);
}
return fp;
}

/* fgetline: read a line from a file, return length */
int fgetline (FILE *fp, char *line, int max)
{
return
fgets(line, max, fp) == NULL ?
0 : strlen(line);
}

# define MAXLINE 1024

int main (int argc, char **argv)
{
FILE *fp0, *fp1;
char line0[MAXLINE], line1[MAXLINE];
int len0, len1, n;

if (argc != 3) {
printf("%s syntax: <file-a> <file-b>\n", argv[0]);
return 0;
}
fp0 = file_open(argv[1], "r", argv[0]);
fp1 = file_open(argv[2], "r", argv[0]);

n = -1;
do {
n++;
len0 = fgetline(fp0, line0, MAXLINE);
len1 = fgetline(fp1, line1, MAXLINE);
} while (len0 > 0 && len1 > 0 && len0 == len1 && strcmp(line0, line1) == 0);

if (len0 != len1 || strcmp(line0, line1) != 0)
printf("Files differ, line %d:\n"
"%s: %s\n"
"%s: %s\n",
n,
argv[1], len0 == 0 ? "[EOF]" : line0,
argv[2], len1 == 0 ? "[EOF]" : line1);

fclose(fp0);
fclose(fp1);
return 0;
}
57 changes: 57 additions & 0 deletions chapter_7.input_output/7_6.fcompare/tests/3_file1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# include <stdio.h>
# include <stdlib.h>
# include <string.h>

/* file_open: open a file, and handle errors */
FILE *file_open (char *name, char *mode, char *prog)
{
FILE *fp;
if ((fp = fopen(name, mode)) == NULL) {
fprintf(stderr, "%s ERROR! Can't open %s.\n", prog, name);
exit(1);
}
return fp;
}

/* fgetline: read a line from a file, return length */
int fgetline (FILE *fp, char *line, int max)
{
return
fgets(line, max, fp) == NULL ?
0 : strlen(line);
}

# define MAXLINE 1024

int main (int argc, char **argv)
{
FILE *fp0, *fp1;
char line0[MAXLINE], line1[MAXLINE];
int len0, len1, n;

if (argc != 3) {
printf("%s syntax: <file-a> <file-b>\n", argv[0]);
return 0;
}
fp0 = file_open(argv[1], "r", argv[0]);
fp1 = file_open(argv[2], "r", argv[0]);

n = -1;
do {
n++;
len0 = fgetline(fp0, line0, MAXLINE);
len1 = fgetline(fp1, line1, MAXLINE);
} while (len0 > 0 && len1 > 0 && len0 == len1 && strcmp(line0, line1) == 0);

if (len0 != len1 || strcmp(line0, line1) != 0)
printf("Files differ, line %d:\n"
"%s: %s\n"
"%s: %s\n",
n,
argv[1], len0 == 0 ? "[EOF]" : line0,
argv[2], len1 == 0 ? "[EOF]" : line1);

fclose(fp0);
fclose(fp1);
return 0;
}
1 change: 1 addition & 0 deletions chapter_7.input_output/7_6.fcompare/tests/4_diff_eof.targs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/4_file0.c tests/4_file1.c
Empty file.
4 changes: 4 additions & 0 deletions chapter_7.input_output/7_6.fcompare/tests/4_diff_eof.tout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Files differ, line 16:
tests/4_file0.c: int fgetline (FILE *fp, char *line, int max)

tests/4_file1.c: [EOF]
57 changes: 57 additions & 0 deletions chapter_7.input_output/7_6.fcompare/tests/4_file0.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# include <stdio.h>
# include <stdlib.h>
# include <string.h>

/* file_open: open a file, and handle errors */
FILE *file_open (char *name, char *mode, char *prog)
{
FILE *fp;
if ((fp = fopen(name, mode)) == NULL) {
fprintf(stderr, "%s ERROR! Can't open %s.\n", prog, name);
exit(1);
}
return fp;
}

/* fgetline: read a line from a file, return length */
int fgetline (FILE *fp, char *line, int max)
{
return
fgets(line, max, fp) == NULL ?
0 : strlen(line);
}

# define MAXLINE 1024

int main (int argc, char **argv)
{
FILE *fp0, *fp1;
char line0[MAXLINE], line1[MAXLINE];
int len0, len1, n;

if (argc != 3) {
printf("%s syntax: <file-a> <file-b>\n", argv[0]);
return 0;
}
fp0 = file_open(argv[1], "r", argv[0]);
fp1 = file_open(argv[2], "r", argv[0]);

n = -1;
do {
n++;
len0 = fgetline(fp0, line0, MAXLINE);
len1 = fgetline(fp1, line1, MAXLINE);
} while (len0 > 0 && len1 > 0 && len0 == len1 && strcmp(line0, line1) == 0);

if (len0 != len1 || strcmp(line0, line1) != 0)
printf("Files differ, line %d:\n"
"%s: %s\n"
"%s: %s\n",
n,
argv[1], len0 == 0 ? "[EOF]" : line0,
argv[2], len1 == 0 ? "[EOF]" : line1);

fclose(fp0);
fclose(fp1);
return 0;
}
16 changes: 16 additions & 0 deletions chapter_7.input_output/7_6.fcompare/tests/4_file1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# include <stdio.h>
# include <stdlib.h>
# include <string.h>

/* file_open: open a file, and handle errors */
FILE *file_open (char *name, char *mode, char *prog)
{
FILE *fp;
if ((fp = fopen(name, mode)) == NULL) {
fprintf(stderr, "%s ERROR! Can't open %s.\n", prog, name);
exit(1);
}
return fp;
}

/* fgetline: read a line from a file, return length */

0 comments on commit 841ad94

Please sign in to comment.