简体   繁体   English

C中的fwrite()函数未编写预期的答案

[英]fwrite() function in C does not write the expected answer

Hello, everyone! 大家好!

I found a programming exercise into an old book: 我在一本旧书中找到了编程练习:

"The content of the a.txt file is: 11 2 13 4 15 6 17 8 19 .We have the following program in C that writes something in the b.txt file: a.txt文件的内容是: 11 2 13 4 15 6 17 8 19。我们在C中具有以下程序,该程序在b.txt文件中写入内容

#include <stdio.h>

int main()
{
    FILE *f, *g;
    short int v[10];

    f = fopen("a.txt", "r");
    g = fopen("b.txt", "w");

    fread(v, 8, 1, f);
    fwrite(v, 6, 1, g);

    fclose(f); fclose(g);

    return 0;
}

I expected my program to write these numbers: 11 2 13 .But instead, it wrote: 11 2 1 我希望程序写这些数字: 11 2 13。相反,它写为: 11 2 1

I tried to see what happens with the v array with the Debugger, but the values showed there does not correspond to my calculations. 我试图查看带有调试器的v数组会发生什么,但是那里显示的值与我的计算不符。

Before the fread() is called, v is uninitialized like this: 6448 64 -108 96 6542 64 6448 64 68 0 在调用fread()之前,v会像这样被初始化: 6448 64 -108 96 6542 64 6448 64 68 0

After the fread() function completes, the v in Debug mode is: 12593 12832 12576 8243 6542 64 6448 64 68 0 fread()函数完成后,处于调试模式的v为: 12593 12832 12576 8243 6542 64 6448 64 68 0

I don't study C anymore, but I'm curious how fread() reads the numbers from a.txt and how fwrite() writes them in b.txt. 我不再学习C,但我很好奇fread()如何从a.txt读取数字以及fwrite()如何将其写入b.txt。 I know the functions prototipes and how they generally work, because I wrote some little programs using them: 我知道函数原型以及它们通常如何工作,因为我使用它们编写了一些小程序:

int fread(void *p, int dim, int n, FILE *f);
//reads n elements from f, each one having dim bytes and stores them in p

int fwrite(void *p, int dim, int n, FILE *f);
//writes n elements in f from p, each one having dim bytes

But in this particular case, I'm confused. 但是在这种情况下,我很困惑。 I even tried to change the type of my array from short int to int , but the result is the same. 我什至尝试将数组的类型从short int更改为int ,但结果是相同的。 I checked the book and the int type has 4 bytes and "short int" has 2 bytes if that helps. 我检查了这本书,如果有帮助,则int类型有4个字节,“ short int”有2个字节。

Well fwrite works as follow (from tutorialspoint ) : fwrite的工作原理如下(来自tutorialspoint ):

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) size_t fwrite(const void * ptr,size_t size,size_t nmemb,FILE * stream)

  • ptr − This is the pointer to the array of elements to be written. ptr-这是指向要写入的元素数组的指针。
  • size − This is the size in bytes of each element to be written. size-这是要写入的每个元素的大小(以字节为单位)。
  • nmemb − This is the number of elements, each one with a size of size bytes. nmemb-这是元素的数量,每个元素的大小为字节大小。
  • stream − This is the pointer to a FILE object that specifies an output stream. stream-这是指向指定输出流的FILE对象的指针。

So this line 所以这条线

fwrite(v, 6, 1, g);

Writes 6 bytes from "11 2 13 4 15 6 17 8 19". 从“ 11 2 13 4 15 6 17 8 19”中写入6个字节。 Do not forget, there are spaces in this line. 不要忘记,这一行中有空格。 So the function writes the very 6 first char (since the size, in almost every cases, of a char is 1) wich are : '1', '1', ' ', '2', ' ', '1'. 因此该函数将写入第一个6个字符(因为在几乎每种情况下,字符的大小均为1),这两个字符分别为: '1', '1', ' ', '2', ' ', '1'.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM