函数如何返回自定义结构?

时间:2020-05-05 20:28:20

标签: c

我试图扫描函数内部的输入文件,而不是扫描main函数中的输入文件。但似乎这种方式行不通。 我想这是输入文件指针的问题,我对使用指针不是很熟悉

#include<stdio.h>

typedef struct 
{
    char firstname[32],lastname[32];
    int age;
}mandef;

mandef scaninput(int rows,FILE *inputfile)
{
    int i;
    mandef man[rows];
    for (i = 0; i < rows; i ++)
    {
        fscanf(inputfile,"%s",&man[i].firstname);
        fscanf(inputfile,"%s",&man[i].lastname);
        fscanf(inputfile,"%d",&man[i].age);
    }
    return man;
}

int main ()
{
    FILE *inputfile;
    inputfile = fopen("input.txt","r");
    if (inputfile == NULL)
    {
        printf("Error: Unable to open input.txt.");
        return(1);
    }
    int i,rows;
    char x;
    fscanf(inputfile,"%d",&rows);
    mandef man[rows];
    man = scaninput(rows,inputfile);
    return 0;
}

我希望代码的工作方式与此代码相同

#include<stdio.h>
typedef struct 
{
    char firstname[32],lastname[32];
    int age;
}mandef;

int main ()
{
    FILE *inputfile;
    inputfile = fopen("input.txt","r");
    if (inputfile == NULL)
    {
        printf("Error: Unable to open input.txt.");
        return(1);
    }
    int i,rows;
    fscanf(inputfile,"%d",&rows);
    mandef man[rows];
    for (i = 0; i < rows; i ++)
    {
        fscanf(inputfile,"%s",&man[i].firstname);
        fscanf(inputfile,"%s",&man[i].lastname);
        fscanf(inputfile,"%d",&man[i].age);
    }
    return 0;
}

input.txt看起来像

2
Mike Abc 18
David Abc 17

2 个答案:

答案 0 :(得分:1)

如果合适的话,我建议您找出指针并使用malloc为数组动态分配内存。我在下面对您的代码进行了一些快速更改,虽然还不完美,但应该可以为您提供总体思路。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct mandef
{
    char firstname[32];
    char lastname[32];
    int age;
} mandef;

mandef *scaninput(int rows,FILE *inputfile)
{
    int i;
    mandef *man = malloc(sizeof(mandef) * rows);
    bzero(man, (sizeof(mandef) * rows));

    for (i = 0; i < rows; i++)
    {
        fscanf(inputfile,"%31s",man[i].firstname);
        fscanf(inputfile,"%31s",man[i].lastname);
        fscanf(inputfile,"%d",&man[i].age);
    }
    return man;
}

int main ()
{
    FILE *inputfile = fopen("input.txt","r");

    if (inputfile == NULL)
    {
        printf("Error: Unable to open input.txt.");
        return(1);
    }

    int rows=0;

    fscanf(inputfile,"%d",&rows);
    mandef *man = scaninput(rows,inputfile);
    if (rows > 0) 
    {
        printf("man[0].firstname is %s\n", man[0].firstname);
    }
    return 0;
}

答案 1 :(得分:1)

您返回局部变量,这是一个坏主意,因为函数终止后,该变量可能不存在。因此,您应该改用指针:

mandef * man = malloc(sizeof(mandef) * rows);
if(!mandef) {
   // handle the error
}

请勿使用&扫描字符串,它应为:

        fscanf(inputfile,"%s",man[i].firstname);
        fscanf(inputfile,"%s",man[i].lastname);
        fscanf(inputfile,"%d",&man[i].age);

测试:

#include<stdio.h>
#include <stdlib.h>

typedef struct 
{
    char firstname[32],lastname[32];
    int age;
}mandef;

mandef * scaninput(int rows,FILE *inputfile)
{
    int i;
    mandef * man = malloc(sizeof(mandef) * rows);
    if (!man) {
        return NULL;
    }
    for (i = 0; i < rows; i ++)
    {
        fscanf(inputfile,"%s",man[i].firstname);
        fscanf(inputfile,"%s",man[i].lastname);
        fscanf(inputfile,"%d",&man[i].age);
    }
    return man;
}

int main ()
{
    FILE *inputfile;
    inputfile = fopen("input.txt","r");
    if (inputfile == NULL)
    {
        printf("Error: Unable to open input.txt.");
        return(1);
    }
    int i,rows;
    char x;
    fscanf(inputfile,"%d",&rows);
    if(rows <= 0)
       return -1;
    mandef * man;
    man = scaninput(rows,inputfile);
    for (i = 0; i < rows; i ++)
    {
        printf("%s\n",man[i].firstname);
        printf("%s\n",man[i].lastname);
        printf("%d\n",man[i].age);
    }
    free(man);
    return 0;
}

输出:

Mike                                                                                                                                            
Abc                                                                                                                                             
18                                                                                                                                              
David                                                                                                                                           
Abc                                                                                                                                             
17
相关问题