![]() |
| | #1 |
| Registered User Join Date: Aug 2008
Posts: 9
| data recall from a structure I have now sat for three hours just playing with it and have no idea why this is happening. I am using code that works elsewhere in my program. I am reasonably proficient in java but as mentioned before I have only been learning C for a week or so, and it has got me beat. any suggestions on why this is happening would be more than appreciated. the text files we have been given can be found here here is my code plane_structure.h contains the plane structure Code:
/*
* File: plane_structure.h
* Author: Tone
*
* Created on 04 December 2009, 13:27
*/
#ifndef _PLANE_STRUCTURE_H
#define _PLANE_STRUCTURE_H
#ifdef __cplusplus
extern "C" {
#endif
/*
* Structure for baggage information
*
*/
typedef struct _baggage_info{
int maximum_baggage_weight;
float arm;
}baggage_info;
/*
* Structure for seat information
*
*/
typedef struct _seat_info {
int seats_in_row;
float arm;
}seat_info;
typedef struct _fuel_info {
int maximum_fuel_weight;
float arm;
char name_of_fuel[50];
int fuel_used_per_hour;
}fuel_info;
typedef struct _plane {
char ident[100];
char name[100];
int maximum_take_off_wieght;
int takeoff_dimension;
int max_landing_weight;
int landing_dimension;
int basic_wieght;
float basic_arm;
int seat_rows;
seat_info seats[50];
int baggage_locations;
baggage_info baggage[50];
fuel_info fuel;
int cruise;
}plane;
#ifdef __cplusplus
}
#endif
#endif /* _PLANE_STRUCTURE_H */
this file works great Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "plane_structure.h"
FILE *fr;
char temp[100];
plane aircraft[100];
plane foundAircraft;
int count=0;
void get_aircraft_filename(){
char filename[BUFSIZ];
char *p;
printf("Please enter the name of the file containing the aircraft data\n\n"
"Filename: ");
//scanf("%s",&filename);
if(fgets(filename, sizeof filename, stdin) != NULL){
if ((p = strchr(filename, '\n')) != NULL)
*p = '\0';
fr = fopen (filename, "rt");
}
if(fr==NULL){
printf("ERROR: file \"%s\" does not exist!\n\n",filename);
get_aircraft_filename();
}
load_plane_data();
}
load_plane_data(){
int i;
char seatInfo[5];
while(fgets(temp,100,fr) != NULL){
strcpy(aircraft[count].ident,&temp[7]);
fgets(temp,100,fr);
strcpy(aircraft[count].name,&temp[6]);
fgets(temp,49,fr);
strcpy(temp,&temp[6]);
aircraft[count].maximum_take_off_wieght=atoi(temp);
fgets(temp,49,fr);
strcpy(temp,&temp[4]);
aircraft[count].takeoff_dimension=atoi(temp);
fgets(temp,49,fr);
strcpy(temp,&temp[6]);
aircraft[count].max_landing_weight=atoi(temp);
fgets(temp,49,fr);
strcpy(temp,&temp[4]);
aircraft[count].landing_dimension=atoi(temp);
fgets(temp,49,fr);
strcpy(temp,&temp[4]);
aircraft[count].basic_wieght=atoi(temp);
fgets(temp,49,fr);
strcpy(temp,&temp[4]);
aircraft[count].basic_arm=atoff(temp);
fgets(temp,49,fr);
strcpy(temp,&temp[10]);
aircraft[count].seat_rows=atoi(temp);
char *token;
char *line = temp;
char *search = " ";
for(i=0;i<aircraft[count].seat_rows;i++){
int inner=0;
fgets(temp,49,fr);
char *seat_line=temp;
token=strtok(seat_line,search);
aircraft[count].seats[i].seats_in_row=atoi(token);
token=strtok(NULL,search);
aircraft[count].seats[i].arm=atoff(token);
}
fgets(temp,49,fr);
strcpy(temp,&temp[9]);
aircraft[count].baggage_locations=atoi(temp);
for(i=0;i<aircraft[count].baggage_locations;i++){
int inner=0;
fgets(temp,49,fr);
for(inner=0;inner<3;inner++){
if(temp[inner]==' ')break;
seatInfo[inner]=temp[inner];
aircraft[count].baggage[i].maximum_baggage_weight=atoi(seatInfo);
}
strcpy(temp,&temp[inner]);
aircraft[count].baggage[i].arm=atoff(temp);
}
//fuel
char fuelChars[5];
int fuelCount=0;
fgets(temp,49,fr);
strcpy(temp,&temp[6]);
//get the first bit
char *fuel_line = temp;
/* Token will point to "LINE". */
token = strtok(fuel_line, search);
aircraft[count].fuel.maximum_fuel_weight=atoi(token);
/* Token will point to "TO". */
token = strtok(NULL, search);
aircraft[count].fuel.arm=atoff(token);
token = strtok(NULL, search);
strcpy(fuelChars,fuelChars);
strcpy(aircraft[count].fuel.name_of_fuel,token);
strcpy(temp,&temp[fuelCount]);
aircraft[count].fuel.fuel_used_per_hour=atoi(temp);
fgets(temp,49,fr);
strcpy(temp,&temp[8]);
aircraft[count].cruise=atoi(temp);
fgets(temp,49,fr);
count++;
}
strcpy(aircraft[count].ident,"END");
fclose(fr);
}
void print_aircraft_info(){
int i;
int counter;
for(counter=0;counter<count;counter++){
printf("Aircraft: %d\n",counter);
printf("Ident: %s",aircraft[counter].ident);
printf("Name: %s",aircraft[counter].name);
printf("MTOW: %d\n",aircraft[counter].maximum_take_off_wieght);
printf("TD: %d\n",aircraft[counter].takeoff_dimension);
printf("MXLW: %d\n",aircraft[counter].max_landing_weight);
printf("LD: %d\n",aircraft[counter].landing_dimension);
printf("BW: %d\n",aircraft[counter].basic_wieght);
printf("BA: %f\n",aircraft[counter].basic_arm);
printf("SeatRows %d\n",aircraft[counter].seat_rows);
for(i=0;i<aircraft[counter].seat_rows;i++){
printf("%d %f\n",aircraft[counter].seats[i].seats_in_row,aircraft[counter].seats[i].arm);
}
printf("Baggage: %d\n",aircraft[counter].baggage_locations);
for(i=0;i<aircraft[counter].baggage_locations;i++){
printf("%d %f\n",aircraft[counter].baggage[i].maximum_baggage_weight,aircraft[counter].baggage[i].arm);
}
printf("Fuel: %d %f %s %d\n",aircraft[counter].fuel.maximum_fuel_weight,aircraft[counter].fuel.arm,aircraft[counter].fuel.name_of_fuel,aircraft[counter].fuel.fuel_used_per_hour);
printf("Cruise speed: %d",aircraft[counter].cruise);
}
}
void print_found_aircraft(){
int i;
printf("Aircraft found:\n");
printf("Ident: %s",foundAircraft.ident);
printf("Name: %s",foundAircraft.name);
printf("MTOW: %d\n",foundAircraft.maximum_take_off_wieght);
printf("TD: %d\n",foundAircraft.takeoff_dimension);
printf("MXLW: %d\n",foundAircraft.max_landing_weight);
printf("LD: %d\n",foundAircraft.landing_dimension);
printf("BW: %d\n",foundAircraft.basic_wieght);
printf("BA: %f\n",foundAircraft.basic_arm);
printf("SeatRows: %d\n",foundAircraft.seat_rows);
for(i=0;i<foundAircraft.seat_rows;i++){
printf("seats per row: %d\narm of seats: %f\n",foundAircraft.seats[i].seats_in_row,foundAircraft.seats[i].arm);
}
printf("Baggage: %d\n",foundAircraft.baggage_locations);
for(i=0;i<foundAircraft.baggage_locations;i++){
printf("%d %f\n",foundAircraft.baggage[i].maximum_baggage_weight,foundAircraft.baggage[i].arm);
}
printf("Fuel: %d %f %s %d\n",foundAircraft.fuel.maximum_fuel_weight,foundAircraft.fuel.arm,foundAircraft.fuel.name_of_fuel,foundAircraft.fuel.fuel_used_per_hour);
printf("Cruise speed: %d",foundAircraft.cruise);
}
void find_aircraft_by_ident(char ident[]){
int counter;
for(counter=0;counter<count;counter++){
char temp[7];
strcpy(temp,aircraft[counter].ident);
int diff=strcmp(temp, ident);
if(diff==0){
foundAircraft=aircraft[counter];
print_found_aircraft();
return;
}
}
printf("ERROR: Plane not found\n\n");
}
void find_aircraft_by_name(char name[]){
int counter;
for(counter=0;counter<count;counter++){
char temp[11];
strcpy(temp,aircraft[counter].name);
int diff=strcmp(temp, name);
if(diff==0){
foundAircraft=aircraft[counter];
print_found_aircraft();
return;
}
}
printf("ERROR: Plane not found\n\n");
}
this is the file with problems Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Define the runway structure */
typedef struct _runway{
char runway_designator[50];
int length_in_feet;
}runway;
/* Define the airport structure */
typedef struct _airport{
char letter_code[6];
char name[20];
float latitude;
float longitude;
int number_of_runways;
runway runways[30];
int number_of_fuels;
char fuels[30][30];
}airport;
FILE *fr;
char filename[100];
airport airports[100];
int count;
char temp[100];
get_airport_filename(){
printf("Please enter the name of the file containing the airport data\n\n"
"Filename: ");
scanf("%s",&filename);
fr = fopen (filename, "rt");
if(fr==NULL){
printf("ERROR: file \"%s\" does not exist!",filename);
get_airport_filename();
}
load_airport_data();
}
load_airport_data(){
int i;
count=0;
char info[5];
while(fgets(temp,100,fr) != NULL){
// fgets(temp,100,fr);
strcpy(airports[count].letter_code,temp);
fgets(temp,100,fr);
strcpy(airports[count].name,temp);
fgets(temp,100,fr);
airports[count].latitude=atoff(temp);
fgets(temp,100,fr);
airports[count].longitude=atoff(temp);
fgets(temp,100,fr);
airports[count].number_of_runways=atoi(temp);
fgets(temp,100,fr);
int outer;
for(outer=0;outer<airports[count].number_of_runways;outer++){
char *token;
char *line = temp;
char *search = " ";
token=strtok(line,search);
strcpy(airports[count].runways[outer].runway_designator,token);
strcpy(temp,&temp[6]);
airports[count].runways[outer].length_in_feet=atoi(temp);
}
outer=0;
fgets(temp,100,fr);
airports[count].number_of_fuels=atoi(temp);
//here is where the problem is
//for some reason the number that comes back from
//airports[count].number_of_fuels on the second loop
//of the main loop returns 8 instead of two. Yet if you break the prog
//and check inside the structure it is clearly 2
for(outer=0;outer<airports[count].number_of_fuels;outer++){
fgets(temp,100,fr);
strcpy(airports[count].fuels[outer],temp);
outer++;
}
outer=0;
fgets(temp,100,fr);
count++;
}
fclose(fr);
}
print_airport_info(){
int counter=0;
for(counter=0;counter<count;counter++){
printf("Code: %s",airports[counter].letter_code);
printf("Name: %s",airports[counter].name);
printf("Latitude: %f\n",airports[counter].latitude);
printf("Longitude: %f\n",airports[counter].longitude);
printf("Number of runways: %d\n", airports[counter].number_of_runways);
int i;
for(i=0;i<airports[counter].number_of_runways;i++){
printf("%s %d\n",airports[counter].runways[i].runway_designator,airports[counter].runways[i].length_in_feet);
}
printf("Number of fuels: %d\n",airports[counter].number_of_fuels);
for(i=0;i<airports[counter].number_of_fuels;i++){
printf("%s",airports[counter].fuels[i]);
}
counter++;
}
}
print_found_airport(){
}
Code:
/*
* File: main.c
* Author: tone
*
* Created on 03 December 2009, 22:32
*/
#include <stdio.h>
#include <stdlib.h>
int main() {
//get_aircraft_filename();
get_airport_filename();
//print_airport_info();
//print_menu();
return (EXIT_SUCCESS);
}
all comments greatly appriciated. |
| m1cha3l is offline | |
| | #2 | |
| dat is, vast staat Join Date: Jul 2008 Location: SE Queens
Posts: 6,612
| Quote:
Maybe that person will come along, some day.
__________________ C programming resources: GNU C Function and Macro Index -- glibc reference manual The C Book -- nice online learner guide Current ISO draft standard CCAN -- new CPAN like open source library repository GDB tutorial #1 -- gnu debugger tutorials -- GDB tutorial #2 cpwiki -- our wiki on sourceforge | |
| MK27 is offline | |
| | #3 |
| Registered User Join Date: Aug 2008
Posts: 9
| I apologise if that was the wrong thing to do! I assumed it would be better to give as much info as possible! the part of the code is in the airports_data_manipulation file and is commented Last edited by m1cha3l; 12-05-2009 at 12:53 PM. |
| m1cha3l is offline | |
| | #4 |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 1,243
| So do what was suggested and post the specific problem, i.e. a few lines of code that reproduces the problem and highlight (bold or colour) the specific line that you think is the source of the problem. You can reduce the majority of this code to reproduce the problem without having all of the other stuff to get in the way. Make a copy of your current project and remove every line that is irrelevant, i.e. print statements and variables that are unrelated to the problem. Then post this modified and shortened copy of your code which reproduces the problem. Then we can start with something. The longer you delay this suggestion, I think the longer you will have to wait for an answer. |
| nadroj is offline | |
| | #5 |
| Registered User Join Date: Aug 2008
Posts: 9
| ok here is the function the problem peice of code is highlighted my apologies once again Code: load_airport_data(){
int i;
count=0;
char info[5];
while(fgets(temp,100,fr) != NULL){
fgets(temp,100,fr);
fgets(temp,100,fr);
fgets(temp,100,fr);
fgets(temp,100,fr);
airports[count].number_of_runways=atoi(temp);
fgets(temp,100,fr);
int outer;
for(outer=0;outer<airports[count].number_of_runways;outer++){
char *token;
char *line = temp;
char *search = " ";
token=strtok(line,search);
strcpy(airports[count].runways[outer].runway_designator,token);
strcpy(temp,&temp[6]);
airports[count].runways[outer].length_in_feet=atoi(temp);
}
fgets(temp,100,fr);
airports[count].number_of_fuels=atoi(temp);
for(outer=0;outer<airports[count].number_of_fuels;outer++){
fgets(temp,100,fr);
strcpy(airports[count].fuels[outer],temp);
}
fgets(temp,100,fr);
count++;
}
fclose(fr);
}
|
| m1cha3l is offline | |
| | #6 | ||
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 1,243
| Quote:
Quote:
I hope you are starting to realize that if you are explicit and precise when describing your problem, you will get relevant and timely answers. If you dont try to help us help you and dont explain properly or vaguely, its only hurting you! | ||
| nadroj is offline | |
| | #7 |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 1,243
| One thing is you have declared fuels as Code: char fuels[30][30]; Code: fgets(temp,100,fr);
strcpy(airports[count].fuels[outer],temp);
EDIT: The code seems to have many potential problems, here is just one more example. You define Code: char letter_code[6];
char name[20];
Code: strcpy(airports[count].letter_code,temp); // letter code can hold up to 6 but your giving up to 100
fgets(temp,100,fr);
strcpy(airports[count].name,temp); // name can hold up to 20 but your giving up to 100
Last edited by nadroj; 12-05-2009 at 01:18 PM. |
| nadroj is offline | |
| | #8 |
| Registered User Join Date: Aug 2008
Posts: 9
| the error is handle_exeptions: Error while dumping state (probably corrupted stack) when debugging i found that while the structure contains the integer 2 this line for(outer=0;outer<airports[count].number_of_fuels;outer++) reacts as 8 @nadroj altered that and it made no difference, but well spotted. Thank you |
| m1cha3l is offline | |
| | #9 | |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 1,243
| Quote:
Also, I dont think the code you have posted is what your running. I dont see how you can even compile any of this code. None of your functions have a return type or return statement. Also the file with your "main" doesnt even "#include" your other files, so it cannot call those functions. Please post your actual code. Last edited by nadroj; 12-05-2009 at 02:07 PM. | |
| nadroj is offline | |
| | #10 |
| Registered User Join Date: Aug 2008
Posts: 9
| as stated in previous post. I have done exactly the same thing with the aircraft file and that works perfectly. No errors at all. and the files I originally posted are the files I am using. I am using netbeans and have copied and paste from there to here |
| m1cha3l is offline | |
| | #11 |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 1,243
| I dont care what your doing with the files Im not talking about--those also have errors. Your "main" file has an error in it because it doesnt include the other required files your using. Realize that if some piece of code "runs" does not mean it is correct. Edit the code to do the following Code: while(fgets(temp,100,fr) != NULL){
fgets(temp,100,fr);
fgets(temp,100,fr);
fgets(temp,100,fr);
fgets(temp,100,fr);
airports[count].number_of_runways=atoi(temp);
// put a print statement here to show what number_of_runways, make sure its what you expect it to be Code: fgets(temp,100,fr);
airports[count].number_of_fuels=atoi(temp);
// print statement for number_of_fuels to make sure its correct |
| nadroj is offline | |
| | #12 |
| Registered User Join Date: Aug 2008
Posts: 9
| i have already said that they are not! when the program runs it returns 8 from the structure but when stepping through the program if you look inside the data structure it says the value is 2. this is another reason i included all the data in my first post so you can see I assign the value 2 to the structure. and fair enough there are flaws with my code! but did you right perfect code from the moment you started? I do intend to write better code but this is my first program! I have on your advice now added the includes in the main file. Last edited by m1cha3l; 12-05-2009 at 01:42 PM. |
| m1cha3l is offline | |
| | #13 |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 1,243
| I never once judged your ability, so stop assuming that and becoming defensive. You have a problem and asked for help. Im saying that these things are the source of your problem. Your saying they are not the source. For the number_of_runways, add one more fgets before you read it, it appears your reading the wrong line. Your probably reading "08/26 3031" which gets interpreted as "08" or simply "8", but you mean to read the next line (or previous line, see next post). Last edited by nadroj; 12-05-2009 at 02:00 PM. |
| nadroj is offline | |
| | #14 |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 1,243
| You havent described the format of the input file, so all we can do is guess. Code: EGFA Aberporth 52.115278 -4.556944 1 // if this is the number_of_runways, then the 5th read is this line 08/26 3031 2 // if this is the fuels, then the 7th read is this line 100LL JA1 Also, it seems the records are not uniform, i.e. the next one is this Code: EGCK Caernarfon 53.104167 -4.340278 2 02/20 3534 08/26 2887 2 100LL JA1 Last edited by nadroj; 12-05-2009 at 01:57 PM. |
| nadroj is offline | |
| | #15 | |
| Registered User Join Date: Aug 2008
Posts: 9
| I supplied a link to the text file provided. you where right i had a fgets in the wrong place. this is the method fixed. Code:
void load_airport_data(){
count=0;
char newline='\n';
while(fgets(temp,20,fr) !=NULL){ // get the code
if(strcmp(temp,&newline)==0)continue;
strcpy(airports[count].letter_code,temp); // put code into variable in structure
fgets(temp,20,fr); // gets name
strcpy(airports[count].name,temp);
fgets(temp,20,fr); // latitude
airports[count].latitude=atoff(temp);
fgets(temp,20,fr); // longitude
airports[count].longitude=atoff(temp);
fgets(temp,10,fr); // number of runways
airports[count].number_of_runways=atoi(temp); //
int outer;
for(outer=0;outer<airports[count].number_of_runways;outer++){ //loop for number of runways
fgets(temp,20,fr); // gets the runway line
char *token;
char *line = temp;
char *search = " ";
token=strtok(line,search);
strcpy(airports[count].runways[outer].runway_designator,token);
strcpy(temp,&temp[6]);
airports[count].runways[outer].length_in_feet=atoi(temp);
}
fgets(temp,100,fr); //get the number of fuels
airports[count].number_of_fuels=atoi(temp);
for(outer=0;outer<airports[count].number_of_fuels;outer++){ //loop for number of fuels
fgets(temp,100,fr);
strcpy(airports[count].fuels[outer],temp);
}
fgets(temp,10,fr); //read blank line
count++;
}
fclose(fr);
}
the includes you told me to put in the main file, is causing errors. this is the error Code: gcc -Werror -o dist/Debug/Cygwin-Windows/cppapplication_1 build/Debug/Cygwin-Windows/airport_data_manipulation.o build/Debug/Cygwin-Windows/main.o build/Debug/Cygwin-Windows/plane_data_manipulation.o build/Debug/Cygwin-Windows/menu.o build/Debug/Cygwin-Windows/main.o: In function `load_airport_data': /cygdrive/c/Documents and Settings/tone/My Documents/NetBeansProjects/CppApplication_1/airport_data_manipulation.c:36: multiple definition of `_load_airport_data' build/Debug/Cygwin-Windows/airport_data_manipulation.o:/cygdrive/c/Documents and Settings/tone/My Documents/NetBeansProjects/CppApplication_1/airport_data_manipulation.c:36: first defined here build/Debug/Cygwin-Windows/main.o: In function `get_airport_filename': /cygdrive/c/Documents and Settings/tone/My Documents/NetBeansProjects/CppApplication_1/airport_data_manipulation.c:75: multiple definition of `_get_airport_filename' build/Debug/Cygwin-Windows/airport_data_manipulation.o:/cygdrive/c/Documents and Settings/tone/My Documents/NetBeansProjects/CppApplication_1/airport_data_manipulation.c:75: first defined here build/Debug/Cygwin-Windows/main.o: In function `print_airport_info': /cygdrive/c/Documents and Settings/tone/My Documents/NetBeansProjects/CppApplication_1/airport_data_manipulation.c:87: multiple definition of `_print_airport_info' build/Debug/Cygwin-Windows/airport_data_manipulation.o:/cygdrive/c/Documents and Settings/tone/My Documents/NetBeansProjects/CppApplication_1/airport_data_manipulation.c:87: first defined here build/Debug/Cygwin-Windows/main.o: In function `print_found_airport': /cygdrive/c/Documents and Settings/tone/My Documents/NetBeansProjects/CppApplication_1/airport_data_manipulation.c:109: multiple definition of `_print_found_airport' Quote:
I do appriciate your help and advice Last edited by m1cha3l; 12-05-2009 at 03:24 PM. | |
| m1cha3l is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| pthread question how would I init this data structure? | mr_coffee | C Programming | 2 | 02-23-2009 12:42 PM |
| Data structure implementation | fkheng | C Programming | 3 | 07-31-2003 07:44 AM |
| can't insert data into my B-Tree class structure | daluu | C++ Programming | 0 | 12-05-2002 06:03 PM |
| Tab Controls - API | -KEN- | Windows Programming | 7 | 06-02-2002 09:44 AM |
| Dynamic Data Structure -- Which one is better? | Yin | C++ Programming | 0 | 04-10-2002 11:38 PM |