在结构中使用指针并在C中写入文件

时间:2015-03-27 02:35:48

标签: c pointers struct file-handling

我正在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-&gt;电话到达它要求地址并且没有反应。我无法弄清楚我做错了什么。我以为它是内存不足所以我改变了

struct collection *r = (struct collection *) malloc (sizeof(collection*));
 到 struct collection *r = (struct collection *) malloc (12*sizeof(collection*)); 并且它已经有一段时间了,同样的事情发生了。我正在使用devC ++进行编译。提前致谢;

4 个答案:

答案 0 :(得分:2)

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

malloc(sizeof(*collection));

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

答案 1 :(得分:1)

在没有sizeof (struct collection)

的情况下尝试*

答案 2 :(得分:1)

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

这也说明了为什么对变量和类型使用相同的名称是个坏主意。如果您使用了不同的名称,那么编译器就会收到错误。使用collection_t作为类型。

答案 3 :(得分:0)

我发现你的代码发生了很多变化。
第一个变化是 struct collection 而不是集合
第二个变化是我使用了两个文件描述符,一个用于读取另一个文件描述符。在你的代码中,你只是初始化“asd”一次,因为它每次都会在文件中写入相同的结构。所以我在do-while循环中初始化。
第四个我使用scanf而不是获取因为当你接受多个输入时它正在跳过一些输入。
第五个我删除了12个表格fwrite和fread。
第六次我又使用了一个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);    
}

和样品输出是
enter image description here