get ready before midterm
here is a problem for system programming course.
Assume you have a data file(car.txt) whose contents are formatted: 10 Ford Red 20 Chevy Blue 30 Toyota Yellow 40 Honda White
(a) Define a type for a data structure that will contain this information.
(b) Create a function that receives string containing one line, parses the information into fields, dynamically allocates the structures, fills the data into it, and returns the value.
now begin with structure , it is Car.h
/* * ===================================================================================== *
- Filename: Car.h *
- Description: *
- Version: 1.0
- Created: 04/04/15 20:20:23
- Revision: none
- Compiler: gcc *
- Author: teaddict (), cyberasker@gmail.com
- Company: * * ===================================================================================== */ #ifndef CAR_H_INCLUDED #define CAR_H_INCLUDED
typedef struct { int number; char model[100]; char color[100]; } Car;
#endif
then we need a GUI for our test app. It is carGui.h and carGui.c
carGui.h
/* * ===================================================================================== *
- Filename: carGui.h *
- Description: *
- Version: 1.0
- Created: 04/04/15 20:22:02
- Revision: none
- Compiler: gcc *
- Author: teaddict (), cyberasker@gmail.com
- Company: * * ===================================================================================== */ #ifndef CARGUI_H_INCLUDED #define CARGUI_H_INCLUDED #include”Car.h”
void getCars(Car*);
void printCars(Car*,int);
#endif
carGui.c
/* * ===================================================================================== *
- Filename: carGui.c *
- Description: *
- Version: 1.0
- Created: 04/04/15 20:23:32
- Revision: none
- Compiler: gcc *
- Author: teaddict (), cyberasker@gmail.com
- Company: * * ===================================================================================== */ #include”carGui.h”
void getCars(Car *c) { char tm[100]; char tc[100]; scanf(“%d %s %s”,&c->number,tm,tc); strcpy(c->model,tm); strcpy(c->color,tc); c++; }
void printCars(Car *c, int n) { int i=0; for(i=0; i<n; i++) { printf(“#########################################n”); printf(“Number:%dnModel:%snColor:%sn”,c->number,c->model,c->color); c++; }
}
and now we write our test.c
/* * ===================================================================================== *
- Filename: test.c *
- Description: *
- Version: 1.0
- Created: 04/04/15 20:29:12
- Revision: none
- Compiler: gcc *
- Author: teaddict (), cyberasker@gmail.com
- Company:
*
*
=====================================================================================
*/
#include
#include"carGui.h"
int main() { Car c[3]; int i=0;
for(i=0; i<3; i++) getCars(&c[i]);
printCars(&c[0],3);
return 0; }
the last thing is our Makefile
CC = gcc OBJS= carGui.o
.SUFFIXES: .c .o
.c .o: $(CC) $^ -o $@ $<
test: $(OBJS) $(CC) test.c -o $@ $(OBJS)
#dependance
carGui.o: Car.h carGui.h carGui.c test: test.c Car.h carGui.h
now you can try it : cat car.txt | ./test |
and all the codes are her too :
https://github.com/teaddict/system_programming/tree/master/car