使用 read() 从文件中读取 int

发布于 2024-11-19 12:25:12 字数 538 浏览 4 评论 0原文

我正在做一项 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨巷深深 2024-11-26 12:25:12

struct->sizeint* 吗?如果它只是一个 int 那么您需要使用 & 传递它的地址。

编辑:正如其他人所说,“1”几乎肯定不是 int 中的字节数,尽管它可能是 - 您可能应该使用 sizeof()

Is structure->size an int*? If it's just an int 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()

瞳孔里扚悲伤 2024-11-26 12:25:12

尝试使用 sizeof 而不是硬编码大小。让编译器为您工作。

其次, pread 的第二个参数看起来是指向您要填充的缓冲区的指针。您正在传递 int 字段本身(按值)——通常,C 编译器非常乐意将 int 视为指针。您需要通过引用传递 int 字段,以便 pread 有一个指向它应该用八位字节填充的 int 的指针。您会遇到内存访问冲突(错误地址),因为您按值传递的 int 字段的值被视为指向内存位置的地址,不允许您触摸。

第一个 pread() 之所以有效,是因为(我怀疑)struct->str 是一个 char* 或一个字符数组,它将被视为指针。

if ( pread(heap,structure->str,STRING_MAX_LEN,0) < 0 )
{
  return -1;
}
else
{
  printf("read str\n");
}

if ( pread(file,&(structure->size), sizeof(structure->size) , STRING_MAX_LEN + 1 ) < 0 )
{
  return 0 ;
}
else
{
  printf("read size\n");
}

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 that pread 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) that structure->str is either a char* or an an array of chars, which will get treated as an pointer.

if ( pread(heap,structure->str,STRING_MAX_LEN,0) < 0 )
{
  return -1;
}
else
{
  printf("read str\n");
}

if ( pread(file,&(structure->size), sizeof(structure->size) , STRING_MAX_LEN + 1 ) < 0 )
{
  return 0 ;
}
else
{
  printf("read size\n");
}
雄赳赳气昂昂 2024-11-26 12:25:12

您传递 1 作为整数的大小。除非它是一个字符,否则你就做错了。

什么是结构->大小?什么类型?

编辑

由于您实际上正在编写一个 int,因此您应该使用 sizeof(int)sizeof(struction->size)

您还应该使用 & 运算符将地址传递给变量,而不是它的值,因此您的行将如下所示:

if (pread(file, &structure->size, sizeof(structure->size), STRING_MAX_LEN + 1) < 0) { 
    return 0; 
} 
else { 
    printf("read size\n"); 
}

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) or sizeof(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:

if (pread(file, &structure->size, sizeof(structure->size), STRING_MAX_LEN + 1) < 0) { 
    return 0; 
} 
else { 
    printf("read size\n"); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文