|
From: Michael C. <jd...@ho...> - 2001-09-04 14:05:30
|
I read your first mail and have to agree that there may well be undefined
results when you mix and match like we have done.
As for your answer to the second problem, see below for more comments...
>From: Johannes Pretorius <joh...@pr...>
>To: dev...@li...
>Subject: RE: [Dev-C++] how to use strings
>Date: Tue, 04 Sep 2001 14:51:17 +0200
>
>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);
*** There is a single quote missing in this, as in my original mail it ***
should read 'O', the character constant for the letter O.
***The following also works but misses the \n.
***fscanf (fhandle, "%s%s%*[^'O']%s%s%s", c1, c2, 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.
***As described above, my original example had 'O' in it.
>
>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.
***
I agree. I think a buffer large enough should be allocated for the data to
be placed into. However, in your example you missed out the new() for c1.
The result would be a core dump.
The size for the buffer could be obtained by the number of bytes read and
returned by fread(). As you know fscanf() returns the number of succesfully
assigned input items.
>
>//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
>
>
I think we have done this subject to death now.
Thanks for all your input.
Michael
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
|