Here’s my code:
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("text.txt","r");
if(fp==NULL)
printf("ahaha");
struct karan{
int index;
int number;
char string[10];
};
struct karan first;
fscanf(fp,"%d %d %s",first.index,first.number,first.string);
printf("%d %d %s",first.index,first.number,first.string);
}
If my text file contains
1 123 karan
2 1234 haha
When i compile the code it says
Possible use of first before definition.
and on running the code it says
Cannot create process!
What am i doing wrong?
You need to use the
&operator withfscanf.Otherwise you’ll be treating the junk in
firstas addresses and will incur undefined behavior. Also, do note the format forfirst.string.There is a C FAQ