|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Bug with compiler or am I just doing something illegal
This program should copy one file onto the other. It works if I
compile it with gcc to a cygwin program. However, if I compile it with the -mno-cygwin option, it doesn't work (this targets native windows). Anyway, I just want to check that the program is valid before I see if I can find a way around a compiler bug. It might be something simple that I am doing wrong. #include <stdio.h> main() { FILE *fp; fp = fopen( "scan0001b.bmp" , "r" ); if( fp == NULL ) { printf("File open failed for read\n"); exit(0); } FILE *fpo; fpo = fopen( "scanout.bmp" , "w"); if( fpo == NULL ) { printf("File open failed for write\n"); exit(0); } int c=1; while(!feof(fp)) { c = fgetc(fp); if( c>=0 ) fputc( c , fpo ); } fclose(fp); fclose(fpo); } When I run it, it stops before it has read the entire file (it only reads around 10%). |
|
#2
|
|||
|
|||
|
Bug with compiler or am I just doing something illegal
raphfrk wrote:
) This program should copy one file onto the other. It works if I ) compile it with gcc to a cygwin program. However, if I compile it ) with the -mno-cygwin option, it doesn't work (this targets native ) windows). ) ) Anyway, I just want to check that the program is valid before I see if ) I can find a way around a compiler bug. ) ) It might be something simple that I am doing wrong. It might be. There are certainly several simple things wrong with the code. ) FILE *fp; ) ) fp = fopen( "scan0001b.bmp" , "r" ); Not binary mode "rb" ? It is a binary file, right ? Perhaps if you read in text mode, certain characters can flag end of file on windows. Ctrl-Z perhaps. ) int c=1; Why initialize it ? And why to 1 ?? ) while(!feof(fp)) This mistake is so common that it has its own FAQ entry. You see, the eof flag is set at the moment a read operation is done when the file is at EF. So after the last character is read, feof(fp) will *not* return true. The next read will return an error code, and *only then* will feof(fp) be true. But theoretically, as is, it should work, because of the extra if (c>=0). ) { ) c = fgetc(fp); ) if( c>=0 ) ) fputc( c , fpo ); ) } The correct idiom is this: while ((c = fgetc(fp)) != EF) { fputc(c, fpo); /* Should check for errors here also, I think */ } /* And here an if (ferror(fp)) would be nice */ ) fclose(fp); ) fclose(fpo); ) ) } ) ) ) ) When I run it, it stops before it has read the entire file (it only ) reads around 10%). , I guess there is a ^Z character at 10% in the input file. SaSW, Willem -- Disclaimer: I am in no way responsible for any of the statements made in the above text. For all I know I might be drugged or something No I'm not paranoid. You all think I'm paranoid, don't you ! #ET |
|
#3
|
|||
|
|||
|
Bug with compiler or am I just doing something illegal
"Ben Bacarisse" wrote: "Robbie Hatley" writes: > "raphfrk" wrote: > while(!feof(fp)) { c = fgetc(fp); if( c>=0 ) fputc( c , fpo ); } > In addition to the excellent advice others here gave you, there's one *HUGE* error here which everyone seems to have missed somehow: the 0 byte, 0x00, is a perfectly valid byte both for text files (ASCII, iso-8859-1, etc) and binary (non-text) files. Many of the bytes in a bmp file will be 0. If you omit those and close up the gaps, you will severely corrupt the copy of the original file. > I presume you missed the = part of the >=? Yes, indeed. I must have been looking at it cross-eyed or something. The canonical version would be: > while ((c = fgetc(fp)) != EF) fputc(c, fpo); Yes, on looking at the instruction file for libc that comes with my compiler (djgpp), i see it recommends similar: int c; while((c=fgetc(stdin)) != EF) fputc(c, stdout); Substitute generic file pointers for stdin and stdout, and it becomes indentical to the version you gave. The c being an int (not char or unsigned char) is important, because fgetc returns an int containing the unsigned value (0 to 255) of the character, or EF if character couldn't be read. (I'm assuming EF would always be defined out of the 0-255 range so it couldn't be confused with a character value.) -- Cheers, Robbie Hatley lonewolf aatt well dott com www dott well dott com slant user slant lonewolf slant |
|
#4
|
|||
|
|||
|
Bug with compiler or am I just doing something illegal
Richard Tobin wrote:
) I think Chuck may have misunderstood "if fgetc returned EF" to mean ) "if the fgetc() function were defined to return EF" rather than "if ) this call to fgetc() happens to return EF". But I could be wrong. It would have been better worded as "when fgetc returns EF", I think. English has these subtle differences between 'if' and 'when', you see. SaSW, Willem -- Disclaimer: I am in no way responsible for any of the statements made in the above text. For all I know I might be drugged or something No I'm not paranoid. You all think I'm paranoid, don't you ! #ET |
|
#5
|
|||
|
|||
|
Bug with compiler or am I just doing something illegal
Willem wrote:
Richard Tobin wrote: ) I think Chuck may have misunderstood "if fgetc returned EF" to mean ) "if the fgetc() function were defined to return EF" rather than "if ) this call to fgetc() happens to return EF". But I could be wrong. > It would have been better worded as "when fgetc returns EF", I think. English has these subtle differences between 'if' and 'when', you see. I see now. I was probably thinking in C when I wrote what I wrote. if (rc == EF) vs. when (rc == EF) -- pete |
![]() |
| Viewing: Web Development Archives > FAQs > C/C++ > Bug with compiler or am I just doing something illegal |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|