Thread: arrays and struct !!

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    38

    arrays and struct !!

    the file txt:

    10 Eilat
    2234 10000 20000 12.50
    2235 12400 24800 22.78
    2236 8300 16600 34.55
    2237 11786 12500 14.34
    2238 16901 25023 9.78
    2239 19076 38678 23.06
    2240 14238 24356 127.08
    2241 511 1256 13.50
    2242 45403 85000 137.00
    2243 600 1000 17.86



    this is my code

    Code:
    #include <stdio.h>
    #include <string.h>
    #define SIZE 10
    struct ItemType {
    int Docking_ID;
    int Max_size;
    int Time_Taken;
    float Chargs;
    };
    struct ItemType item[SIZE];
    
    
    int main(){
        FILE *file1; // fisrt file
    
    
        
        
        int i;
        char Harbour_Name;
        int Num_Of_Bays;
        
        file1 = fopen("Port_Data_File.txt", "r");        /* Open file first.txt */
        if (file1 == NULL) {
        /* Couldn't open the file. */
        printf("Input file not found.\n");
        return -1; /* Non-zero means error. */
        }
        fscanf(file1,"%d %s",&Num_Of_Bays,&Harbour_Name);
        printf("\n");
        printf("NUMBER OF DOCKING BAYS ARE %d AND THE HARBOUR NAME IS %s\n",Num_Of_Bays,Harbour_Name);
        printf("\n");
        printf("DOCKING ID   MAXIMUN SIZE OF BAY   TIME TO UNLOAD  CHARGE\n");
        printf("==========   ===================   ==============  ======\n");
        for (i=0;i<SIZE;i++){
             
        fscanf(file1,"%d %d %d %f",&item[i].Docking_ID,&item[i].Max_size,&item[i].Time_Taken,&item[i].Chargs);
        
        printf("%d \t %d \t %d \t %f\n", item[i].Docking_ID,item[i].Max_size,item[i].Time_Taken,item[i].Chargs);
        }
           
       
    
    
        
    
    
        
    
    
    getchar();
        return 0;
    }

    output :

    Error


    it has to give what is in the file :


    10 Eilat
    2234 10000 20000 12.50
    2235 12400 24800 22.78
    2236 8300 16600 34.55
    2237 11786 12500 14.34
    2238 16901 25023 9.78
    2239 19076 38678 23.06
    2240 14238 24356 127.08
    2241 511 1256 13.50
    2242 45403 85000 137.00
    2243 600 1000 17.86
    Last edited by abood1190; 10-16-2011 at 03:45 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > char Harbour_Name;
    How many characters can you store here?

    How many chars do you need to store "Eilat"?

    Oh, and please avoid creating a new threads for essentially the same problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    one string = Eilat
    am sorry to create another thread.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Take a look at line 1 ... you can only put 1 (one) number include statement on a line.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    that wanst an error.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    please guys i really need help ... i just want to read my file ... can anyone please offer help ... i couldnt solve it

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you do
    Code:
    char Harbour_Name[] = "Eilat";
    printf("Space needed=%d\n", (int)sizeof(Harbour_Name) );
    What does that tell you about how much space you need to store a string?


    Now do
    Code:
        char Harbour_Name[200];  //!! pick a suitable size yourself
        fscanf(file1,"%d %s",&Num_Of_Bays,Harbour_Name); //!! note: no & on Harbour_Name
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    WoW .. it works!
    i just changed to char Harbour_Name[6];
    i will get back to you because i have more questions ^_^
    Thank you Salem

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    Check out the output ...


    Now, my question >>> can i process the ships ?? can i play with numbers ?? for example, i want to send some ships by their ID to certain docking bays ?? can i do that through my code or i need to add some other functions?


    arrays and struct !!-untitled-jpg

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    this is the code

    Code:
    #include <stdio.h>#include <string.h>
    #define SIZE 10
    #define SIZE2 23
    
    
    typedef struct ItemType {
    int Docking_ID;
    int Max_size;
    int Time_Taken;
    float Chargs;
    int Ship_ID;
    int Ship_Max_Size;
    int Actual_Size;
    int Arrival_Time;
    };
    struct ItemType item[SIZE];
    
    
        
    int main(){
        FILE *file1,*file2; // fisrt file
        
        int i;
        char Harbour_Name[6];
        int Num_Of_Bays;
        int Num_Ship;
        
        file1 = fopen("Port_Data_File.txt", "r");        /* Open first file.txt */
        if (file1 == NULL) {
        /* Couldn't open the file. */
        printf("Input file not found.\n");
        return -1; /* Non-zero means error. */
        }
        
        
         /* Introduction */   
        printf("       ================================================================\n");
    	printf("       --------------<<<<<<<<  WELCOME >>>>>>>>>-----------------------\n");
    	printf("       ================================================================\n");
    	printf("+                    THIS PROGRAM IS A SIMULATION                     +\n");
    	printf("\n");
    	printf("+                          FOR A HARBOUR                              +\n");
    	printf("\n");
    	
        fscanf(file1,"%d %s",&Num_Of_Bays,Harbour_Name);
        printf("\n");
        printf("NUMBER OF DOCKING BAYS ARE %d AND THE HARBOUR NAME IS %s\n",Num_Of_Bays,Harbour_Name);
        printf("\n");
        printf("DOCKING ID   MAXIMUN SIZE OF BAY   TIME TO UNLOAD     CHARGE\n");
        printf("==========   ===================   ==============     ======\n");
        for (i=0;i<SIZE;i++){
             
        fscanf(file1,"%d %d %d %f",&item[i].Docking_ID,&item[i].Max_size,&item[i].Time_Taken,&item[i].Chargs);
        
        printf("%d\t\t%d\t\t\t%d\t\t%f\n", item[i].Docking_ID,item[i].Max_size,item[i].Time_Taken,item[i].Chargs);
        }
        
        file2 = fopen("Boat_Data_File.txt", "r");        /* Open second file.txt */
        if (file1 == NULL) {
        /* Couldn't open the file. */
        printf("Input file not found.\n");
        return -1; /* Non-zero means error. */
        }    
        fscanf(file2,"%d",&Num_Ship); // take the input of numbers from the file
        printf("\n"); // space of line
        printf("NUMBER OF SHIPS ARE %d\n",Num_Ship); // display number of ships
        printf("\n"); // space of line
        printf("SHIP ID      MAXIMUN SIZE OF SHIP   ACTUAL SIZE     TIME TAKEN\n");
        printf("=======      ===================    ===========     ===========\n");
        for (i=0;i<SIZE;i++){
             
        fscanf(file2,"%d %d %d %d",&item[i].Ship_ID,&item[i].Ship_Max_Size,&item[i].Actual_Size,&item[i].Arrival_Time);
        
        printf("%d\t\t%d\t\t\t%d\t\t%d\n", item[i].Ship_ID,item[i].Ship_Max_Size,item[i].Actual_Size,item[i].Arrival_Time);
        }    
           
           
        for (i=0;item[i].Max_size>=item.Ship_Max_Size;i++){
             
         
        printf("%d\t\t%d\t\t\t%d\t\t%d\n", item[i].Ship_ID,item[i].Ship_Max_Size,item[i].Actual_Size,item[i].Arrival_Time);
        } 
    
    
    
    
        
    
    
    
    
        
    
    
    
    
        getchar();
        return 0;
    }


    waiting the answers guys .. and thank you for helping

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct, arrays, srand...
    By jmnp86 in forum C++ Programming
    Replies: 1
    Last Post: 03-15-2011, 02:44 AM
  2. deallocating a struct containing arrays
    By maxtothemax in forum C Programming
    Replies: 5
    Last Post: 09-28-2009, 07:09 PM
  3. struct arrays
    By silentkarma in forum C++ Programming
    Replies: 9
    Last Post: 07-23-2008, 03:24 AM
  4. Problem with struct arrays...
    By Bizmark in forum C Programming
    Replies: 5
    Last Post: 03-27-2008, 07:46 PM
  5. about arrays create within a struct~
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-26-2002, 04:21 AM