2016-03-19 4 views
-2
struct customer information[6]; 

int count,loop; 

printf("How many records do you want to add?\n"); 
scanf("%d",&loop); 

FILE *ptr; 
ptr = fopen("information.txt","w+"); 
if(!ptr) 
{ 
    printf("file could not be opened\n"); 
    getchar(); 
    return -1; 
} 

for(count=1; count<=10; count++) 
{ 
    printf("Please enter the customer's id number:\n"); 
    scanf("%d",&information[6].idnum); 
    printf("Please enter the customer's first name and last name:\n"); 
    scanf("%s%s",information[6].Fname,information[6].Lname); 
    printf("Please enter the customer's car model type:\n"); 
    scanf("%s",information[6].cartyp); 
    printf("Please enter the customer's license plate number:\n"); 
    scanf("%s",information[6].Licnum); 
    printf("Please enter the customer's car difficulty:\n"); 
    scanf("%s",information[6].Crdffcty); 

fprintf(ptr,"%d\%s\%s\%s\%s\%s\n",information[6].idnum,information[6].Fname,   
information[6].Lname,information[6].cartyp,information[6].Licnum, 
information[6].Crdffcty); 

if(loop==count) 
{ 
    continue; 
} 
} 

fclose(ptr); 
} 

Я пытаюсь записать файл в цикле, но когда я запустил код, программа не зацикливается более одного раза. Появляется сообщение об ошибке, когда программа перестала работать, и ничего не было в созданном текстовом документе. Я попробовал некоторые предложения на этом сайте, но, похоже, что-то еще не так с кодировкой. ошибок и предупреждений нет. Может ли кто-нибудь сказать мне, что я сделал не так?Запись в файл с использованием для цикла в c программировании

+0

Неверный индекс '6'. – BLUEPIXY

+0

'struct customer information [6];' имеет 6 элементов, вы получаете доступ к элементу с индексом 6, который является седьмым элементом. –

ответ

0

У вас есть структура, определенная с помощью 6, и вы пытаетесь получить доступ к индексу 6, то есть к 7-му элементу. Итак, вы должны сделать это, предположив, что хотите последний элемент:

struct customer information[6]; 

int count,loop; 

printf("How many records do you want to add?\n"); 
scanf("%d",&loop); 

FILE *ptr; 
ptr = fopen("information.txt","w+"); 
if(!ptr) 
{ 
    printf("file could not be opened\n"); 
    getchar(); 
    return -1; 
} 

for(count=1; count<=10; count++) 
{ 
    printf("Please enter the customer's id number:\n"); 
    scanf("%d",&information[5].idnum); 
    printf("Please enter the customer's first name and last name:\n"); 
    scanf("%s%s",information[5].Fname,information[5].Lname); 
    printf("Please enter the customer's car model type:\n"); 
    scanf("%s",information[5].cartyp); 
    printf("Please enter the customer's license plate number:\n"); 
    scanf("%s",information[5].Licnum); 
    printf("Please enter the customer's car difficulty:\n"); 
    scanf("%s",information[5].Crdffcty); 

fprintf(ptr,"%d\%s\%s\%s\%s\%s\n",information[5].idnum,information[5].Fname,   
information[5].Lname,information[6].cartyp,information[5].Licnum, 
information[5].Crdffcty); 

if(loop==count) 
{ 
    continue; 
} 
} 

fclose(ptr); 
} 
Смежные вопросы