I've been working up my game with open-source distros and decided to solve a
K&R problem to highlight my own misunderstanding of the -v compiler switch.
I selected a problem from chp 7 at more or less random and decided on 7.6.
I don't have an english k&R, but the german one lautet: Aufgabe 7-6.
Schreiben Sie ein Programm, das zwei Dateien vergleicht und die erste Zeile
ausgibt, wo sie verschieden sind.
For a soln, I used Heathfield's 2nd soln:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFF_SIZE 1000
/* uses fgets, removes the '\n' at the end of the string if it exists
*/
char *safegets(char *buffer, int length, FILE *file)
{
char *ptr;
int len;
if (buffer != NULL)
{
ptr = fgets(buffer, length, file);
if (ptr != NULL)
{
len = strlen(buffer);
if (len > 0)
{
if (buffer[len - 1] == '\n')
{
buffer[len - 1] = '\0';
}
}
}
return ptr;
}
return NULL;
}
int main(int argc, char *argv[])
{
FILE *leftFile, *rightFile;
char buff1[BUFF_SIZE], buff2[BUFF_SIZE];
char *ptr1, *ptr2;
unsigned long lineNum = 0;
if (argc < 3)
{
fprintf(stderr, "Usage : 7_6 <path to file> <path to
file>\n");
return 0;
}
if (!(leftFile = fopen(argv[1], "r")))
{
fprintf(stderr, "Couldn't open %s for reading\n",
argv[1]);
return 0;
}
if (!(rightFile = fopen(argv[2], "r")))
{
fprintf(stderr, "Couldn't open %s for reading\n",
argv[2]);
fclose(leftFile); /* RJH 10 Jul 2000 */
return 0;
}
/* read through each file, line by line */
ptr1 = safegets(buff1, BUFF_SIZE, leftFile);
ptr2 = safegets(buff2, BUFF_SIZE, rightFile);
++lineNum;
/* stop when either we've exhausted either file's data */
while (ptr1 != NULL && ptr2 != NULL)
{
/* compare the two lines */
if (strcmp(buff1, buff2) != 0)
{
printf("Difference:\n");
printf("%lu\t\"%s\" != \"%s\"\n", lineNum,
buff1, buff2);
goto CleanUp;
}
ptr1 = safegets(buff1, BUFF_SIZE, leftFile);
ptr2 = safegets(buff2, BUFF_SIZE, rightFile);
++lineNum;
}
/*
* if one of the files ended prematurely, it definitely
* isn't equivalent to the other
*/
if (ptr1 != NULL && ptr2 == NULL)
{
printf("Difference:\n");
printf("%lu\t\"%s\" != \"EOF\"\n", lineNum, buff1);
}
else if (ptr1 == NULL && ptr2 != NULL)
{
printf("Difference:\n");
printf("%lu\t\"EOF\" != \"%s\"\n", lineNum, buff2);
}
else
{
printf("No differences\n");
}
CleanUp:
fclose(leftFile);
fclose(rightFile);
return EXIT_SUCCESS;
}
// gcc -o print kr76.c
// print a.a c.c
// gcc -o kr -v kr76.c 2>text33.txt >text34.txt
The part at the end I call the goocher. These are dos commands that are
commented out. I sthis why I always receive a warning about newlines at end
of files?
a.a and c.c I created with notepad, viz.
The quick
brown fox
jumps over
the lazy dog.
and
The quick
brown faux
jumps over
the lazy dog.
When I run the executable, I get:
Difference:
2 "brown fox" != "brown faux"
So, no errors, no warnings, no misbehavior.
When I command:
gcc -o kr -v kr76.c 2>text33.txt >text34.txt
, I get:
Built by Equation Solution (http://www.Equation.com).
Using built-in specs.
Target: i386-pc-mingw32
Configured with:
.../gcc-4.2.3-mingw/configure --host=i386-pc-mingw32 --build=x86_64-unknown-linux-gnu
--target=i386-pc-mingw32 --prefix=/home/gfortran/gcc-home/binary/mingw32/native/x86_32/gcc/4.2.3
--with-gcc --with-gnu-ld --with-gnu-as --disable-shared --disable-nls --disable-tls
--with-gmp=/home/gfortran/gcc-home/binary/mingw32/native/x86_32/gmp --with-mpfr=/home/gfortran/gcc-home/binary/mingw32/native/x86_32/mpfr
--enable-languages=c,c++,fortran --with-sysroot=/home/gfortran/gcc-home/binary/mingw32/cross/x86_32/gcc/4.2.3
--enable-threads=win32 --enable-libgomp --disable-win32-registry
Thread model: win32
gcc version 4.2.3
c:/path/gcc/bin/../libexec/gcc/i386-pc-mingw32/4.2.3/cc1.exe -quiet -v -iprefix
c:\path\gcc\bin\../lib/gcc/i386-pc-mingw32/4.2.3/ kr76.c -quiet -dumpbase
kr76.c -mtune=i386 -auxbase kr76 -version -o
C:\DOCUME~1\fred\LOCALS~1\Temp/ccAL65cL.s
ignoring nonexistent directory
"/home/gfortran/gcc-home/binary/mingw32/cross/x86_32/gcc/4.2.3/home/gfortran/gcc-home/binary/mingw32/native/x86_32/gcc/4.2.3/lib/gcc/i386-pc-mingw32/4.2.3/../../../../include"
ignoring nonexistent directory
"/home/gfortran/gcc-home/binary/mingw32/native/x86_32/gcc/4.2.3/lib/gcc/i386-pc-mingw32/4.2.3/include"
ignoring nonexistent directory
"/home/gfortran/gcc-home/binary/mingw32/native/x86_32/gcc/4.2.3/i386-pc-mingw32/include"
ignoring nonexistent directory
"/home/gfortran/gcc-home/binary/mingw32/cross/x86_32/gcc/4.2.3/mingw/include"
ignoring nonexistent directory "C:/gcc/include"
#include "..." search starts here:
#include <...> search starts here:
C:/path/gcc/include
c:/path/gcc/bin/../lib/gcc/i386-pc-mingw32/4.2.3/include
c:/path/gcc/bin/../lib/gcc/i386-pc-mingw32/4.2.3/../../../../i386-pc-mingw32/includeEnd of search list.GNU C version 4.2.3 (i386-pc-mingw32) compiled by GNU C version 4.2.3.GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=131006Compiler executable checksum: b9b7925f719568bc0be9159aad1ef341kr76.c:110:48: warning: no newline at end of file c:/path/gcc/bin/../lib/gcc/i386-pc-mingw32/4.2.3/../../../../i386-pc-mingw32/bin/as.exe -o C:\DOCUME~1\fred\LOCALS~1\Temp/ccQHaDin.oC:\DOCUME~1\fred\LOCALS~1\Temp/ccAL65cL.s c:/path/gcc/bin/../libexec/gcc/i386-pc-mingw32/4.2.3/collect2.exe --sysroot=/home/gfortran/gcc-home/binary/mingw32/cross/x86_32/gcc/4.2.3 -Bdynamic -s -o kr.exeC:/path/gcc/i386-pc-mingw32/lib/crt2.o -Lc:/path/gcc/bin/../lib/gcc/i386-pc-mingw32/4.2.3 -Lc:/path/gcc/bin/../lib/gcc -LC:/path/gcc/i386-pc-mingw32/lib -Lc:/path/gcc/bin/../lib/gcc/i386-pc-mingw32/4.2.3/../../../../i386-pc-mingw32/lib -Lc:/path/gcc/bin/../lib/gcc/i386-pc-mingw32/4.2.3/../../..C:\DOCUME~1\fred\LOCALS~1\Temp/ccQHaDin.o -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt -luser32 -lkernel32 -ladvapi32 -lshell32 -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt// end comment by gccWhat does this all mean?--"A man is accepted into church for what he believes--and turned out forwhat he knows."-Mark Twain
|