-3

i am trying to create an array of structures in C. it is as simple as reading in the information from a file into a structure, but I keep getting two warning messages about reading in my two character (number/colour) as well as that the program seems to just read zero for every value.

#include<stdio.h> 
#include<string.h>

typedef struct car
{
    int year;
    char number[9];
    char colour[10];
    float engine;
} car_type[6];

int main()
{
    car_type car;
    int i;
    FILE*fptr;

    fptr = fopen("indata.txt", "r");

    while (!feof(fptr))
    {
        fscanf(fptr, "%d %c %c %f", &car[i].year, &car[i].number,
                &car[i].colour, &car[i].engine);
        i++;
    }

    fclose(fptr);

    return 0;
}
LPs
  • 16,045
  • 8
  • 30
  • 61
Luke Byrne
  • 15
  • 5
  • 2
    Those are strings, not single characters. Use `%s`. Voting to close this as simple typo. – Lundin Mar 29 '17 at 11:55
  • 1
    That kind of typedef mixing struct and fixed size array is probably not the most clear way to code... It's debatable if typedeffing structs at all is useful. `struct car car[6];` as the main local variable declaration would make more quickly understandable code. – hyde Mar 29 '17 at 11:57
  • 2
    `i` is an uninitialised variable anyway, so the behaviour is *undefined*. – Weather Vane Mar 29 '17 at 11:58
  • 2
    Also: always, *always* check for return value of *`scanf` functions, and print out IO errors, and handle parse errors somehow. – hyde Mar 29 '17 at 11:59
  • 3
    Please see [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) Better would be `while(fscanf(fptr, "%d%s%s%f", &car[i].year, car[i].number, car[i].colour, &car[i].engine) == 4) { i++; }` But I would actually read each line with `fgets` and then apply `sscanf`. – Weather Vane Mar 29 '17 at 12:01
  • 1
    Welcome to Stack Overflow! To help people answer your question, you'll need to be more specific about the warning. Please [edit] your post to incorporate the exact messages you get from your [mcve] (preferably using copy+paste to avoid transcription errors). – Toby Speight Mar 29 '17 at 12:03
  • Please do yourself a favor and format your code correctly (for example like the samples in your C textbook). – Jabberwocky Mar 29 '17 at 12:04

1 Answers1

0

I did not test this "code". The correct strategy you should apply is:
1) find the filesize
2) allocate the buffer
3) read whole file in it
4) know how many strings ('\n') it contains (in while loop)
5) in for cycle handle the buffer (i = 0; i < num_lines; i++) ...

#include <stdio.h>   
#include <string.h>  
#include <stdlib.h>  

#define BUF 64  
typedef struct _CarType  
{  
    int year;  
    int number;  
    int colour;  
    double engine;  
} CarType;  

int main(int argc, char argv[])  
{  
    CarType car[6];  

    FILE* fp = fopen("indata.txt","r");  
    if(fp != NULL)  
    {  
        int i = 0;  
        while(!feof(fp))  
        {  
            char year[BUF], colour[BUF], engine1[BUF], engine1[BUF], number[BUF];  
            memset(year,     '\0', BUF);  
            memset(colour,   '\0', BUF);  
            memset(engine1,  '\0', BUF);  
            memset(engine2,  '\0', BUF);  
            memset(number,   '\0', BUF);  

            fscanf(fp, "%s %s %s %s.%s", year, number, colour, engine1, engine2);  
            car[i].year      =    atoi(year);  
            car[i].number    =    atoi(number);  
            car[i].colour    =    atoi(colour);  
            car[i].engine    =    atoi(engine1);  
            car[i].engine    +=   (atoi(engine2)) / (10 * strlen(engine2));  

            i++;  
        }  
        fclose(fp);  
    }  
    return 0;  
}
  • Please don't just drop a wall of code with no explanation about what was wrong or how/why you changed it. – Lundin Mar 29 '17 at 12:46
  • thanks so much for your help - I'm new to programming in general and I've just been given this task so I'm quite stuck! Your explanation helps a lot - may I ask, where should the for loop actually go? – Luke Byrne Mar 29 '17 at 13:07
  • Whenever you can try to use "for" inspite of "while". It will constrain the code from the possible mistakes. You ask - where to use "for" , my answer is instead of while. By the way, the good practice is to check what the malloc, fopen etc. return. – user 7362383 Mar 29 '17 at 13:15
  • Thank you for your help! I am nearly on break but planning on finishing the code in an hour or so, if I get it out correctly (I think I will as I understand what to do now) would you mind pointing out some areas where it could be better formatted? – Luke Byrne Mar 29 '17 at 13:21
  • No. The best tutors for programmer are gdb and valgrind. They will help you. – user 7362383 Mar 29 '17 at 13:26