|
From: Johannes P. <joh...@pr...> - 2001-09-04 12:50:12
|
Okay
Problem 2 is actually kinda simple in the sense if I understand you correctly
//SNIP
#include <stdio.h>
#include <alloc.h>
main(int argc, char *argv[]) {
FILE *CheckFile;
int Byte;
unsigned int Checksum;
int read=-1;
int filePos =0;
if ( argc != 2 ) {
printf("usage: checksum filename\n");
exit(1);
}
if ( (CheckFile = fopen(argv[1], "rb")) == NULL ) {
printf("Can't open file: %s\n", argv[1]);
exit(1);
}
/* Initialize the checksum accumulator */
Checksum = 0;
fseek (CheckFile, 0L, SEEK_SET);
char c1[100];
char c2[100];
char c3[100];
char c4[100];
char c5[100];
memset(c1,0,100);
memset(c2,0,100);
memset(c3,0,100);
memset(c4,0,100);
memset(c5,0,100);
printf ("pos %d\n", ftell (CheckFile));
printf("Succesfull assignments : %d c1 : %s\n c2 : %s\n",fscanf
(CheckFile, "%s%s%*[\n]", c1, c2),c1,c2);
//part 1
printf("Succesfull assignments : %d c3 : %s\n c4 : %s\n c5 : %s
",fscanf (CheckFile, "%*[^'O]%s%s%s", c3, c4, c5),c3,c4,c5);
//part 2
//printf("Succesfull assignments : %d c3 : %s\n c4 : %s\n c5 : %s
",fscanf (CheckFile, "%s%s%s%*[\n]", c3, c4, c5),c3,c4,c5);
printf ("pos %d\n", ftell (CheckFile));
fclose(CheckFile);
exit(0);
}
//END SNIP
If you take the above code and run it it does the same as your application.
If you comment part 1 out and uncomment part 2 it reads the next line as
needed. The reason is that you have told the function to move the
cursor until it reaches one of the specified characeters in part 1 that is
a qute (') or a captial O. This is why it runs until past that point as one
of there charcters only
comes after that to test. Enter a capital O in the second line of the program.
Well this is all I can think of now. One thing thou using %s to read a
string into a char array is DANGEROUS for the function will read the string
into it regardless of its size.
To test put in a string in the first line that is MORE than 100 then printf
(well I geuss my program can also be wrong)
if you rather use a char pointer you can read in more that the 100
specified. yust remmeber the free.. here is a code sample to replace in the
above.
//SNIP 2
char *c1;
char c2[100];
char c3[100];
char c4[100];
char c5[100];
// memset(c1,0,100);
memset(c2,0,100);
memset(c3,0,100);
memset(c4,0,100);
memset(c5,0,100);
printf ("pos %d\n", ftell (CheckFile));
printf("Succesfull assignments : %d c1 : %s\n c2 : %s\n",fscanf
(CheckFile, "%s%s", c1, c2),c1,c2);
free(c1);
//END SNIP 2
Well good luck
Johannes
|