Thread: Reading from a text file to a struct and print with justification

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    2

    Reading from a text file to a struct and print with justification

    Hello,

    I've been trying to read and print the contents of a text file. The text file is like this:

    1234 Orange Toyota 2010
    5487 Blue Audi 1985

    This is my code. The compiler shows NO erros but the program doesn't run.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define CARS_FILE "cars.txt"
    
    
    
    
    typedef struct {
    	int plateNumber, year;
    	char color[10];
    	char brand[10];
    } Car;
    
    
    
    
    int main()
    {
    	Car *cars;
    	int i=0;
    	FILE *myFile;
    	
    	myFile = fopen(CARS_FILE, "r");
    	if(myFile == NULL)
    	{
    		printf("ERROR!\n");
    		return 1;
    	}
    	while(fscanf(myFile,"%d\t%s\t%s\t%d\n", &cars[i].plateNumber, &cars[i].color, &cars[i].brand, &cars[i].year)!= EOF)
    	{
    		printf("%d\t%s\t%s\t%d\n", cars[i].plateNumber, cars[i].color, cars[i].brand, cars[i].year);
    	}
    	fclose(myFile);	
    	system("PAUSE");
    	return 0;
    }
    Any help would be much appreciated...

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,731
    You need to either allocate some space for cars with malloc and/or realloc or change it to a static array. Also, you need to increment the counter.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    2
    GReaper could you elaborate, please; I'm new to C language and to programming in general...I incremented the counter. How can I use malloc?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,731
    You need to allocate memory. malloc does that, for example if you want 10 elements you'd do:
    Code:
    cars = malloc(sizeof(Car)*10);
    If you want to have dynamic size, you can use realloc. I'd use it like this:
    Code:
    cars = NULL;
    carAmount = 0;
    for (;;) {
        Car tempCar;
        /* Read next available car into tempCar, break if no more cars */
    
        ++carAmount;
        cars = realloc(cars, carAmount*sizeof(Car));
        cars[carAmount - 1] = tempCar;
    }
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-17-2013, 07:18 PM
  2. Reading data from a text file into a C struct
    By someone2088 in forum C Programming
    Replies: 11
    Last Post: 12-07-2011, 10:14 AM
  3. Print Struct to file
    By doia in forum C Programming
    Replies: 4
    Last Post: 02-05-2010, 04:35 PM
  4. Text Justification for console output
    By greywolf0723 in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2009, 12:29 AM
  5. reading text file to struct help needed!
    By werdy666 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 11:37 AM

Tags for this Thread