I am having trouble with some file IO stuff.
I have this file:
db.dat:
Ryan
12 69.00 30.00 0.00
Bindy Lee
25 120.00 89.00 1.00
And this is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define RECORDS 30
#define LEN 20
main()
{
FILE *fptr;
fptr = fopen("db.dat", "r");
int i;
int counter = 2;
for (i = 0; i < counter; i++)
{
char temp1[LEN];
char temp2[LEN + 10];
fgets(temp1, LEN, fptr);
fgets(temp2, LEN, fptr);
printf("%s %s", temp1, temp2);
}
fclose(fptr);
}
I am supposed to get both lines, but I am getting this instead:
Ryan
12 69.00 30.00 0.00
Bindy Lee
Can someone please help! I don’t know why I am not getting both lines, and why I am getting spaces. Very odd…Thanks!!!!
fgetsstops after it readsLENcharacters OR reaches the end of the line. I think your problem here is that you madeLENtoo small.Change your printf to something more verbose like
printf("temp1='%s'\ntemp2='%s'\n", temp1, temp2);and you should be able to see what was actually read into each string.