Using ‘read’系统调用函数从文件描述符检索数据会导致垃圾值。

huangapple go评论146阅读模式
英文:

Retrieving data from a file descriptor using 'read' system call function results in garbage value

问题

I am doing Linux system programming using Visual Studio and deploying it to VirtualBox VM. Here, I have created an array of 10 unsigned long elements with the value of i*i. Then, I wrote the array to 'sample.bin' file. However, while retrieving the array, I get garbage values or zeros.

#include <cstdio>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main() {

    unsigned long elements[10];
    for (int i = 0; i < 10; i++) {
        elements[i] = (unsigned long)(i * i);
    }

    int fileHandle = open("sample.bin", O_CREAT | O_TRUNC | O_RDWR | O_APPEND, 0777);

    int dataToFile = write(fileHandle, elements, 10 * sizeof(unsigned long));
    
    unsigned long readBuffer[10];
    
    int dataFromFile = read(fileHandle, readBuffer, 10 * sizeof(unsigned long));
    
    for (int i = 0; i < 10; i++) {
        printf("%d\t", readBuffer[i]);
    }

    return 0;
}

I tried the following:

  1. I tried modifying the 'write' function as write(fileHandle, &elements, 10 * sizeof(unsigned long));, but it didn't work. The same goes for the 'read' function.

  2. I tried using '%lu' as the format specifier, but it still didn't work.

英文:

I am doing Linux system programming using Visual Studio and deploying it to VirtualBox VM. Here, I have create an array of 10 unsigned long element of i*i value. Then, I wrote the array to 'sample.bin' file. However, while retrieving the array, I get garbage values or zero.

#include &lt;cstdio&gt;
#include &lt;sys/stat.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;unistd.h&gt;

int main() {

    unsigned long elements[10];
    for (int i = 0; i &lt; 10; i++) {
        elements[i] = (unsigned long)(i * i);
    }

    int fileHandle = open(&quot;sample.bin&quot;, O_CREAT | O_TRUNC | O_RDWR | O_APPEND, 0777);

    int dataToFile = write(fileHandle, elements, 10 * sizeof(unsigned long));
    
    unsigned long readBuffer[10];
    
    int dataFromFile = read(fileHandle, readBuffer, 10 * sizeof(unsigned long));
    
    for (int i = 0; i &lt; 10; i++) {

        printf(&quot;%d\t&quot;, readBuffer[i]);

    }

    return 0;
}

I tried the following:

  1. I tried modifying 'write' function as write(fileHandle, &amp;elements, 10 * sizeof(unsigned long));, but it didn't work. The same goes for 'read' function.

  2. I tried '%lu' as format specifier, still didn't work

答案1

得分: 0

如@yeputons在评论中提到的,您需要在读取数据之前重置文件句柄,例如使用lseek

这段代码看起来可能是一个练习,您正在练习使用系统调用。在这种情况下,您显然必须使用您正在学习的调用。否则,您可能应该使用fwrite/fread或C++流。

有关writefwrite之间的区别的信息,请参阅问题https://stackoverflow.com/questions/11414191/what-are-the-main-differences-between-fwrite-and-write。

英文:

As @yeputons mentioned in a comment, you will need to reset the file handle before you read the data. E.g. by using lseek.

The code looks like it might be an exercise, where you are practicing using the system calls. In that case, you obviously have to use the calls you are learning to use. Otherwise, you should probably use fwrite/fread or C++ streams instead.

See the question https://stackoverflow.com/questions/11414191/what-are-the-main-differences-between-fwrite-and-write for information about the difference between write and fwrite.

huangapple
  • 本文由 发表于 2023年5月15日 05:09:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定