结构作为外部函数 C 的参数
Structure as a parameter of extern function C
我必须使用这种结构读取文本文件。另外,我必须使用外部函数。我写了读取文件的代码,它在主函数中工作。
文本文件:
banana 3 orange 8 music 9
- 第一个字符为空格 space*
#include <stdio.h>
#include <stdlib.h>
struct file
{
char name[30];
char size;
};
int main()
{
int n=0;
struct file f[30];
FILE *files;
files=fopen("files.txt","r");
int n=0;
while (1)
{
fgetc(files);
if(feof(files)) break;
fscanf(files,"%s %c",&f[n].name,&f[n].size);
n++;
}
}
但是当我尝试使用另一个 c 文件和外部函数进行读取时,它不起作用..:(
这是filereading.c里面写的:
void fileReading(struct file *f[30], FILE *files)
{
int n=0;
while (1)
{
fgetc(files);
if(feof(files)) break;
fscanf(files,"%s %c",&f[n].name,&f[n].size);
n++;
}
}
和fileReading.h:
void fileReading(struct fisier *, FILE *);
在main.c中:
#include <stdio.h>
#include <stdlib.h>
struct file
{
char name[30];
char size;
};
int main()
{
int n=0;
struct file f[30];
FILE *files;
files=fopen("files.txt","r");
fileReading(f[30],files);
}
当我编译它时,它说:
request for member 'name' in something not a structure or union
request for member 'size' in something not a structure or union|
||=== Build finished: 2 errors, 2 warnings (0 minutes, 0 seconds) ===||
你能帮帮我吗?谢谢!
文件 fileReading.c 不知道 struct file
的定义。您需要在 main.c 和 fileReading.c.
中将其从 main.c 移动到 fileReading.h 和 #include "fileReading.h"
另外,fileReading的定义和调用不正确。而不是:
void fileReading(struct file *f[30], FILE *files)
你想要:
void fileReading(struct file *f, FILE *files)
你这样称呼它:
fileReading(f,files);
这是不正确的:
fileReading(f[30],files);
因为您传递的是单个 struct file
而不是数组,并且您传递的单个实例是数组末尾的一个元素(因为大小为 30,有效索引为 0 -29),这会导致不确定的行为。
据我所知,您似乎对指针没有很好的理解。
这些更改应该可以解决您的问题:
void fileReading(struct file *f, FILE *files)
{
int n=0;
while (1)
{
fgetc(files);
if(feof(files)) break;
fscanf(files,"%s %c",f[n].name,&f[n].size);
//printf("%s %c",f[n].name,f[n].size);
n++;
}
}
int main()
{
int n=0;
struct file f[30];
FILE *files;
files=fopen("files.txt","r");
fileReading(f,files);
}
你做错了什么:
void fileReading(struct file *f[30], FILE *files) //here you were saying file is a **
fscanf(files,"%s %c",&f[n].name,&f[n].size); // here you need to send the a char* but you were sending a char ** as a second parameter
fileReading(f[30],files); // here you were sending the 31th element of the structure array f which by the way doesn't exist (indexing is from 0 , f[29] is the last) even though that was not what you wanted to do in the first place
我必须使用这种结构读取文本文件。另外,我必须使用外部函数。我写了读取文件的代码,它在主函数中工作。
文本文件:
banana 3 orange 8 music 9
- 第一个字符为空格 space*
#include <stdio.h>
#include <stdlib.h>
struct file
{
char name[30];
char size;
};
int main()
{
int n=0;
struct file f[30];
FILE *files;
files=fopen("files.txt","r");
int n=0;
while (1)
{
fgetc(files);
if(feof(files)) break;
fscanf(files,"%s %c",&f[n].name,&f[n].size);
n++;
}
}
但是当我尝试使用另一个 c 文件和外部函数进行读取时,它不起作用..:(
这是filereading.c里面写的:
void fileReading(struct file *f[30], FILE *files)
{
int n=0;
while (1)
{
fgetc(files);
if(feof(files)) break;
fscanf(files,"%s %c",&f[n].name,&f[n].size);
n++;
}
}
和fileReading.h:
void fileReading(struct fisier *, FILE *);
在main.c中:
#include <stdio.h>
#include <stdlib.h>
struct file
{
char name[30];
char size;
};
int main()
{
int n=0;
struct file f[30];
FILE *files;
files=fopen("files.txt","r");
fileReading(f[30],files);
}
当我编译它时,它说:
request for member 'name' in something not a structure or union
request for member 'size' in something not a structure or union|
||=== Build finished: 2 errors, 2 warnings (0 minutes, 0 seconds) ===||
你能帮帮我吗?谢谢!
文件 fileReading.c 不知道 struct file
的定义。您需要在 main.c 和 fileReading.c.
#include "fileReading.h"
另外,fileReading的定义和调用不正确。而不是:
void fileReading(struct file *f[30], FILE *files)
你想要:
void fileReading(struct file *f, FILE *files)
你这样称呼它:
fileReading(f,files);
这是不正确的:
fileReading(f[30],files);
因为您传递的是单个 struct file
而不是数组,并且您传递的单个实例是数组末尾的一个元素(因为大小为 30,有效索引为 0 -29),这会导致不确定的行为。
据我所知,您似乎对指针没有很好的理解。 这些更改应该可以解决您的问题:
void fileReading(struct file *f, FILE *files)
{
int n=0;
while (1)
{
fgetc(files);
if(feof(files)) break;
fscanf(files,"%s %c",f[n].name,&f[n].size);
//printf("%s %c",f[n].name,f[n].size);
n++;
}
}
int main()
{
int n=0;
struct file f[30];
FILE *files;
files=fopen("files.txt","r");
fileReading(f,files);
}
你做错了什么:
void fileReading(struct file *f[30], FILE *files) //here you were saying file is a **
fscanf(files,"%s %c",&f[n].name,&f[n].size); // here you need to send the a char* but you were sending a char ** as a second parameter
fileReading(f[30],files); // here you were sending the 31th element of the structure array f which by the way doesn't exist (indexing is from 0 , f[29] is the last) even though that was not what you wanted to do in the first place