C读取 文本文件 没有问题,除文本文件之外的文件就有问题

大_爱 2014-02-27 11:02:52
RT
这是我的代码

FILE *stream;
int const NUMB = 1024 * 63;
char buffer[NUMB] = {0};
stream = fopen(filePath,"rb");

while ( fgets(buffer,NUMB,stream) != 0)
{
printf("%s\n", buffer);
}
fclose(stream);
...全文
142 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
大_爱 2014-02-27
  • 打赏
  • 举报
回复
引用 13 楼 zhao4zhong1 的回复:
不要把 fopen("...","...");fscanf,fprintf,fgets,fgetc,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fgetc,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了 不加白不加!
引用 13 楼 zhao4zhong1 的回复:
不要把 fopen("...","...");fscanf,fprintf,fgets,fgetc,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fgetc,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了 不加白不加!
引用 13 楼 zhao4zhong1 的回复:
不要把 fopen("...","...");fscanf,fprintf,fgets,fgetc,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fgetc,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了 不加白不加!
能用fread()读取整个文件么?

while ( fread(buffer,NUMB,1,stream) != NULL)
	{
		
		 printf("%s\n", buffer);
为什么我这么写,这能读取到一点东西啦。也是结束符的问题?
赵4老师 2014-02-27
  • 打赏
  • 举报
回复
不要把 fopen("...","...");fscanf,fprintf,fgets,fgetc,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fgetc,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了 不加白不加!
大_爱 2014-02-27
  • 打赏
  • 举报
回复
引用 11 楼 zhao4zhong1 的回复:
#include <stdio.h>
#include <string.h>
FILE *stream;
int const NUMB = 1024 * 63;
char buffer[NUMB] = {0};
char filePath[256];
int r,a;
void HexDump(char *buf,int len,int addr) {
    int i,j,k;
    char binstr[80];

    for (i=0;i<len;i++) {
        if (0==(i%16)) {
            sprintf(binstr,"%08x -",i+addr);
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        } else if (15==(i%16)) {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
            sprintf(binstr,"%s  ",binstr);
            for (j=i-15;j<=i;j++) {
                sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
            }
            printf("%s\n",binstr);
        } else {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        }
    }
    if (0!=(i%16)) {
        k=16-(i%16);
        for (j=0;j<k;j++) {
            sprintf(binstr,"%s   ",binstr);
        }
        sprintf(binstr,"%s  ",binstr);
        k=16-k;
        for (j=i-k;j<i;j++) {
            sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
        }
        printf("%s\n",binstr);
    }
}
int main() {
    strcpy(filePath,"c:\\windows\\system32\\msimg32.dll");
    stream=fopen(filePath,"rb");
    if (NULL==stream) {
        printf("Can not open file %s!\n",filePath);
        return 1;
    }
    a=0;
    while (1) {
        r=fread(buffer,1,NUMB,stream);
        HexDump(buffer,r,a);
        a+=r;
        if (r<NUMB) break;
    }
    fclose(stream);
    return 0;
}
感谢赵老师,fgets() 函数不能用于二进制文件,fgets函数用于二进制文件的时候会自动转码。转完之后可能就发生变化了。可能会遇到意想不到的问题,比如我这里的提前结束。
赵4老师 2014-02-27
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
FILE *stream;
int const NUMB = 1024 * 63;
char buffer[NUMB] = {0};
char filePath[256];
int r,a;
void HexDump(char *buf,int len,int addr) {
    int i,j,k;
    char binstr[80];

    for (i=0;i<len;i++) {
        if (0==(i%16)) {
            sprintf(binstr,"%08x -",i+addr);
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        } else if (15==(i%16)) {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
            sprintf(binstr,"%s  ",binstr);
            for (j=i-15;j<=i;j++) {
                sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
            }
            printf("%s\n",binstr);
        } else {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        }
    }
    if (0!=(i%16)) {
        k=16-(i%16);
        for (j=0;j<k;j++) {
            sprintf(binstr,"%s   ",binstr);
        }
        sprintf(binstr,"%s  ",binstr);
        k=16-k;
        for (j=i-k;j<i;j++) {
            sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
        }
        printf("%s\n",binstr);
    }
}
int main() {
    strcpy(filePath,"c:\\windows\\system32\\msimg32.dll");
    stream=fopen(filePath,"rb");
    if (NULL==stream) {
        printf("Can not open file %s!\n",filePath);
        return 1;
    }
    a=0;
    while (1) {
        r=fread(buffer,1,NUMB,stream);
        HexDump(buffer,r,a);
        a+=r;
        if (r<NUMB) break;
    }
    fclose(stream);
    return 0;
}
大_爱 2014-02-27
  • 打赏
  • 举报
回复
引用 6 楼 baichi4141 的回复:
文本文件是最简单的,每个二进制字节直接按ASCII码表对应一个字符,所以有fgets等等字符串相关函数自动替你处理 其他格式的文件,必须自己按照该文件格式的定义处理每一个二进制字节——当然,如果是比较通用的格式,可以找到相应的处理模块不必自己从头写,例如处理word文件就可以直接使用微软放在word软件里的模块 但是根本上,作为一个程序员,你就不该有“任何问题都可以找几条语句直接搞定”的想法,一门语言应该提供给你的都是最基本的操作,任何稍微复杂一点的问题都需要你自己动脑子将这些基本操作组合成你需要的解决方法,如果你能找到一些比较简单的处理方法,那是因为别人替你做了大部分工作,不代表这大部分工作就不存在
大_爱 2014-02-27
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。
正在看,好像没什么用!
大_爱 2014-02-27
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。啊 GBK:0xB0 0xA1,Unicode-16 LE:0x4A 0x55,Unicode-16 BE:0x55 0x4A,UTF-8:0xE5 0x95 0x8A In Visual C++ 2005, fopen supports Unicode file streams. A flag specifying the desired encoding may be passed to fopen when opening a new file or overwriting an existing file, like this: fopen("newfile.txt", "rw, ccs=<encoding>"); Allowed values of the encoding include UNICODE, UTF-8, and UTF16-LE. If the file is already in existence and is opened for reading or appending, the Byte Order Mark (BOM) is used to determine the correct encoding. It is not necessary to specify the encoding with a flag. In fact, the flag will be ignored if it conflicts with the type of the file as indicated by the BOM. The flag is only used when no BOM is present or if the file is a new file. The following table summarizes the modes used in for various flags given to fopen and Byte Order Marks used in the file. Encodings Used Based on Flag and BOM Flag No BOM (or new file) BOM: UTF-8 BOM: UTF-16 UNICODE ANSI UTF-8 UTF-16LE UTF-8 UTF-8 UTF-8 UTF-16LE UTF-16LE UTF-16LE UTF-8 UTF-16LE
还是不行。
赵4老师 2014-02-27
  • 打赏
  • 举报
回复
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。
baichi4141 2014-02-27
  • 打赏
  • 举报
回复
文本文件是最简单的,每个二进制字节直接按ASCII码表对应一个字符,所以有fgets等等字符串相关函数自动替你处理 其他格式的文件,必须自己按照该文件格式的定义处理每一个二进制字节——当然,如果是比较通用的格式,可以找到相应的处理模块不必自己从头写,例如处理word文件就可以直接使用微软放在word软件里的模块 但是根本上,作为一个程序员,你就不该有“任何问题都可以找几条语句直接搞定”的想法,一门语言应该提供给你的都是最基本的操作,任何稍微复杂一点的问题都需要你自己动脑子将这些基本操作组合成你需要的解决方法,如果你能找到一些比较简单的处理方法,那是因为别人替你做了大部分工作,不代表这大部分工作就不存在
大_爱 2014-02-27
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。啊 GBK:0xB0 0xA1,Unicode-16 LE:0x4A 0x55,Unicode-16 BE:0x55 0x4A,UTF-8:0xE5 0x95 0x8A In Visual C++ 2005, fopen supports Unicode file streams. A flag specifying the desired encoding may be passed to fopen when opening a new file or overwriting an existing file, like this: fopen("newfile.txt", "rw, ccs=<encoding>"); Allowed values of the encoding include UNICODE, UTF-8, and UTF16-LE. If the file is already in existence and is opened for reading or appending, the Byte Order Mark (BOM) is used to determine the correct encoding. It is not necessary to specify the encoding with a flag. In fact, the flag will be ignored if it conflicts with the type of the file as indicated by the BOM. The flag is only used when no BOM is present or if the file is a new file. The following table summarizes the modes used in for various flags given to fopen and Byte Order Marks used in the file. Encodings Used Based on Flag and BOM Flag No BOM (or new file) BOM: UTF-8 BOM: UTF-16 UNICODE ANSI UTF-8 UTF-16LE UTF-8 UTF-8 UTF-8 UTF-16LE UTF-16LE UTF-16LE UTF-8 UTF-16LE
确实提前结束了。
赵4老师 2014-02-27
  • 打赏
  • 举报
回复
对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。啊 GBK:0xB0 0xA1,Unicode-16 LE:0x4A 0x55,Unicode-16 BE:0x55 0x4A,UTF-8:0xE5 0x95 0x8A In Visual C++ 2005, fopen supports Unicode file streams. A flag specifying the desired encoding may be passed to fopen when opening a new file or overwriting an existing file, like this: fopen("newfile.txt", "rw, ccs=<encoding>"); Allowed values of the encoding include UNICODE, UTF-8, and UTF16-LE. If the file is already in existence and is opened for reading or appending, the Byte Order Mark (BOM) is used to determine the correct encoding. It is not necessary to specify the encoding with a flag. In fact, the flag will be ignored if it conflicts with the type of the file as indicated by the BOM. The flag is only used when no BOM is present or if the file is a new file. The following table summarizes the modes used in for various flags given to fopen and Byte Order Marks used in the file. Encodings Used Based on Flag and BOM Flag No BOM (or new file) BOM: UTF-8 BOM: UTF-16 UNICODE ANSI UTF-8 UTF-16LE UTF-8 UTF-8 UTF-8 UTF-16LE UTF-16LE UTF-16LE UTF-8 UTF-16LE
大_爱 2014-02-27
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。 不要把 fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了
我读取的时候也没做替换啊
大_爱 2014-02-27
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。 不要把 fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了
fread 函数不自己处理?
赵4老师 2014-02-27
  • 打赏
  • 举报
回复
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。 不要把 fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了
赵4老师 2014-02-27
  • 打赏
  • 举报
回复
fread Reads data from a stream. size_t fread( void *buffer, size_t size, size_t count, FILE *stream ); Function Required Header Compatibility fread <stdio.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count. Use the feof or ferror function to distinguish a read error from an end-of-file condition. If size or count is 0, fread returns 0 and the buffer contents are unchanged. Parameters buffer Storage location for data size Item size in bytes count Maximum number of items to be read stream Pointer to FILE structure Remarks The fread function reads up to count items of size bytes from the input stream and stores them in buffer. The file pointer associated with stream (if there is one) is increased by the number of bytes actually read. If the given stream is opened in text mode, carriage return–linefeed pairs are replaced with single linefeed characters. The replacement has no effect on the file pointer or the return value. The file-pointer position is indeterminate if an error occurs. The value of a partially read item cannot be determined. Example /* FREAD.C: This program opens a file named FREAD.OUT and * writes 25 characters to the file. It then tries to open * FREAD.OUT and read in 25 characters. If the attempt succeeds, * the program displays the number of actual items read. */ #include <stdio.h> void main( void ) { FILE *stream; char list[30]; int i, numread, numwritten; /* Open file in text mode: */ if( (stream = fopen( "fread.out", "w+t" )) != NULL ) { for ( i = 0; i < 25; i++ ) list[i] = (char)('z' - i); /* Write 25 characters to stream */ numwritten = fwrite( list, sizeof( char ), 25, stream ); printf( "Wrote %d items\n", numwritten ); fclose( stream ); } else printf( "Problem opening the file\n" ); if( (stream = fopen( "fread.out", "r+t" )) != NULL ) { /* Attempt to read in 25 characters */ numread = fread( list, sizeof( char ), 25, stream ); printf( "Number of items read = %d\n", numread ); printf( "Contents of buffer = %.25s\n", list ); fclose( stream ); } else printf( "File could not be opened\n" ); } Output Wrote 25 items Number of items read = 25 Contents of buffer = zyxwvutsrqponmlkjihgfedcb Stream I/O Routines See Also fwrite, _read

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧