Hello again, I'm sorry for my newbie-ness at C.
I'm trying my best to learn, and I've been doing nothing but study the last week!
But I'm afraid pointers were just a major wall to me...I was hoping someone could help me with this program - I've made a thread a couple days ago, but I greatly corrected the code, started from scratch, and I think I've done some improvements, but again, I've hit a wall.
So, I'll try to explain the purpose of this program -This program is intended to read a .TXT file which contains a large database of car brands, price, model of the gar, how many doors it has, etc...
This is in the database under the form of:
You can see it HERECode:BRAND MODEL VERSION DOORS FUELTYPE PRICE €
So, that said, I tried making a program which did this:
Shows a menu of things to do, many I haven't even gotten to, so I'll talk about only the important options.
INFO : Using gcc from CYGWIN to compile the program, and Notepad++ to write it.Code:0 - Exits the program 1 - Asks you what file to read 2 - Shows all the brands of cars the file possesses 3 - Shows you the price of a target car, after you giving him the BRAND and MODEL.
Here goes the code, I'll try and split it into the functions I did
Code:#include <stdio.h>#include <stdlib.h> #include <string.h> #define MAX_AUTO 6000 #define BRAND 20 #define MODEL 25 #define VERSION 100 #define FUELTYPE 15
Initialize a structure to put the file after it's read in.
Code:typedef struct { char Brand[BRAND]; char Model[MODEL]; char Version[VERSION]; int Doors; char Fueltype[FUELTYPE]; int Price; } AUTOMOBILE;
This function is supposed to put all the structure entries at /0 before reading the file
Code:void clean_database(AUTOMOBILE *ptr) { int i; for(i=0;i < MAX_AUTO; i++) { strcpy(ptr[i].Brand,"\0"); strcpy(ptr[i].Model,"\0"); strcpy(ptr[i].Version,"\0"); ptr[i].Doors = 0; strcpy(ptr[i].Fueltype,"\0"); ptr[i].Price= 0.0; } }
Shows the menu
Code:void show_menu() { int esc; printf("\t\tC PROGRAMMING IS HARD\n\n"); printf("\t0 - Exit program\n"); printf("\t1 - Read database file\n"); printf("\t2 - Show all brands in database file\n"); printf("\t3 - Show price\n"); while(1) { printf("Choose option:"); scanf(" %d",&esc); if(esc < 0 || esc > 12) printf("Wrong Input\n"); else break; } switch(esc) { case 0: printf("Program terminated.\nGoodbye"); exit(0); case 1: printf("1"); break; case 2: printf("2"); break; case 3: printf("3"); break; } }
And the one giving me all the problems - the read file
I've attached the complete file on the thread, I don't want to make the thread too long, which it already is.Code:void read_file(int argc, char *argv[]) { int i=0,cont=0,contr=0; char ch[500]; AUTOMOBILE car[MAX_AUTO]; FILE *fp; clean_database(car); if(argc==2) { if((strcmp(argv[1],"bd.txt"))==0) { fp=fopen(argv[1],"r"); if(fp==NULL) { printf("Opening the file %s was not possible\n",argv[1]); exit(1); } while((fgets(ch,500,fp))!=NULL) { for(i=0;i!='\0';i++) { if(ch[i]==' ' && ch[i+1]==' ') { switch(cont) { case 0:{ while(ch[i+2] != *strstr(ch," ")) { strcpy(car->Brand, ch[i+2]); cont++; } break; } case 1:{ while(ch[i+2]!= *strstr(ch," ")) { strcpy(car->Model, ch[i+2]); cont++; } break; } case 2:{ while(ch[i+2]!= *strstr(ch," ")) { strcpy(car->Version, ch[i+2]); cont++; } break; } case 3:{ while(ch[i+2]!= *strstr(ch," ")) { strcpy(car->Doors, ch[i+2]); cont++; } break; } case 4:{ while(ch[i+2]!= *strstr(ch," ")) { strcpy(car->Fueltype, ch[i+2]); cont++; } break; } case 5:{ while(ch[i+2]!= *strstr(ch," ")) { strcpy(car->Price, ch[i+2]); } break; } } } else continue; } } printf("%d parameters have been read and saved into the programs structure",i); show_menu (); } } else if(argc>2) { printf("Sintax: \n\n%s File\n\nou\n\n%s",argv[0],argv[0]); exit(2); } else if(argc==1) { contr++; show_menu(contr); fclose(fp); } }
Almost forgot - errors I get are
Code:tentar.c: In function `read_file':tentar.c:111: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast tentar.c:119: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast tentar.c:127: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast tentar.c:135: warning: passing arg 1 of `strcpy' makes pointer from integer without a cast tentar.c:135: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast tentar.c:143: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast tentar.c:151: warning: passing arg 1 of `strcpy' makes pointer from integer without a cast tentar.c:151: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast
And those are all in the read_file function, in each and every strcpy...THANKS FOR YOUR HELP IF YOU CAN Regards,Bruno