Question

Using Structs on C code 1. At the top of your .c file, #include the header...

Using Structs on C code

1. At the top of your .c file, #include the header file you claimed.

#include “truck.h"

in my case.

Code:

#ifndef TRUCK_H
#define TRUCK_H

typedef struct truck_structure
{
   char make[20]; //model
   char typeOfDrivingLicense[50]; //commercial driving license required
   char typeOfTransmission[20]; //manual or automatic
   int yearManufactured; //when the truck was manufactured
   int numOfWheels; //how many wheels it has
   double weightEmpty; //the weight of the truck when it is empty
   double weightLoaded; // the weight of the truck when it is loaded
   double power; //the power
   char engineType[50]; //type of the engine
} Truck;

#endif

2. You must create five instances of the struct you claim.

That means you need to create five trucks

followed by C code in the main function to set the make, year, etc., etc. for each of the five trucks

Copy and paste will really make this easier, just make sure the information for every instance of your selected struct is different. No two trucks with the same name.

3.Save your populated structs – use the fwrite ( ) C library method

0 0
Add a comment Improve this question Transcribed image text
Answer #1

truck.cpp

#include "truck.h"
#include<stdio.h>

int main(){
Truck t1={"Ferrari","commercial","manual",2019,4,2000,2500,100,"electrical"};
Truck t2={"Ford","commercial","manual",2020,8,2300,2700,100,"electrical"};
Truck t3={"Audi","commercial","manual",2016,4,1800,2500,100,"electrical"};
Truck t4={"BMW","commercial","manual",2018,4,2100,2900,100,"electrical"};
Truck t5={"Maruti","commercial","manual",2019,4,1700,2500,100,"electrical"};

FILE *file;

// open file for writing
file = fopen ("data.txt", "w");

fwrite (&t1, sizeof(Truck), 1, file);
fwrite (&t2, sizeof(Truck), 1, file);
fwrite (&t3, sizeof(Truck), 1, file);
fwrite (&t4, sizeof(Truck), 1, file);
fwrite (&t5, sizeof(Truck), 1, file);

if(fwrite != 0)
printf("contents to file written successfully !\n");
else
printf("error writing file !\n");

// close file
fclose (file);

}

Add a comment
Know the answer?
Add Answer to:
Using Structs on C code 1. At the top of your .c file, #include the header...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Project 8 – Populating a struct and saving it. This is another “switch” assignment. Rather like...

    Project 8 – Populating a struct and saving it. This is another “switch” assignment. Rather like the scenarios, here you must select someone else's posted .h file, post a claim, and using it, codepopulating their struct with values you choose. Here “populate” means to give each member a value, using assignment operators and strcpy ( ) as needed. This all assumes you completed Project 7 first! You can't skip ahead on this one. I populated the Cat fluffy in my...

  • I need to complete tSubinfo and tSectionInfo in data.h in order to be able to save...

    I need to complete tSubinfo and tSectionInfo in data.h in order to be able to save the following info: In tSubInfo id : That isthe identifier of the subseccion ( only one character) subBooks: Vectors that tells us the position where books are in the subsection totSubBooks: total quantity of books in the subsection tSectionInfo: section: Estructure of the type section secSubs:Vector with all subsections (type tSubInfo) of the section (max 10 sections) which have one book totSecSubs: total quantity...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...

  • Program in C Student Data using Structs In this assignment you will create a program (using...

    Program in C Student Data using Structs In this assignment you will create a program (using multiple .c files) to solve the following problem: You have been hired by the university to create a program that will read in input from a .txt file. This input will contain a student name, student id #, their age and their current gpa. You should use the following struct for your student data: struct Student{                 char name[50];                 int id;                 float...

  • PROGRAMING IN C. PLEASE HELP FIX MY CODE TO FIT THE FOLLOWING CRITERIA: 1.)Read your stocks...

    PROGRAMING IN C. PLEASE HELP FIX MY CODE TO FIT THE FOLLOWING CRITERIA: 1.)Read your stocks from your mystocks.txt file. You can use this information for the simulator. 2.)The stock price simulator will return the current price of the stock. The current prices is the prices of the requested ticker + a random number. 3.)We will assume that the stock price remains constant after the price check till your trade gets executed. Therefore, a buy or a sell order placed...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT