Thread: Structures, files, and array help

  1. #1
    Registered User
    Join Date
    Dec 2022
    Posts
    1

    Structures, files, and array help

    Code:
    #include <stdio.h>
    
    typedef  struct  cars {
                                              char  make [ 20 ] ;
                                              char  model [ 20 ] ;
                                              char  color  [ 15 ] ;
                                              int  year ;
                                              double price ;
                                          }  cars ; 
    
    
    
    
    //Provide the prototype for your PCFs below:
    
    
    
    
    int main (void)
    {
        //Local variables declared... add additional variables as required
        cars  inventory [ 25 ] ;
    
    
         //1. Open the cars.dat file in the read mode and confirm
        //     you have properly opened the file.
    
    
    
    
         //2. Read the entire contents of the cars.dat file into the
         //    inventory array.    
    
    
    
    
         //3. Close the cars.dat file
    
    
    
    
         //4. Call function_1( ), which will process the contents of the
         //    inventory array by sorting the inventory array to place all the cars
         //     in ascending order according to their price.
    
    
    
    
        //5. Call function_2( ), which will save (write) the sorted inventory
        //    array to a file named output.dat
     
    
    
        //6. Terminate
        return 0 ;
    }
    
    
    //Provide the definition for your PCFs below:
    I can't figure out how to populate the array with the data in the data file. Above is what I have so far. Below is what the data file looks like.

    Mazda Miata Blue 2022 22432.32
    Dodge Charger Yellow 2021 25649.99
    Lexus GX460 Black 2022 62180.00
    Audi SQ5 Yellow 2022 63274.00
    Lexus GS350 Yellow 2022 56107.44
    Volkswagen Touareg Green 2022 57139.65
    Lexus RX450H Silver 2022 57687.62
    Mercedes ML-Class Black 2022 63566.99
    Audi A6 Yellow 2022 60846.22
    Audi Q7 Brown 2022 62208.41
    BMW 4-Series Blue 2022 57560.00
    Ford F-150 White 2022 62035.00
    Chevrolet Corvette Blue 2022 68200.00
    Toyota Tundra Gray 2022 60320.00
    Chevrolet Suburban White 2022 67000.00
    Audi A8L Yellow 2020 75234.00
    Audi R8 Red 2019 91345.00
    Ferrari 430 Red 2016 150234.00
    Lamborghini Aventador Orange 2022 390000.00
    McLaren MP4-12C Yellow 2017 172349.00
    Mercedes CLS63/E63 Brown 2015 56183.00
    Nissan GT-R Blue 2020 73435.00
    Maserati Spyder Yellow 2015 63434.90
    Tesla Model-X Black 2019 65834.73
    Porche Boxter Yellow 2018 64654.66

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You really need to show some actual effort to solve the problem.

    Start by opening the file and checking that the file opened correctly.

    You have all the basic steps listed, now you need to try to implement those items.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures and Files
    By BIGDENIRO in forum C Programming
    Replies: 1
    Last Post: 11-30-2013, 12:29 AM
  2. Help with Files and Structures
    By PenguinTux in forum C++ Programming
    Replies: 11
    Last Post: 03-23-2011, 02:31 AM
  3. Structures and Files
    By nuddy in forum C Programming
    Replies: 2
    Last Post: 03-31-2010, 12:15 PM
  4. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  5. structures and files
    By eth0 in forum C++ Programming
    Replies: 9
    Last Post: 01-06-2004, 06:48 PM

Tags for this Thread