|
From: Johannes P. <joh...@pr...> - 2001-09-04 12:21:52
|
>Date: Tue, 04 Sep 2001 13:28:15 +0200
>To: "Michael Codadeen" <jd...@ho...>
>From: Johannes Pretorius <joh...@pr...>
>Subject: RE: [Dev-C++] how to use strings
>
>Hi Michael
>----------------
>
>it seems I described the reason I believe fprintf() wont move the file
>pointer wrong. What I remember and please correct me if I
>am wrong is that the following :
>FILE *filepointer;
>is actually a pointer to a STRUCTURE that is named FILE. This meaning it
>is NOT a memory alocation pointer. I did this long ago so
>the change is very big that I am wrong. So to start of this means that
>reading from the filepointer to stream data to the output stream
>if it is with fprintf() or sprintf() the function does not know HOW to get
>the data (the data is internal to the structure) and secondly the function
>was not writen
>to move the stream pointer (as with sockets I cant remember now but I
>belive you have to use a different reader to get the data. The chance is
>that I am
>incorrect and you must use sprintf as I have done this about 2 years ago I
>kinda forgot with all this Delphi.). But anyways try this application.
>
>//SNIP
>
>#include <stdio.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], "r")) == NULL ) {
> printf("Can't open file: %s\n", argv[1]);
> exit(1);
> }
>
> /* Initialize the checksum accumulator */
> Checksum = 0;
>
> /* Add each byte in turn into the checksum accumulator */
> while ((!feof(CheckFile)) && (!ferror(CheckFile))) {
> filePos = ftell(CheckFile);
> printf("filePos : %d\n",filePos);
> read = fprintf(stdout,"%s",*CheckFile);
> if (read){
> printf("read : %d\n",read);
> if ((!feof(CheckFile)) && (!ferror(CheckFile))) {
> printf("set file pos\n");
> if (fseek(CheckFile,read,SEEK_CUR) != 0){
> printf("Error setting file Pos\n");
> break;
> }
> }else{
> break;
> }
> } else{
> printf("Error reading ");
> break;
> }
> }
> fclose(CheckFile);
> exit(0);
>}
>
>
>//END SNIP
>
>You will see that we read and read and nothing gets done. This is a result
>that I dont understand actually as it is suppose to read until EOF.
>
>I hope something makes some more sense.
>
>I am looking at the second problem will be back an about 10 minutes
>Johanens
>
>
>
>
>
>
>At 10:00 04/09/2001 +0000, you wrote:
>>Hi Johannes,
>>
>>I believe that fprintf() does move the file pointer it just looks as
>>though it isn't, try using ftell() to print the file position.
>>
>>I believe that because there is a struct being written to the file via
>>fread() and then a string via fprintf() this is causing the problem as
>>you suggested.
>>
>>These functions generally should never be mixed as the problems we are
>>seeing are caused. Also when reading files between different systems
>>there are problems concerning the endianess (big endian/little endian)
>>for the file, allignment, etc. For example, the allignment of an int on
>>one machine may be different for another. One would have to define a
>>common alligned struct for writing, similar to the way X11, sockets, and
>>other network protocols do it.
>>
>>However, we are only using one os in this case so this shouldn't be a
>>problem.
>>
>>I also used fgets() and feof() to test for EOF. The output is only "Hallo
>>World", and it doesn't see the number on the next line but reports EOF.
>>
>> char line [256];
>> while (fgets (line, 256, fhandle) != NULL)
>> {
>> printf ("%s", line);
>> }
>>
>> if (feof (fhandle))
>> {
>> printf ("EOF for fhandle.\n");
>> }
>>
>>However, I have also used fscanf()with the field specifier and the
>>circumflex symbol to ignore upto the newline, etc. This works in certain
>>circumstances, i.e. only when I specify two scanf() commands as opposed to one.
>>
>> fseek (fhandle, 0L, SEEK_SET);
>>
>> char c1[100];
>> char c2[100];
>> char c3[100];
>> char c4[100];
>> char c5[100];
>>
>>/*
>> // doesn't work - only sees first line
>>
>> printf ("pos %d\n", ftell (fhandle));
>> fscanf (fhandle, "%s%s%*[^\n]%*[^'O']%s%s%s", c1, c2, c3, c4, c5);
>> printf ("%s %s %s %s %s\n", c1, c2, c3, c4, c5);
>> printf ("pos %d\n", ftell (fhandle));
>>*/
>>
>> // works well
>>
>> printf ("pos %d\n", ftell (fhandle));
>> fscanf (fhandle, "%s%s%*[^\n]", c1, c2);
>> printf ("%s %s\n", c1, c2);
>>
>> fscanf (fhandle, "%*[^'O']%s%s%s", c3, c4, c5);
>> printf ("pos %d\n", ftell (fhandle));
>> printf ("%s %s %s\n", c3, c4, c5);
>>
>>
>>You will notice that the file position is over 200 at the end of the
>>program. This leads me to conclude that because of the 'crap' after the
>>first string this causes problems for the string functions, we therefore
>>need to ignore this stuff.
>>
>>This doesn't explain why I need two fscanf()s to do this. Although I have
>>been testing on a HPUX compiler and this should work.
>>
>>Regards,
>>
>>Michael
>>
>>
>>
>>_________________________________________________________________
>>Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
>>
>>
>>_______________________________________________
>>Dev-cpp-users mailing list
>>Dev...@li...
>>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>Johannes Pretorius
>(Development Department - Promed)
>
>Tel : +27 11 263-5920
>website : http://www.promed.co.za
|