英文:
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:
-
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. -
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 <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:
-
I tried modifying 'write' function as
write(fileHandle, &elements, 10 * sizeof(unsigned long));
, but it didn't work. The same goes for 'read' function. -
I tried '%lu' as format specifier, still didn't work
答案1
得分: 0
如@yeputons在评论中提到的,您需要在读取数据之前重置文件句柄,例如使用lseek
。
这段代码看起来可能是一个练习,您正在练习使用系统调用。在这种情况下,您显然必须使用您正在学习的调用。否则,您可能应该使用fwrite
/fread
或C++流。
有关write
和fwrite
之间的区别的信息,请参阅问题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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论