- #1
Peon666
- 108
- 0
Homework Statement
I want to read each line separately in a char array from a text file. As yet, the best I could do is to read the entire file in a single char array. How can I read each line in a separate array?
Homework Equations
The Attempt at a Solution
Code:
#include <stdio.h>
int main()
{
FILE *f;
char s[1000];
f=fopen("infile","r");
if (!f)
return 1;
while (fgets(s,1000,f)!=NULL)
printf("%s",s);
fclose(f);
return 0;
}
The file is a mixture of chars, ints, spaces and tabs. Here's what the file contains:
p1 0
12 2 21 2 13 32 18
p2 9
13 17 3 21 45 67 21
p3 34
12 3 43 24 3 43 23 12 32 12 35 3
p4 178
12 32 42 32 3 32 32 2 54 64 2
p5 250
44 43 11 32 78 123 32 324 432 21 123 231 23 123
p6 689
12 324 423 324 543 23 53 44 62 43 432 23 122 32
Thanks.