2016-11-14 15 views
2

私はあなたの所有ペットの数を最初に尋ね、次に各ペットの名前と年齢を構造体(すべてリンクリストを使用)に保存するこのプログラムを作成しました。リンクリストのデータをC言語のtxtファイルに書き込む

私の質問は:手順writeToFile()を使用してデータを.txtファイルに書き込もうとしていますが、実行時に.txtファイルにデータが含まれていません。なぜ私は理解できないのですか?

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

struct Node { 
    char *name; 
    int age; 
    struct Node *next; 
}; 

struct Node * petRecord; 
struct Node * newRecord; 

void printPetRecord() 
{ 
    while(petRecord != NULL) 
    { 
     printf("Name of Pet: %s\n", petRecord->name); 
     printf("Age of Pet: %d\n", petRecord->age); 
     petRecord = petRecord->next; 
    } 
} 

void writeToFile() 
{ 
    FILE * fptr; 
    fptr = fopen("petnames.txt", "w"); 

    if(fptr==NULL) 
    { 
     printf("Error\n"); 
    } 

    else 
    { 
     while(petRecord != NULL) 
     { 
      fprintf(fptr, "\nPet Name: %s\nAge: %d\n", petRecord->name, petRecord->age); 
      petRecord = petRecord->next; 
     } 
    } 

    fclose(fptr); 
    } 

int main() 
{ 
    int count, i; 
    printf("How many pets do you have? "); 
    scanf("%d", &count); 

    for(i=0; i<count; i++) 
    { 
     if(i==0) 
     { 
      petRecord = malloc(sizeof(struct Node)); 
      newRecord = petRecord; 
     } 
     else 
     { 
      newRecord->next = malloc(sizeof(struct Node)); 
      newRecord = newRecord->next; 
     } 
     newRecord->name = malloc(50*sizeof(char)); 
     printf("Name of Pet: "); 
     scanf("%s", newRecord->name); 
     printf("Age of Pet: "); 
     scanf("%d", &newRecord->age); 
    } 
    newRecord->next = NULL; 
    printf("\n\n"); 
    printPetRecord(); 
    writeToFile(); 
} 
+1

があなたの代わりに標準出力にそれを印刷しようとしたことがありますか? –

+0

グローバル変数は使用しないでください。 –

+0

@Katrina:グローバル変数 – developer

答えて

3

あなたの機能printPetRecord()はnullに設定にポインタを残します:

は、これは私のコードです。

struct Node * iterator = petRecord; 

をしてからitertaeはイテレータを使用して:

インサイドprintPetRecord()このようなものを作ります。

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

struct Node { 
    char *name; 
    int age; 
    struct Node *next; 
}; 

struct Node * petRecord; 
struct Node * newRecord; 

void printPetRecord() 
{ 
    struct Node * iterator = petRecord; 
    while(iterator != NULL) 
    { 
     printf("Name of Pet: %s\n", iterator->name); 
     printf("Age of Pet: %d\n", iterator->age); 
     iterator=iterator->next; 
    } 
} 

void writeToFile() 
{ 
    FILE * fptr; 
    fptr = fopen("petnames.txt", "w"); 
    struct Node * iterator = petRecord; 

    if(fptr==NULL) 
    { 
     printf("Error\n"); 
    } 

    else 
    { 
     while(iterator!= NULL) 
     { 
      fprintf(fptr, "\nPet Name: %s\nAge: %d\n", iterator->name, iterator->age); 
      iterator= iterator->next; 
     } 
    } 

    fclose(fptr); 
    } 

int main() 
{ 
    int count, i; 
    printf("How many pets do you have? "); 
    scanf("%d", &count); 

    for(i=0; i<count; i++) 
    { 
     if(i==0) 
     { 
      petRecord = malloc(sizeof(struct Node)); 
      newRecord = petRecord; 
     } 
     else 
     { 
      newRecord->next = malloc(sizeof(struct Node)); 
      newRecord = newRecord->next; 
     } 
     newRecord->name = malloc(50*sizeof(char)); 
     printf("Name of Pet: "); 
     scanf("%s", newRecord->name); 
     printf("Age of Pet: "); 
     scanf("%d", &newRecord->age); 
    } 
    newRecord->next = NULL; 
    printf("\n\n"); 
    printPetRecord(); 
    writeToFile(); 
} 

実行:

> gcc -o main main.c 
> ./main 
How many pets do you have? 2 
Name of Pet: a 
Age of Pet: 2 
Name of Pet: b 
Age of Pet: 3 


Name of Pet: a 
Age of Pet: 2 
Name of Pet: b 
Age of Pet: 3 
> cat petnames.txt 

Pet Name: a 
Age: 2 

Pet Name: b 
Age: 3 
+3

で直接反復処理をすると、リストの頭が失われました。グローバル変数が悪いことがもう一度示されます。 –

+0

この[struct Node * iterator = petRecord]を実行すると、私はリンクされたリストの最初の値のアドレスを指す新しいNodeを作成します。したがって、データをtxtファイルに書き込むには、while(iterator!= NULL){ \t fprintf(fptr、 "\ nPet Name:%s \ nAge:%d \ n"、iterator-> name、イテレータ - >年齢); \t iterator = iterator-> next; }まだ私のファイルにはまだデータがありません:/ – Katrina

+0

私のために働く:) – mko

関連する問題