我是C新手和整体编程人员。我正在尝试创建一个简单的文件验证程序,该程序从文件中读取记录并从无效记录中排序有效。我设法实现了这一点,但在尝试返回主程序中使用的结构时遇到了问题。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define MAX 20
typedef struct data{
int ref;
int serial;
char string[MAX+1];
}vaild,invalid;
int g = 1;
int e = 1;
void read(FILE *file);
void val(struct data* t);
void create_record(struct data* temp);
void create_error(struct data* temp);
void export_data(struct data* record, struct data* error);
int main(){
FILE *file = fopen("file.txt", "r");
if (file != NULL){
read (file);
我希望能够在main函数中从这里调用结构'有效和'无效'。就像下面注释掉的printf函数一样。
//printf("%i", valid[1].ref); <<<<<<<<<< I need to be able to play around with the structures from inside the main function!
}
return 0;
}
void read(FILE *file){
struct data* t = malloc(sizeof(struct data));
char buf[1000];
while(!feof(file)){
fgets(buf, sizeof buf, file);
sscanf(buf, "%d.%d.%s", &t->ref, &t->serial, t->string);
val(t);
}
}
void val (struct data* t){
if((t->ref < 30)){
struct data* valid = (struct data*) malloc(sizeof(struct data));
valid = (struct data*)realloc(valid, g * sizeof(struct data));
valid[g-1] = *t;
if (valid == NULL){
puts("Memory allocation error!");
exit(EXIT_FAILURE);
}
printf("\nGOOD:%i.%i.%s\n", valid[g-1].ref, valid[g-1].serial, valid[g-1].string);
g++;
}
else{
struct data* invalid = (struct data*) malloc(sizeof(struct data));
invalid = (struct data*)realloc(invalid, e * sizeof(struct data));
invalid[e-1] = *t;
if ( invalid == NULL){
puts("Memory allocation error!");
exit(EXIT_FAILURE);
}
printf("\nBAD:%i.%i.%s\n", invalid[e-1].ref, invalid[e-1].serial, invalid[e-1].string);
e++;
}
}
我似乎无法通过使用返回函数从main内部调用结构,我觉得我必须做一些非常简单的错误。变得非常沮丧。
输入文件如下:
04.06.hello
09.65.test
88.55.string
27.12.qwerty
11.53.ytrewq
92.02.ecco
其他一切正常,没有编译器错误。我已将所有返回类型更改为void以使其不那么混乱,我认为我必须错误地使用它们。
答案 0 :(得分:1)
找到你的问题
void val (struct data* t){
if((t->ref < 30)){
struct data* valid = (struct data*) malloc(sizeof(struct data));
valid = (struct data*)realloc(valid, g * sizeof(struct data));
valid[g-1] = *t;
if (valid == NULL){
puts("Memory allocation error!");
exit(EXIT_FAILURE);
}
printf("\nGOOD:%i.%i.%s\n", valid[g-1].ref, valid[g-1].serial, valid[g-1].string);
g++;
}
else{
struct data* invalid = (struct data*) malloc(sizeof(struct data));
invalid = (struct data*)realloc(invalid, e * sizeof(struct data));
invalid[e-1] = *t;
if ( invalid == NULL){
puts("Memory allocation error!");
exit(EXIT_FAILURE);
}
printf("\nBAD:%i.%i.%s\n", invalid[e-1].ref, invalid[e-1].serial, invalid[e-1].string);
e++;
}
}
在上面的代码中定义有效和无效的内部If和else if,这些变量的范围在那些if条件中。所以你会得到&#34;有效的未定义&#34;错误。
试试这个
struct data* val(struct data* t){
if((t->ref < 30)){
struct data* valid_file = (struct data*) malloc(sizeof(struct data));
valid_file = (struct data*)realloc(valid_file, g * sizeof(struct data));
valid_file[g-1] = *t;
if (valid_file == NULL){
puts("Memory allocation error!");
exit(EXIT_FAILURE);
}
printf("\nGOOD:%i.%i.%s\n", valid_file[g-1].ref, valid_file[g-1].serial, valid_file[g-1].string);
g++;
return valid_file;
}
// Will come when result is invalid
struct data* invalid_file = (struct data*) malloc(sizeof(struct data));
invalid_file = (struct data*)realloc(invalid_file, e * sizeof(struct data));
invalid_file[e-1] = *t;
if ( invalid_file == NULL){
puts("Memory allocation error!");
exit(EXIT_FAILURE);
}
printf("\nBAD:%i.%i.%s\n", invalid_file[e-1].ref, invalid_file[e-1].serial, invalid_file[e-1].string);
e++;
return invalid_file;
}
注 - 将有效和无效的变量更改为不同的结构名称定义。
完整的工作示例:)
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define MAX 20
typedef struct data{
int ref;
int serial;
char string[MAX+1];
}vaild,invalid;
int g = 1;
int e = 1;
struct data* read(FILE *file);
struct data* val(struct data* t);
void create_record(struct data* temp);
void create_error(struct data* temp);
void export_data(struct data* record, struct data* error);
int main(){
FILE *file = fopen("file.txt", "r");
if (file != NULL){
struct data* answ=read (file);
printf("%i \n", answ->ref);
}
return 0;
}
struct data* read(FILE *file){
struct data* t = malloc(sizeof(struct data));
char buf[1000];
while(!feof(file)){
fgets(buf, sizeof buf, file);
sscanf(buf, "%d.%d.%s", &t->ref, &t->serial, t->string);
val(t);
}
return t;
}
struct data* val(struct data* t){
if((t->ref < 30)){
struct data* valid_file = (struct data*) malloc(sizeof(struct data));
valid_file = (struct data*)realloc(valid_file, g * sizeof(struct data));
valid_file[g-1] = *t;
if (valid_file == NULL){
puts("Memory allocation error!");
exit(EXIT_FAILURE);
}
printf("\nGOOD:%i.%i.%s\n", valid_file[g-1].ref, valid_file[g-1].serial, valid_file[g-1].string);
g++;
return valid_file;
}
// Will come when result is invalid
struct data* invalid_file = (struct data*) malloc(sizeof(struct data));
invalid_file = (struct data*)realloc(invalid_file, e * sizeof(struct data));
invalid_file[e-1] = *t;
if ( invalid_file == NULL){
puts("Memory allocation error!");
exit(EXIT_FAILURE);
}
printf("\nBAD:%i.%i.%s\n", invalid_file[e-1].ref, invalid_file[e-1].serial, invalid_file[e-1].string);
e++;
return invalid_file;
}
答案 1 :(得分:0)
当struct很小时,在C中返回结构是可以接受的。否则你最好使用指针。
下面的代码片段是一个简单的示例,向您展示如何返回结构。 同样的规则适用于返回任何其他数据类型(例如,int,double,...),返回类型需要匹配,并且函数需要具有return语句。
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int x;
int y;
} Point;
Point init_point(Point p, int x, int y) {
p.x = x;
p.y = y;
return p;
}
void main() {
Point p1, p2;
printf("%d, %d\n", p1.x, p1.y);
printf("%d, %d\n", p2.x, p2.y);
p1 = init_point(p1, 55, 10);
printf("%d, %d\n", p1.x, p1.y);
printf("%d, %d\n", p2.x, p2.y);
p2 = init_point(p2, 10000, 190);
printf("%d, %d\n", p1.x, p1.y);
printf("%d, %d\n", p2.x, p2.y);
}
结果是:
-1080464084,-1080464264
1,-1080464092
55,10
1,-1080464092
55,10
10000,190