无论如何,我只想在看到 之前检查程序是否有效。我可以找到解决编译器错误的方法。 我做错了可能很简单。 -------------------------- ----------------------------------- #include< stdio.h> ; main() main()返回一个int。当你写这个函数时这么说: int main(void) { FILE * fp; fp = fopen(" scan0001b.bmp"," r"); 你没有以二进制模式打开文件。 在某些系统上,这会导致文件中的某些字符变为 由C库(或操作系统??)解释,那些 字符将无法像在磁盘上那样完全到达您的程序。它可能会发生一些角色告诉C库(或 操作系统)停止在那里读取文件,即使 读取的字符数只是系统通过其他方式报告的文件的一小部分 长度。 if(fp) == NULL) { printf(文件打开失败,读取\ n); 退出(0); } FILE * fpo; fpo = fopen(" scanout.bmp"," w" ); 你没有以二进制模式打开文件。 字符的翻译可能与发生的情况类似。当你以非二进制模式读取文件时,。 > if(fpo == NULL) { printf(文件打开失败,写入\ n); 退出(0); } int c = 1; 为什么1?这是什么意思? 我看到你正在使用C99(你正在混合声明和代码)。只要您了解C99编译器 并不像C89编译器那样容易获得,并且你不介意丢失 一些便携性。 > while(!feof(fp)) { c = fgetc(fp); if(c> = 0) fputc(c,fpo); } 这个循环是错误的。 feof()不会做你认为的那样做。 在c-faq上阅读问题12.2的答案( http: //c-faq.com/ ), ,当你在那里时,给网站添加书签并立即返回 再次。 fclose(fp); 无法测试fclose()调用是否成功。 fclose(fpo); } --------------------------- ------------------ 当我运行它时,它会在读完整个文件之前停止(它只是 读数大约10%)。 This program should copy one file onto the other. It works if Icompile it with gcc to a cygwin program. However, if I compile itwith the -mno-cygwin option, it doesn''t work (this targets nativewindows).Anyway, I just want to check that the program is valid before I see ifI 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 onlyreads around 10%). 解决方案 In comp.lang.c, raphfrk wrote:This program should copy one file onto the other. It works if Icompile it with gcc to a cygwin program. However, if I compile itwith the -mno-cygwin option, it doesn''t work (this targets nativewindows).Anyway, I just want to check that the program is valid before I see ifI 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" );You''ve opened this file with the "read text" option. Presuming that thefilename represents a file in the Microsoft bitmap graphics format(".BMP"), then this is the wrong mode to open the file in. You probablywantfp = fopen( "scan0001b.bmp" , "rb" );here.if( fp == NULL ) { printf("File open failed for read\n"); exit(0); }FILE *fpo;fpo = fopen( "scanout.bmp" , "w");Similarly, this is the wrong mode to open a (presumably binary) output file.>if( fpo == NULL ) { printf("File open failed for write\n"); exit(0); }int c=1;while(!feof(fp))Remember, feof() does not read the file, and returns true /after/ the trueread (in your case, fgetc()) returns an end-of-file condition. { c = fgetc(fp);In Microsoft Windows, text files are permitted to contain a binary octetmarker (0x1a or ^Z), which will indicate a logical end-of-file prior to thephysical end of the file. While this is a leftover from the MSDOS 1 days,it still is enforced and acted apon by the underlying Windows I/O model.If you /did/ mean your input file to contain pure binary data (rather thanthe text that you indicate by the file mode string), then there is a verygood chance that at least one character (octet) of this pure binary datahas the value of 0x1a. This would cause your fgetc() on the file toprematurely return EOF, and subsequently cause feof() to return true. Thisin turn causes you to abort the copy process prior to the actual physicalend-of-file of the (presumably binary) input file. if( c>=0 ) fputc( c , fpo ); }fclose(fp);fclose(fpo);}---------------------------------------------When I run it, it stops before it has read the entire file (it onlyreads around 10%).--Lew PitcherMaster Codewright & JOAT-in-training | Registered Linux User #112576 http://pitcher.digitalfreehold.ca/ | GPG public key available by request---------- Slackware - Because I know what I''m doing. ------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 characterscan 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 donewhen the file is at EOF. 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)) != EOF) {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%).Offhand, 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 statementsmade in the above text. For all I know I might bedrugged or something..No I''m not paranoid. You all think I''m paranoid, don''t you !#EOTraphfrk wrote:This program should copy one file onto the other. It works if Icompile it with gcc to a cygwin program. However, if I compile itwith the -mno-cygwin option, it doesn''t work (this targets nativewindows).Anyway, I just want to check that the program is valid before I see ifI can find a way around a compiler bug.It might be something simple that I am doing wrong.-------------------------------------------------------------#include <stdio.h>main()main() return an int. Say so when you write the function:int main(void){FILE *fp;fp = fopen( "scan0001b.bmp" , "r" );You''re not opening the file in binary mode.On some systems, that will cause some characters in the file to beinterpreted by the C library (or the Operating System??) and thosecharacters will not reach your program exactly as they are on disk. Itmay even happen that some character tells the C library (or theOperating System) to stop reading the file right there, even thoughthe number of characters read is only a small percentage of the filelength reported by the system through other means.if( fp == NULL ) { printf("File open failed for read\n"); exit(0); }FILE *fpo;fpo = fopen( "scanout.bmp" , "w");You''re not opening the file in binary mode.Translation of characters can occur in a similar way to what happenswhen you read a file in not binary mode.>if( fpo == NULL ) { printf("File open failed for write\n"); exit(0); }int c=1;Why 1? What does it mean?I see you''re using C99 (you''re mixing declarations and code). There''snothing wrong with that as long as you understand that C99 compilersaren''t as readily available as C89 compilers, and you don''t mind losesome portability.>while(!feof(fp)) { c = fgetc(fp); if( c>=0 ) fputc( c , fpo ); }This loop is wrong.feof() doesn''t do what you think it does.Read the answer to question 12.2 on the c-faq ( http://c-faq.com/ ),and while you''re there, bookmark the site and return there every nowand again.fclose(fp);Failed to test if the fclose() call succeded.fclose(fpo);}---------------------------------------------When I run it, it stops before it has read the entire file (it onlyreads around 10%). 这篇关于编译器的错误或我只是做一些非法的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!