关于fwrite写按照文本模式打开的数组写和读的问题

来源:百度知道 编辑:UC知道 时间:2024/06/07 07:58:10
利用fwrite把一个双精度浮点型数组写入一个文件,这个文件采用w模式和r模式写和读,然后在依次利用fread读出数组中的每个数,在标准输出流中显示。
#define ARSIZE 1000

int main()

{

double numbers[ARSIZE];

double value;

const char * file = "numbers.dat";

int i;

long pos;

FILE *iofile;

// create a set of double values

for(i = 0; i < ARSIZE; i++)

numbers[i] = 100.0 * i + 1.0 / (i + 1);

// attempt to open file

if ((iofile = fopen(file, "wb")) == NULL)

{

fprintf(stderr, "Could not open %s for output.\n", file);

exit(1);

}

// write array in binary format to file

fwrite(numbers, sizeof (double), ARSIZE, iofile);

fclose(iofile);

if ((iofile = fopen(file, "rb")) == NULL)

{

应该是编译器的问题,我在GCC中编译并执行没有任何问题。这是后几行:

The value there is 97800.001021.
The value there is 97900.001020.
The value there is 98000.001019.
The value there is 98100.001018.
The value there is 98200.001017.
The value there is 98300.001016.
The value there is 98400.001015.
The value there is 98500.001014.
The value there is 98600.001013.
The value there is 98700.001012.
The value there is 98800.001011.
The value there is 98900.001010.
The value there is 99000.001009.
The value there is 99100.001008.
The value there is 99200.001007.
The value there is 99300.001006.
The value there is 99400.001005.
The value there is 99500.001004.
The value there is 99600.001003.
The value there is 99700.001002.
The value there is 99800.001001.
The value there is 99900.001000.
Bye!

把 double value;
改为全局量试试.
也就是放到
int main() 之前.

频繁读写 while ( fread(&value, ...<