Reputation: 127
I am trying to read data from a text file with some data in it.
My text file:
1 dior Asfiya 20 abcde 12345
2 abde Sabah 17 saar 5757657
My code:
typedef long long int ulong;
struct Customer
{
char name[25];
int acc_no;
int age;
char gender[2];
char address[60];
ulong phone;
char password[10];
};
struct Customer add,check;
int new_acc()
{
int c;
FILE *p;
FILE *q;
p = fopen("test.txt","a");
q = fopen("test.txt","r");
if(p == NULL)
{
puts("Could't open file\n");
return(-1);
}
if(q == NULL)
{
puts("Couldn't open file\n");
return(-1);
}
account_no:
printf("\nAdd record");
printf("\nEnter account number: ");
scanf("%d",&check.acc_no);
while(c = fscanf(q,"%d %s %s %d %s %s %llu%[^\n]",&add.acc_no,add.password,add.name,&add.age,add.gender,add.address,&add.phone) == 7)
{
if(check.acc_no==add.acc_no)
{
printf("\nAccount no. already in use!");
goto account_no;
}
}
if(c != EOF){
goto details;
}
details:
add.acc_no = check.acc_no;
printf("\nEnter password: ");
scanf("%s",add.password);
printf("\nFull Name: ");
scanf("%s",add.name);
printf("\nEnter your age: ");
scanf("%d",&add.age);
printf("\nEnter your gender: ");
scanf(" %c",&add.gender);
printf("\nEnter address: ");
scanf("%s",add.address);
printf("\nEnter phone number: ");
scanf("%llu",&add.phone);
fprintf(p,"%d %s %s %d %s %s %lld\n",add.acc_no,add.password,add.name,add.age,add.gender,add.address,add.phone);
fclose(p);
fclose(q);
}
While debugging the code it gives segmentation fault when the while loop starts. It worked perfectly fine before adding the first line of data into the file. I am not able to figure out why is this happening.
Upvotes: 1
Views: 131
Reputation: 73366
goto
.Refactor this part of your code please.
About your arrays, which await to store a string - did you take in account the string NULL terminator, that also needs a cell of the array to be stored?
In any case, I see gender
has a size of 2, but your input suggests that it needs more.
Your input has 7 tokens:
1 dior Asfiya 20 abcde 12345
that means that gender
will overflow, invoking Undefined Behavior (UB), since you will attempt to store a 4-character length string into an array of size 2. That might explain the crash.
You would have seen it by your check for 7 values, but there is also another problem in your code, as you can read in the next section.
You probably meant:
1 dior Asfiya 20 abcde f 12345
Change this:
while(c = fscanf(q,"%d %s %s %d %s %s %llu%[^\n]",&add.acc_no,add.password,add.name,&add.age,add.gender,add.address,&add.phone) == 7)
to this:
while((c = fscanf(q,"%d %s %s %d %s %s %llu%[^\n]",&add.acc_no,add.password,add.name,&add.age,add.gender,add.address,&add.phone)) == 7)
since, because of operators priority, the comparison happens before the assignment, which is not what you want. You want first to assign to your variable, and then compare, so use parentheses to specify the desired priority.
Upvotes: 2
Reputation: 780724
You're not setting c
correctly when you write:
while(c = fscanf(q,"%d %s %s %d %s %s %llu%[^\n]",&add.acc_no,add.password,add.name,&add.age,add.gender,add.address,&add.phone) == 7)
This is setting c
to the result of the ==
comparison, because ==
has higher precedence than =
.
Also, you don't need [^\n]
at the end of the format string. %d
at the beginning will skip over any whitespace, including the newline at the end of the previous line.
It should be:
while((c = fscanf(q,"%d %s %s %d %s %s %llu",&add.acc_no,add.password,add.name,&add.age,add.gender,add.address,&add.phone)) == 7)
I'm not sure what you intend with
if(c != EOF){
goto details;
}
details:
Since details:
is immediately after the if
statement, you go there whether or not the condition succeeds.
You should get out of the habit of using goto
, it's generally considered poor programming style. Put the code that should run when the condition is true inside the if
. You can use continue
and break
to restart and end a loop.
Upvotes: 2