从c

时间:2015-05-29 20:44:03

标签: c arrays file pointers

我想读取以fprintf(fp,"%s %s %s\n", p->name,p->surname,p->tc);格式编写的数据。我用

创建了结构患者**
struct patients **create_array(struct patients **ptr,int length){
int i;

    ptr=(struct patients **)malloc(length*sizeof(struct patients));

return ptr;

}

上面的函数创建指针数组并将其赋予main。 Main调用read_file()函数从文件中读取以已知格式写入的数据。但是当我尝试在主打印它时,我的数据没有被填充,它打印出无意义的东西。我认为阅读数据的问题就是为什么我只放了阅读功能。我的问题是什么?欢迎所有建议。

 #include<stdio.h>

struct patients
{
    int importance;
    char name[10], surname[10], tc[11];
};

FILE *file_opening(char x[])
{
    return (fopen(x,"w+"));
}

writing_file (FILE *fp, struct patients *p)
{
    fprintf(fp,"%s %s  %s\n", p->name,p->surname,p->tc);
}

struct patients **read_file (FILE *fp,struct patients **p)
{
    int i=-1;
    do{
    i++;
    }while(fscanf(fp,"%s %s  %s",p[i]->name,p[i]->surname,p[i]->tc) !=EOF);

    return p;
}

void show_all_patients(struct patients **p, int start_index, int length){
int i;

    for(i=start_index;i<length;i++)
        printf("%s %s %s",p[i]->name,p[i]->surname,p[i]->tc);

}

struct patients **create_array(int length){

    return (struct patients **)malloc(length*sizeof(struct patients));

}


int menu(void){
    int choice;
    printf("1)take patient\n2)show all patients\n3)exit");
    scanf("%d",&choice);
    return choice;
}

main(){
int i=0,j,choice,cured_patient=0,length=1;
FILE *fp;
struct patients **ptr;
char file_name[40]="patient_list.txt";

ptr=create_array(length);
fp=file_opening(file_name);
ptr=read_file(fp,ptr);


    do{
        choice=menu();

            if(choice==1){
            printf("%s %s %s\n",ptr[i]->name,ptr[i]->surname,ptr[i]->tc);
            i++;
            }
            else if(choice==2){
                show_all_patients(ptr,i,length);
            }
    }while(choice!=3);

for(j=i;j<length;j++)
writing_file(fp,ptr[j]);

fclose(fp);
}

2 个答案:

答案 0 :(得分:0)

我会改变的事情。

  1. create_array的返回值和输入参数。

    该功能也可以简化为:

    struct patients* create_array(int length)
    {
       return malloc(length*sizeof(struct patients));
    }
    
  2. 返回read_file

    的值和参数
    struct patients* read_file (FILE *fp ,struct patients *p)
    {
        int i=-1;
        do
        {
          i++;
        } while(fscanf(fp, "%s %s  %s",p[i].name, p[i].surname, p[i].tc) != EOF);
    
        return p;
    }
    
  3. 确保提供要读取的最大字符数。否则,您最终可能会读取比阵列更能读取的内容。

    更改

        } while(fscanf(fp, "%s %s  %s",p[i].name, p[i].surname, p[i].tc) != EOF);
    

    to(给定struct中数组的大小)

        } while(fscanf(fp, "%9s %9s %10s",p[i].name, p[i].surname, p[i].tc) != EOF);
    

答案 1 :(得分:0)

代码问题:

  1. writing_file没有返回值。添加void作为返回类型。

    void writing_file (FILE *fp, struct patients *p)
    {
       fprintf(fp,"%s %s  %s\n", p->name,p->surname,p->tc);
    }
    
  2. 您需要#include <stdlib.h>

    如果不这样做,malloc的返回值将被假定为int,这可能会导致奇怪的问题。

  3. 语法错误:

    根据您对ptr的定义,行

    ptr=create_array(length);
    

    ptr=read_file(fp,ptr);
    

    语法无效。我用gcc得到以下错误:

     
    soc.c: In function ‘main’:
    soc.c:58:7: error: assignment to expression with array type
        ptr=create_array(length);
           ^
    soc.c:60:7: error: assignment to expression with array type
        ptr=read_file(fp,ptr);
    

    如果你的编译器没有报告这两行的错误,那么现在是时候使用不同的编译器了。

  4. 读取和写入同一文件

    您正在使用相同的FILE*来读取和写入。目前尚不清楚您是否打算这样做,或者您的错误。

    使用以下方式打开文件时

    return (fopen(x,"w+"));
    

    文件的内容被截断。有关详细信息,请参阅http://en.cppreference.com/w/c/io/fopen。请特别注意参数下的表格。它说:

      

    &#34; W +&#34; |写扩展|创建一个用于读/写的文件破坏内容|创造新的

    如果您只想从文件中读取数据,请使用

    return (fopen(x,"r"));
    

    如果要从文件中读取数据并将数据写回,请先使用上述模式读取,关闭文件,然后使用以下命令重新打开:

    return (fopen(x,"w"));
    
  5. 处理病人阵列

    我认为你应该使用:

    FILE* fp = NULL;
    int length=10;
    struct patients *ptr = NULL;
    
    ptr=create_array(length);
    fp=file_opening(file_name);
    read_file(fp, ptr, length);
    

    适当调整其余代码。

  6. 添加代码以释放内存

    malloc的每次通话都应该对free进行相应的调用。我会添加

    void delete_array(struct patients *ptr)
    {
       free(ptr);
    }
    

    并在函数结束前从main调用它。