Reputation: 13
Ok, I'm trying to read in a text file with my information. The text file contents is this:
gasData.txt
0 987654 201200 4.000000
1 red 89114 0.000000
2 red 89712 13.500000
3 red 90229 15.300000
4 987654 201001 0.000000
5 987654 201111 5.200000
6 987654 201612 25.299999
7 red 89300 7.100000
8 green 16 0.000000
9 green 216 20.000000
10 green 518 61.000000
11 green 879 50.000000
CODE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#define N 12
struct record
{
char id[7];
int odometer;
float gallons;
};
typedef struct record record_t;
record_t gasData[N];
int main(int argc, char *argv[])
{
FILE *fileInput;
float gallons;
int element;
char id[10], odometer[10];
fileInput = fopen("gasData.txt", "r");
for(element = 0; element < N; element++)
{
fscanf(fileInput, "%s %s %f", id, odometer, &gallons);
/*if (feof(fileInput))
{
printf("end-of-file detected on file in\n");
exit(1);
}*/
printf("element = %d:, id = %s, odometer = %s, gallons = %f\n", element, id, odometer, gallons);
}
exit (0);
}
OUTPUT
element = 0:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 1:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 2:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 3:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 4:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 5:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 6:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 7:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 8:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 9:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 10:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 11:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
What I'm getting out is nonsense, here is my output from the program. The only thing that works is the element, but that's just the count of the loop, so no problem there. Also, when I uncomment my end-of-file loop, my program crashes. Sorry I don't know how to edit the list. Thanks in advance for any help.
EDITED CODE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#define N 12
/*struct record
{
char id[7];
int odometer;
float gallons;
};
typedef struct record record_t;
record_t gasData[N];*/
int main(int argc, char *argv[])
{
FILE *fileInput;
float gallons;
int element;
char id[10]; char odometer[10];
fileInput = fopen("gasData.txt", "r");
if (fileInput == NULL)
return -1;
for(element = 0; element < N; element++)
{
if (fscanf(fileInput, "%9s%9s%f", id, odometer, &gallons) == 3)
{
printf("element = %d:, id = %s, odometer = %s, gallons = %f\n",
element, id, odometer, gallons);
}
}
exit (0);
}
Upvotes: 0
Views: 77
Reputation: 5291
After incorporating @ihorob's solution, I got the a working code.
Thanks a lot.
Addition:
Remove the #include <unistd.h>
, it is not required in this example and causes doubt in its relavence to Windows OS.
Here is the code:
#include <stdio.h>
#define N 12
struct record
{
char id[7];
int odometer;
float gallons;
};
typedef struct record record_t;
record_t gasData[N];
int main(int argc, char *argv[])
{
float gallons = 0.0;
int element = 0;
char id[10] = { 0 }, odometer[10] = { 0 };
FILE *fileInput = fopen("gasData.txt", "r");
if (fileInput == NULL)
return -1;
for (element = 0; element < N; element++)
if (fscanf(fileInput, "%9s%9s%f", id, odometer, &gallons) == 3)
printf("element = %d:, id = %s, odometer = %s, gallons = %f\n", element, id, odometer, gallons);
return(0);
}
Upvotes: 1
Reputation: 53046
You are likely overflowing the buffers and overwrinting the '\0'
terminator try this
fscanf(fileInput, "%9s%9s%f", id, odometer, &gallons);
and also, check that fscanf()
did succeed, otherwise the values will be uninitialized and the same thing will happen so
if (fscanf(fileInput, "%9s%9s%f", id, odometer, &gallons) == 3)
{
printf("element = %d:, id = %s, odometer = %s, gallons = %f\n",
element, id, odometer, gallons);
}
don't assume that things are going to go fine, also check that fopen()
didn't fail
fileInput = fopen("gasData.txt", "r");
if (fileInput == NULL)
return -1;
This code
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#define N 12
struct record
{
char id[7];
int odometer;
float gallons;
};
typedef struct record record_t;
record_t gasData[N];
int main(int argc, char *argv[])
{
FILE *fileInput;
float gallons;
int element;
char id[10];
char odometer[10];
fileInput = fopen("gasData.txt", "r");
if (fileInput == NULL)
{
fprintf(stderr, "cannot open `gasData.txt'\n");
return -1;
}
for(element = 0; element < N; element++)
{
if (fscanf(fileInput, "%9s%9s%f", id, odometer, &gallons) == 3)
{
printf("element = %d:, id = %s, odometer = %s, gallons = %f\n",
element, id, odometer, gallons);
}
}
return 0;
}
should not fail.
Upvotes: 2