管与fwrite()函数不能写入文件的问题

C语言 码拜 8年前 (2016-05-24) 2415次浏览
在C Primer Plus里有个例子,要求把数据(一个数组)输入到一个文件中,然后要求用户随机输入一个索引,然后输出这个索引对应的值
本人照着书上代码敲了下,发现无论是输入什么所以,结果都是0.00,然后本人看了下,程序创建的那个文件用ls命令看大小是0字节。(本人系统是ubuntu14.04)

#include<stdio.h>
#include<stdlib.h>
const int LEN = 1000;
int main(void)
{
  double tarSource[LEN];
  const char* fileName = "./test.bat";
  FILE* tarFile;
  int Indexs;
  long POS;
  double targetDat;
  for(int i = 0 ; i < LEN ; ++i)   //随机生产一个double数组
    tarSource[i] = (long)i;
  if((tarFile = fopen(fileName,"wb")) == NULL)  //打开文件
  {
    printf("Open %s Fail!\n",fileName);
    exit(1);
  }
  fwrite(tarSource,sizeof(double),LEN,tarFile);  //二进制写入文件   <--问题在这里
  if(fclose(tarFile) == EOF)  //关闭文件
  {
    printf("File %s Can"t Close.\n",fileName);
    exit(2);
  }
  puts("Please Input Any Index(0-1000).");  //用户输入索引,没有进行索引范围能否合法判断
  scanf("%d",&Indexs);
  if((tarFile = fopen(fileName,"wb")) == NULL)  //打开目标文件
  {
    printf("Open %s Fail!\n",fileName);
    exit(3);
  }
  POS = (long)Indexs * sizeof(double);
  fseek(tarFile,POS,SEEK_SET);  //移动流
  fread(&targetDat,sizeof(double),1,tarFile);//输出制定所以数据
  printf("%dth Double Is %.2lf.\n",Indexs,targetDat);
  fclose(tarFile);  //关闭文件
 
  return 0;
}

原因是这个程序生产的文件test.bat文件用ls命令看大小是0字节,所以本人想问题应该是fwrite函数没写进文件中,本人想问下,为什么没写进文件里面

解决方案

40

检查fwrite返回值判断原因
http://en.cppreference.com/w/c/io/fwrite

20

“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.

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明管与fwrite()函数不能写入文件的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)