Strings are driving me crazy. It's like no buit-in function is useful.
What I want to do is this. I've got this file f:
1
I like fries
2
Do you like fries?
3
I like trains.
How can I read the ENTIRE sentence "I like fries" from a file? So far nothing has worked for me. I do not know how long the sentence is and the variable I am reading / saving the sentence into is char *Blaa;
ok EDIT cause this is driving me crazy
I've got this
typedef struct Student
{
int nrcrt;
char *name;
} Student;
Student *S;
void Readstuff()
{
FILE *f;
int i;
f = fopen("students.txt","r");
if (!f)
{
perror("can't open file.\n"); exit(EXIT_FAILURE);
}
char line[4096];
for(i=0;i<N;i++)
{
fscanf(f,"%d",&S[i].nrcrt);
printf("%d\n",S[i].nrcrt);
while (fgets(line,sizeof(line),f) != 0)
{
strcpy(S[i].name,line);
}
}
close(f);
}
I can read ints into S[i].nrcrt, but if I want to read a string an put it in a S[i].name, the entire universe explodes. I keep getting segmentation fault. I tried to read a line in a variable line
and then copy it into S[i].name
but nothing.
Let's pretend for the sake of argument a student's name can be "I like fries". I didn't want to overcomplicate the question so this is why I didn't actually say what I needed.