C中的read()错误
我写了一个玩具程序来学习C中的文件I/O,下面是我代码的一部分,这个程序的输出是错误的——我不知道我的代码中有什么错误C中的read()错误,c,file,io,C,File,Io,我写了一个玩具程序来学习C中的文件I/O,下面是我代码的一部分,这个程序的输出是错误的——我不知道我的代码中有什么错误 int fd = open(path_to_file, O_RDWR | O_APPEND | O_CREAT, 0777); int size = 100; int offset = 0; write(fd, &size, sizeof(int)); write(fd, &offset, sizeof(int)); lseek(fd, 0, SEEK_SET
int fd = open(path_to_file, O_RDWR | O_APPEND | O_CREAT, 0777);
int size = 100;
int offset = 0;
write(fd, &size, sizeof(int));
write(fd, &offset, sizeof(int));
lseek(fd, 0, SEEK_SET); /* start reading from the beginning */
int *size = malloc(sizeof(int));
int *offset = malloc(sizeof(int));
read(fd, size, sizeof(int));
read(fd, offset, sizeof(int));
printf("size is %d, offset is %d \n", *size, *offset);
free(size);
free(offset);
我得到的结果是:
大小为1953719668,偏移量为1684497779
这些数字太大了,但它们应该分别是100和0。我不明白这是怎么发生的。有人能帮助理解这一点吗 Ingo拥有它的权利,如果该文件已经存在,您可以附加到它…
Mallocing for 1 single int不是很有效,但应该可以工作。
我猜您复制并粘贴了代码,变量上的相同名称阻止了代码的编译。
即使文件存在,工作示例:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
int fd = open("test.dat", O_RDWR | O_CREAT, 0777);
int size = 100;
int offset = 0;
write(fd, &size, sizeof(int));
write(fd, &offset, sizeof(int));
lseek(fd, 0, SEEK_SET); /* start reading from the beginning */
int *result_size = malloc(sizeof(int));
int *result_offset = malloc(sizeof(int));
read(fd, result_size, sizeof(int));
read(fd, result_offset, sizeof(int));
printf("size is %d, offset is %d \n", *result_size, *result_offset);
free(result_size);
free(result_offset);
}
#包括
#包括
#包括
#包括
#包括
int main(){
int fd=开放(“test.dat”,O|RDWR | O|u CREAT,0777);
int size=100;
整数偏移=0;
写入(fd和size,sizeof(int));
写入(fd和offset,sizeof(int));
lseek(fd,0,SEEK_SET);/*从头开始读取*/
int*result_size=malloc(sizeof(int));
int*result_offset=malloc(sizeof(int));
读取(fd、结果大小、大小(int));
读取(fd,结果_偏移量,sizeof(int));
printf(“大小为%d,偏移量为%d\n”,*结果大小,*结果偏移量);
免费(结果大小);
自由(结果_偏移);
}
Ingo拥有它的权利,如果该文件已经存在,您可以附加到它…Mallocing for 1 single int不是很有效,但应该可以工作。
我猜您复制并粘贴了代码,变量上的相同名称阻止了代码的编译。
即使文件存在,工作示例:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
int fd = open("test.dat", O_RDWR | O_CREAT, 0777);
int size = 100;
int offset = 0;
write(fd, &size, sizeof(int));
write(fd, &offset, sizeof(int));
lseek(fd, 0, SEEK_SET); /* start reading from the beginning */
int *result_size = malloc(sizeof(int));
int *result_offset = malloc(sizeof(int));
read(fd, result_size, sizeof(int));
read(fd, result_offset, sizeof(int));
printf("size is %d, offset is %d \n", *result_size, *result_offset);
free(result_size);
free(result_offset);
}
#包括
#包括
#包括
#包括
#包括
int main(){
int fd=开放(“test.dat”,O|RDWR | O|u CREAT,0777);
int size=100;
整数偏移=0;
写入(fd和size,sizeof(int));
写入(fd和offset,sizeof(int));
lseek(fd,0,SEEK_SET);/*从头开始读取*/
int*result_size=malloc(sizeof(int));
int*result_offset=malloc(sizeof(int));
读取(fd、结果大小、大小(int));
读取(fd,结果_偏移量,sizeof(int));
printf(“大小为%d,偏移量为%d\n”,*结果大小,*结果偏移量);
免费(结果大小);
自由(结果_偏移);
}
Ingo拥有它的权利,如果该文件已经存在,您可以附加到它…Mallocing for 1 single int不是很有效,但应该可以工作。
我猜您复制并粘贴了代码,变量上的相同名称阻止了代码的编译。
即使文件存在,工作示例:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
int fd = open("test.dat", O_RDWR | O_CREAT, 0777);
int size = 100;
int offset = 0;
write(fd, &size, sizeof(int));
write(fd, &offset, sizeof(int));
lseek(fd, 0, SEEK_SET); /* start reading from the beginning */
int *result_size = malloc(sizeof(int));
int *result_offset = malloc(sizeof(int));
read(fd, result_size, sizeof(int));
read(fd, result_offset, sizeof(int));
printf("size is %d, offset is %d \n", *result_size, *result_offset);
free(result_size);
free(result_offset);
}
#包括
#包括
#包括
#包括
#包括
int main(){
int fd=开放(“test.dat”,O|RDWR | O|u CREAT,0777);
int size=100;
整数偏移=0;
写入(fd和size,sizeof(int));
写入(fd和offset,sizeof(int));
lseek(fd,0,SEEK_SET);/*从头开始读取*/
int*result_size=malloc(sizeof(int));
int*result_offset=malloc(sizeof(int));
读取(fd、结果大小、大小(int));
读取(fd,结果_偏移量,sizeof(int));
printf(“大小为%d,偏移量为%d\n”,*结果大小,*结果偏移量);
免费(结果大小);
自由(结果_偏移);
}
Ingo拥有它的权利,如果该文件已经存在,您可以附加到它…Mallocing for 1 single int不是很有效,但应该可以工作。
我猜您复制并粘贴了代码,变量上的相同名称阻止了代码的编译。
即使文件存在,工作示例:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
int fd = open("test.dat", O_RDWR | O_CREAT, 0777);
int size = 100;
int offset = 0;
write(fd, &size, sizeof(int));
write(fd, &offset, sizeof(int));
lseek(fd, 0, SEEK_SET); /* start reading from the beginning */
int *result_size = malloc(sizeof(int));
int *result_offset = malloc(sizeof(int));
read(fd, result_size, sizeof(int));
read(fd, result_offset, sizeof(int));
printf("size is %d, offset is %d \n", *result_size, *result_offset);
free(result_size);
free(result_offset);
}
#包括
#包括
#包括
#包括
#包括
int main(){
int fd=开放(“test.dat”,O|RDWR | O|u CREAT,0777);
int size=100;
整数偏移=0;
写入(fd和size,sizeof(int));
写入(fd和offset,sizeof(int));
lseek(fd,0,SEEK_SET);/*从头开始读取*/
int*result_size=malloc(sizeof(int));
int*result_offset=malloc(sizeof(int));
读取(fd、结果大小、大小(int));
读取(fd,结果_偏移量,sizeof(int));
printf(“大小为%d,偏移量为%d\n”,*结果大小,*结果偏移量);
免费(结果大小);
自由(结果_偏移);
}
阅读之前是否需要将数据刷新到文件中?为什么要malloc
这两个int?不要malloc一个int。这是毫无意义的。如果需要传递int的地址,请对实际int使用&
运算符。我是否应该将变量大小和偏移量放在堆栈上?谢谢你确定,文件的路径
不存在吗?如果是这样的话,你就不会写到文件的开头,在那里你已经读过了。你需要在读之前将数据刷新到文件中吗?为什么你要malloc
这两个int?不要malloc一个int。这是毫无意义的。如果需要传递int的地址,请对实际int使用&
运算符。我是否应该将变量大小和偏移量放在堆栈上?谢谢你确定,文件的路径
不存在吗?如果是这样的话,你就不会写到文件的开头,在那里你已经读过了。你需要在读之前将数据刷新到文件中吗?为什么你要malloc
这两个int?不要malloc一个int。这是毫无意义的。如果需要传递int的地址,请对实际int使用&
运算符。我是否应该将变量大小和偏移量放在堆栈上?谢谢你确定,文件的路径
不存在吗?如果是这样的话,你就不会写到文件的开头,在那里你已经读过了。你需要在读之前将数据刷新到文件中吗?为什么你要malloc
这两个int?不要malloc一个int。这是毫无意义的。如果需要传递int的地址,请对实际int使用&
运算符。我是否应该将变量大小和偏移量放在堆栈上?谢谢你确定,文件的路径
不存在吗?如果有,您就不会写入文件的开头,在那里您已经读过了