fread fopen 字符 二进制读写

刚刚遇到的问题,一直以为使用C标准库的fopen函数时,mode参数中’b’的作用可有可无,今天遇到了一点麻烦,所以才感觉到了有什么区别。
Linux下的换行符是‘\n’,windows 下的是’\r\n’。
这个问题在Linux不会出现,在windows 下才会。
现有一个windows下的文本文件。
如果打开文件的时候mode参数没有‘b’,则认为是以字符文件的形式打开,在使用fread读取文件内容的时候,遇到了’\r\n’这样的两个字节换行符,fread会把它变成一个字符 ‘\n’, 忽略了’\r’,这样,fread的返回值–实际读取字节数要比文件定位符的跳转的数量小。比如:读取了50个字节的内容,有5行(4个换行符),那么fread返回值就是50-4=46。
如果用带有‘b’的二进制模式打开,这个问题就不会有了,因为它会把’\r’单独作为一个字符对待。

#include <stdio.h>

int main( void )
{
    FILE *stream;
    char list[4000];
    int  i, numread, numwritten;

    stream=fopen("C:\\t1.cpp","rb");
    if (!stream)
    {
        printf("Open file error\n");
        return 0;
    }

    numread = fread( list, sizeof( char ), 2000, stream );
    printf( "Number of items read = %d\n", numread );
    numread = fread( list, sizeof( char ), 20, stream );
    printf( "Number of items read = %d\n", numread );


    fclose( stream );
    return 1;
}

参考

http://blog.csdn.net/mn200456/article/details/6950057

FILE *fopen( 
   const char *filename,
   const char *mode 
);
FILE *_wfopen( 
   const wchar_t *filename,
   const wchar_t *mode 
);

Parameters
filename
Filename.

mode
Type of access permitted.

Return Value
Each of these functions returns a pointer to the open file. A null pointer value indicates an error. If filename or mode is NULL or an empty string, these functions trigger the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, these functions return NULL and set errno to EINVAL.

The character string mode specifies the type of access requested for the file, as follows:

“r”
Opens for reading. If the file does not exist or cannot be found, the fopen call fails.

“w”
Opens an empty file for writing. If the given file exists, its contents are destroyed.

“a”
Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn’t exist.

“r+”
Opens for both reading and writing. (The file must exist.)

“w+”
Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.

“a+”
Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.

When a file is opened with the “a” or “a+” access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek or rewind, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.

The “a” mode does not remove the EOF marker before appending to the file. After appending has occurred, the MS-DOS TYPE command only shows data up to the original EOF marker and not any data appended to the file. The “a+” mode does remove the EOF marker before appending to the file. After appending, the MS-DOS TYPE command shows all data in the file. The “a+” mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker.

When the “r+”, “w+”, or “a+” access type is specified, both reading and writing are allowed (the file is said to be open for “update”). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.

In addition to the above values, the following characters can be included in mode to specify the translation mode for newline characters:

t
Open in text (translated) mode. In this mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing with “a+”, fopen checks for a CTRL+Z at the end of the file and removes it, if possible. This is done because using fseek and ftell to move within a file that ends with a CTRL+Z, may cause fseek to behave improperly near the end of the file.

Also, in text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output. When a Unicode stream-I/O function operates in text mode (the default), the source or destination stream is assumed to be a sequence of multibyte characters. Therefore, the Unicode stream-input functions convert multibyte characters to wide characters (as if by a call to the mbtowc function). For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters (as if by a call to the wctomb function).

b
Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed.

If t or b is not given in mode, the default translation mode is defined by the global variable _fmode. If t or b is prefixed to the argument, the function fails and returns NULL.

For more information about using text and binary modes in Unicode and multibyte stream-I/O, see Text and Binary Mode File I/O and Unicode Stream I/O in Text and Binary Modes.

c
Enable the commit flag for the associated filename so that the contents of the file buffer are written directly to disk if either fflush or _flushall is called.

n
Reset the commit flag for the associated filename to “no-commit.” This is the default. It also overrides the global commit flag if you link your program with COMMODE.OBJ. The global commit flag default is “no-commit” unless you explicitly link your program with COMMODE.OBJ (see Link Options).

N
Specifies that the file is not inherited by child processes.

S
Specifies that caching is optimized for, but not restricted to, sequential access from disk.

R
Specifies that caching is optimized for, but not restricted to, random access from disk.

T
Specifies a file as temporary. If possible, it is not flushed to disk.

D
Specifies a file as temporary. It is deleted when the last file pointer is closed.

ccs=ENCODING
Specifies the coded character set to use (UTF-8, UTF-16LE, or UNICODE) for this file. Leave unspecified if you want ANSI encoding. This option is available in Visual C++ 2005 and later.

Valid characters for the mode string used in fopen and _fdopen correspond to oflag arguments used in _open and _sopen, as follows.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值