一个read write数据不一致的问题

zh1599512 2016-03-02 09:32:51
用fread一个文件,buffer中的内容检验过了,hash值都一样,但是从buffer中write回另外一个文件的时候,发现两个文件不一致。后面新的文件总是比原文件大一点点,怎么解决啊?
...全文
301 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-03-02
  • 打赏
  • 举报
回复
fopen, _wfopen Open a file. FILE *fopen( const char *filename, const char *mode ); FILE *_wfopen( const wchar_t *filename, const wchar_t *mode ); Function Required Header Compatibility fopen <stdio.h> ANSI, Win 95, Win NT _wfopen <stdio.h> or <wchar.h> 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 The c, n, and t mode options are Microsoft extensions for fopen and _fdopen and should not be used where ANSI portability is desired. Return Value Each of these functions returns a pointer to the open file. A null pointer value indicates an error. Parameters filename Filename mode Type of access permitted Remarks The fopen function opens the file specified by filename. _wfopen is a wide-character version of fopen; the arguments to _wfopen are wide-character strings. _wfopen and fopen behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tfopen fopen fopen _wfopen 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. Valid characters for the mode string used in fopen and _fdopen correspond to oflag arguments used in _open and _sopen, as follows. Characters in mode String Equivalent oflag Value for _open/_sopen a _O_WRONLY | _O_APPEND (usually _O_WRONLY | _O_CREAT | _O_APPEND) a+ _O_RDWR | _O_APPEND (usually _O_RDWR | _O_APPEND | _O_CREAT ) r _O_RDONLY r+ _O_RDWR w _O_WRONLY (usually _O_WRONLY | _O_CREAT | _O_TRUNC) w+ _O_RDWR (usually _O_RDWR | _O_CREAT | _O_TRUNC) b _O_BINARY t _O_TEXT c None n None Example /* FOPEN.C: This program opens files named "data" * and "data2".It uses fclose to close "data" and * _fcloseall to close all remaining files. */ #include <stdio.h> FILE *stream, *stream2; void main( void ) { int numclosed; /* Open for read (will fail if file "data" does not exist) */ if( (stream = fopen( "data", "r" )) == NULL ) printf( "The file 'data' was not opened\n" ); else printf( "The file 'data' was opened\n" ); /* Open for write */ if( (stream2 = fopen( "data2", "w+" )) == NULL ) printf( "The file 'data2' was not opened\n" ); else printf( "The file 'data2' was opened\n" ); /* Close stream */ if( fclose( stream ) ) printf( "The file 'data' was not closed\n" ); /* All other files are closed: */ numclosed = _fcloseall( ); printf( "Number of files closed by _fcloseall: %u\n", numclosed ); } Output The file 'data' was opened The file 'data2' was opened Number of files closed by _fcloseall: 1 Stream I/O Routines See Also fclose, _fdopen, ferror, _fileno, freopen, _open, _setmode
zh1599512 2016-03-02
  • 打赏
  • 举报
回复
引用 4 楼 赵4老师的回复:
不要把 fopen("...","...");fscanf,fprintf,fgets,fgetc,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fseek,ftell,fread,fwrite,fgetc,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了
赵老师,谢谢指导,顺便问下,默认的open, fopen的打开方式是什么啊?write 直接就是二进制写入么?fwrite默认也是二进制写入嘛?
赵4老师 2016-03-02
  • 打赏
  • 举报
回复
不要把 fopen("...","...");fscanf,fprintf,fgets,fgetc,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fseek,ftell,fread,fwrite,fgetc,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了
zh1599512 2016-03-02
  • 打赏
  • 举报
回复
引用 2 楼 galiniur0u的回复:
楼主的buffer在fwrite的时候是不是大小控制的不对。 fwrite(a, strlen(a), 1, out); strlen(a)这里是不是没控制好?
write函数是直接以2进制写入嘛?open默认的打开方式是文本?
galiniur0u 2016-03-02
  • 打赏
  • 举报
回复
楼主的buffer在fwrite的时候是不是大小控制的不对。 fwrite(a, strlen(a), 1, out); strlen(a)这里是不是没控制好?
cocoabird 2016-03-02
  • 打赏
  • 举报
回复
比较readsize和writesize
基于扩展(EKF)和无迹卡尔曼滤波(UKF)的电力系统动态状态估计(Matlab代码实现)内容概要:本文档围绕基于扩展卡尔曼滤波(EKF)和无迹卡尔曼滤波(UKF)的电力系统动态状态估计展开,提供了完整的Matlab代码实现方案。重点介绍如何利用EKF与UKF算法对电力系统中的动态状态进行高精度估计,尤其适用于存在非线性特性和测量噪声的实际场景。文中详细阐述了两种滤波方法的理论基础、适用条件及在电力系统中的具体应用流程,并通过仿真验证其有效性。此外,文档还整合了多个相关科研方向的技术资源,涵盖智能优化算法、机器学习、信号处理、路径规划、电力系统优化等多个领域,突出Matlab在科研仿真中的广泛应用。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及工程技术人员,尤其适合从事电力系统状态估计、动态建模与仿真研究的相关从业者;; 使用场景及目标:①掌握EKF与UKF在非线性系统状态估计中的实现原理与差异;②应用于含新能源接入的复杂电力系统动态状态估计;③为科研项目、论文复现或算法优化提供可运行的代码基础和技术参考;; 其他说明:文档内容高度聚焦科研实用价值,强调“借力”于成熟算法与工具提升研究效率,建议结合提供的网盘资源(含YALMIP等工具包)进行代码调试与扩展应用,同时按目录顺序系统学习以避免理解断层。

70,039

社区成员

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

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