我为另一个主程序编写了这个头文件。该文件声明了main函数(其他地方)使用的struct_buffer结构。
我想要做的是当有人使用buffer_init函数初始化缓冲区时,会返回指向缓冲区的指针。缓冲区将包含一个指针数组,缓冲区中当前指针数(大小),以及指向缓冲区中存储指针的文件的文件指针。
主程序将调用add_to_buffer()函数来添加指向缓冲区的指针。这反过来调用buffer_dump()函数将指针转储到buffer-> fp指定的文件中。最后,我将调用buffer_close()来关闭文件。然而,编译它给了我一些我无法理解和摆脱的错误。
以下是C中的头文件代码,我正在尝试编译,它给了我无法理解的错误。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PTRS 1000
struct struct_buffer
{
int size;
File *fp;
unsigned long long ptrs[MAX_PTRS];
};
//The struct_buffer contains the size which is the number of pointers in ptrs, fp is the file pointer to the file that buffer will write
struct struct_buffer*
buffer_init(char *name)
{
struct struct_buffer *buf = malloc(sizeof(struct struct_buffer));
buf->size = 0;
buf->fp = fopen(name,"w");
return buf;
}
//This function dumps the ptrs to the file.
void
buffer_dump (struct struct_buffer *buf)
{
int ctr=0;
for(ctr=0; ctr < buf->size ; ctr++)
{
fprintf(buf->fp, "%llx\n",buf->ptrs[ctr]);
}
}
//this function adds a pointer to the buffer
void
add_to_buffer (struct struct_buffer *buf, void *ptr)
{
if(buf->size >= MAX_PTRS)
{
buffer_dump(buf);
buf->size=0;
}
buf->ptrs[(buf->size)++] = (unsigned long long)ptr;
}
//this functions closes the file pointer
void
buffer_close (struct struct_buffer *buf)
{
fclose(buf->fp);
}
以上编译代码给出了以下错误。我无法理解这个问题。请向我解释一下麻烦是什么。
buffer.h:10: error: expected specifier-qualifier-list before 'File'
buffer.h: In function 'buffer_init':
buffer.h:19: error: 'struct struct_buffer' has no member named 'fp'
buffer.h: In function 'buffer_dump':
buffer.h:29: error: 'struct struct_buffer' has no member named 'fp'
buffer.h:29: error: 'struct struct_buffer' has no member named 'ptrs'
buffer.h: In function 'add_to_buffer':
buffer.h:40: error: 'struct struct_buffer' has no member named 'ptrs'
buffer.h: In function 'buffer_close':
buffer.h:46: error: 'struct struct_buffer' has no member named 'fp'
答案 0 :(得分:1)
File
未定义。您正在寻找的正确类型是FILE
。
struct struct_buffer
{
int size;
FILE *fp; // <---------
unsigned long long ptrs[MAX_PTRS];
};
其他错误似乎是此错误的产物,因此这应该是您唯一的解决方法。