【问题标题】:Using pointer in structure and writing it file in C在结构中使用指针并将其写入 C 文件
【发布时间】:2015-03-27 02:35:47
【问题描述】:

我正在用 C 语言完成我的大学项目,但遇到了一些问题。 我使用了指向结构的指针,并使用 fwrite 将其写入文件,但它没有帮助。这是我使用的代码。

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
struct collection{
        char *fname, *lname, *telephone, *address, *seat;   
};

collection * alloc( ){
 struct collection *r = (struct collection *) malloc (sizeof(collection*));
  r->fname = NULL;
  r->lname = NULL;
  r->telephone = NULL;
  r->address = NULL;
  r->seat = NULL;
  return (r);   
}

void string_realloc_and_copy (char **dest, const char *src){
  *dest =(char *) realloc (*dest, strlen (src) + 1);
  strcpy (*dest, src);
}

int main(){
    char ch = 'Y', temp[50];
    FILE *ptf;
    struct collection *asd;
    asd = alloc();

    //printf("%d",sizeof(asd));

    //opening file
    ptf = fopen("lang.txt","w+");
    do{   
    printf("First name: ");
    gets(temp);
    string_realloc_and_copy(&asd->fname,temp);
    printf("Last name: ");
    gets(temp);
    string_realloc_and_copy(&asd->lname,temp);
    printf("Telephone: ");
    gets(temp);
    string_realloc_and_copy(&asd->telephone,temp);
    printf("Address: ");
    gets(temp);
    string_realloc_and_copy(&asd->address,temp);
    printf("Seat you want to book: ");      
    gets(temp);
    string_realloc_and_copy(&asd->seat,temp);
    fwrite(asd,12*sizeof(collection),1,ptf);
    fflush(ptf);
    //fprintf(ptf,"\n");
    printf("Do you wish to enter another data...? (Y/N) ");
    ch = getch();
    }while((ch=toupper(ch))== 'Y');
    rewind(ptf);
        while(fread(asd,12*sizeof(collection),1,ptf) == 1){
        printf("\n\n%s",asd->fname);
        printf("\n\n%s",asd->lname);
        printf("\n\n%s",asd->telephone);
        printf("\n\n%s",asd->address);
        printf("\n\n%s",asd->seat);
    }
    fclose(ptf);    
}

它一直工作到 asd->telephone 到达它要求地址并且没有响应。我无法弄清楚我做错了什么。我以为是内存不足所以我改变了

struct collection *r = (struct collection *) malloc (sizeof(collection*));

struct collection *r = (struct collection *) malloc (12*sizeof(collection*)); 它工作了一段时间,同样的事情一次又一次地发生了。我正在使用 devC++ 进行编译。提前致谢;

【问题讨论】:

  • 顺便说一句,您可以使用 calloc(1, sizeof(collection)) 并避免将所有结构元素设置为 NULL。
  • 您对 realloc() 的使用也是错误的。第一个参数必须是指向您要重新分配和调整大小的东西的指针。在您的情况下,它总是作为 NULL 传入,因为您从未为 fname、lname 等分配任何内存。为什么不直接使用 strdup() 而不是 realloc/strcpy?
  • 你确定这是 C 而不是 C++?

标签: c pointers struct file-handling


【解决方案1】:

首先,您不应该使用gets(),因为它已被弃用,并且它从stdin 获取的字符数量没有限制。我建议你使用 fgets。您应该像这样使用 malloc 作为指针

malloc(sizeof(*collection));

这样你就可以为指针分配内存。 另一件事是用 putc() 试试这个程序,看看它是否有效。

【讨论】:

  • 它已被弃用,因为无法限制字符数会造成高度利用的安全漏洞
  • 我使用gets,因为我不知道一个名字可以有多长。所以我想我会使用gets获取输入,然后再次复制fname分配内存。
  • 这不是为最大尺寸定义宏的方式
【解决方案2】:

malloc (sizeof(collection*)) 应该是 malloc(sizeof(*collection))。您的代码只为指向 collection 类型的指针分配了足够的空间,而不是为 collection 指针指向的结构的大小。

这也说明了为什么对变量和类型使用相同的名称是个坏主意。如果你使用了不同的名字,你会从编译器中得到一个错误。类型使用collection_t

【讨论】:

  • 我知道 malloc(sizeof(*collection)) 是正确的方法,但它一直显示错误说 [Error] expected primary-expression before ')' token
  • 如果您能重写整个代码并帮助我...我将不胜感激。
【解决方案3】:

试试不带*sizeof (struct collection)

【讨论】:

  • 它可以工作,但它不会将给定的数据写入文件中我在文件中得到的所有内容都是这样的g¨ ˆ¨ Ho¨ pg¨ €g¨ gM ˆM HoM pgM €gM
【解决方案4】:


我发现你的代码有很多变化。
第一个变化是 struct collection 而不是 collection
第二个变化是我用了两个文件描述符一个用于读取另一个用于写入。
第三,在您的代码中,您只初始化“asd”一次,因此它每次都会将相同的结构写入 file 。所以我在 do-while 循环中进行了初始化。
第四个我使用 scanf 而不是gets,因为当您接受多个输入时,它会跳过一些输入。
第五个我从 fwrite 和 fread 中删除了 12 个。
我使用了第六个再一个 getchar,因为我们将在给出座位号后按 Enter,所以 ch 正在使用该输入来接受用户输入我不得不再使用一个 getchar
最后更改的代码是

   #include<stdio.h>
   #include<stdlib.h>
   #include<ctype.h>
   #include<string.h>
  //#include<conio.h>
struct collection
 {
    char *fname, *lname, *telephone, *address, *seat;   
};

struct collection  *alloc( ){
  struct collection *r= malloc(sizeof(struct collection));
  r->fname = NULL;
  r->lname = NULL;
  r->telephone = NULL;
  r->address = NULL;
  r->seat = NULL;
  return (r);   
}

void string_realloc_and_copy (char **dest, const char *src){
  *dest =(char *) realloc (*dest, strlen (src) + 1);
  strcpy (*dest, src);
}

int main(){
    char ch = 'Y', temp[50];
    FILE *ptf;
    struct collection *asd;

    //printf("%d",sizeof(asd));

//opening file
ptf = fopen("lang.txt","wb");
do{   
asd = alloc();
printf("First name: ");
scanf("%s",temp);
string_realloc_and_copy(&asd->fname,temp);
printf("Last name: ");
scanf("%s",temp);
string_realloc_and_copy(&asd->lname,temp);
printf("Telephone: ");
scanf("%s",temp);
string_realloc_and_copy(&asd->telephone,temp);
printf("Address: ");
scanf("%s",temp);
string_realloc_and_copy(&asd->address,temp);
printf("Seat you want to book: ");      
scanf("%s",temp);
string_realloc_and_copy(&asd->seat,temp);
fwrite(asd,sizeof(struct collection),1,ptf);
fflush(ptf);
//fprintf(ptf,"\n");
printf("Do you wish to enter another data...? (Y/N) ");
ch = getchar();
ch = getchar();
}while((ch=toupper(ch))== 'Y');
//rewind(ptf);
fclose(ptf);    
FILE *ptf1;
ptf1 = fopen("lang.txt","rb");
    while(fread(asd,sizeof(struct collection),1,ptf1)){
    printf("\n\n%s",asd->fname);
    printf("\n\n%s",asd->lname);
    printf("\n\n%s",asd->telephone);
    printf("\n\n%s",asd->address);
    printf("\n\n%s",asd->seat);
}
fclose(ptf1);    
}

样本输出为

【讨论】:

  • 首先,“w+”不是附加模式。我尝试使用“a+”(附加模式),只有一个文件描述符,它工作正常。
  • 您好,我不确定 devc++,this is my code 是否带有“a+”,它也包含输出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-20
  • 1970-01-01
  • 2011-03-28
  • 1970-01-01
  • 2017-04-23
  • 1970-01-01
相关资源
最近更新 更多