使用 read() 从文件中读取 int
我正在做一项 C 编程作业,其中只允许使用较低级别的文件 IO 命令(因此没有 fprintf()
或 fscanf()
函数调用)来读取并写入一个文件。
在代码的一个部分中,我将一个字符串写入一个 128 个字符的文件,并在它旁边写入一个整数。我可以使用 pread
来读取
字符串,但是当我再次使用 pread
读取时,出现“错误地址”错误整数:
if(pread(heap, structure->str, STRING_MAX_LEN, 0) < 0)
{
return -1;
}
else
{
printf("read str\n");
}
if(pread(file, structure->size, 1, STRING_MAX_LEN + 1) < 0)
{
return 0;
}
else
{
printf("read size\n");
}
有人知道我做错了什么吗?
I'm working on a C programming assignment where we are only allowed to use lower level file IO commands (so no fprintf()
or fscanf()
function calls) to read and write a file.
In one section of the code I write a string to a file of 128 characters and right next to it I write an integer. I am able to use pread
to read
the string of characters alright but I get a "bad address" error when I use pread
again to read the integer:
if(pread(heap, structure->str, STRING_MAX_LEN, 0) < 0)
{
return -1;
}
else
{
printf("read str\n");
}
if(pread(file, structure->size, 1, STRING_MAX_LEN + 1) < 0)
{
return 0;
}
else
{
printf("read size\n");
}
Does anybody know what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
struct->size
是int*
吗?如果它只是一个int
那么您需要使用&
传递它的地址。编辑:正如其他人所说,“1”几乎肯定不是 int 中的字节数,尽管它可能是 - 您可能应该使用
sizeof()
Is
structure->size
anint*
? If it's just anint
then you'd need to pass its address with&
.Edit: and as said by others, "1" is almost certainly not the number of bytes in your int, though it could be - you should probably use
sizeof()
尝试使用
sizeof
而不是硬编码大小。让编译器为您工作。其次,
pread
的第二个参数看起来是指向您要填充的缓冲区的指针。您正在传递 int 字段本身(按值)——通常,C 编译器非常乐意将 int 视为指针。您需要通过引用传递 int 字段,以便pread
有一个指向它应该用八位字节填充的 int 的指针。您会遇到内存访问冲突(错误地址),因为您按值传递的 int 字段的值被视为指向内存位置的地址,不允许您触摸。第一个
pread()
之所以有效,是因为(我怀疑)struct->str
是一个char*
或一个字符数组,它将被视为指针。Try using
sizeof
rather than hardcoding the size. Let the compiler work for you.Second, the 2nd argument of
pread
looks to be a pointer to the buffer you intend to fill. You're passing the int field itself (by value) — usually, the C compiler is more than happy to treat an int as a pointer. You need to pass the int field by reference, so thatpread
has a pointer to the int it's supposed to fill with octets. You get the memory access violation (bad address) because the value of int field you're passing by value, treated as an address point to a memory location, you're not allowed to touch.The first
pread()
works because (I suspect) thatstructure->str
is either achar*
or an an array of chars, which will get treated as an pointer.您传递 1 作为整数的大小。除非它是一个字符,否则你就做错了。
什么是
结构->大小
?什么类型?编辑
由于您实际上正在编写一个 int,因此您应该使用
sizeof(int)
或sizeof(struction->size)
。您还应该使用
&
运算符将地址传递给变量,而不是它的值,因此您的行将如下所示:You are passing a 1 as the size of the integer. Unless it is a char, you are doing it wrong.
What is
structure->size
? What type?EDIT
Since that you are in fact writing an int, you should use
sizeof(int)
orsizeof(structure->size)
.You should also pass the address to the variable, and not it's value, using the
&
operator, so your line would be like: