You can achieve what you describe using the fgets
function as follows:
#include <stdio.h>
#include <stdlib.h>
const int MAX_LENGTH = 256;
int main (void)
{
// Open the file
FILE* file = fopen ("data.dat", "r");
// Read the lines
char a[MAX_LENGTH];
fgets (a, MAX_LENGTH - 1, file);
char b[MAX_LENGTH];
fgets (b, MAX_LENGTH - 1, file);
// ...
// Close the file
fclose (file);
return EXIT_SUCCESS;
}
Set the MAX_LENGTH
variable to the maximum expected length of a line in your file. You should also test fopen
and fgets
for returning NULL
, as both are error conditions.
solved C, read by line [closed]