Hi, I have a bit of problem to store array from text file, It compile but result is not correct. it will store array
when Max Capacity of ship is greater than harbour capacity
here is my code

#include <stdio.h> 
int loadShip(void)
{
    FILE  *fp, *fp1; 
    //char filename;
    int numOfShip, id, sMaxCap, actCap, time, i; // Ship
    int hId, hMaxCap, timeTake; // Harbour
    double aPerUnit;
    int getLine[23];
    
    
    //printf("Please enter a filename that store information about Habour");
    //scanf("%c", &filename);
    
    fp = fopen("data2.txt", "r"); 
    fp1 = fopen("data.txt", "r"); 
    
    if (fp == NULL) 
    { 
            printf("Error: This file could not be opened. or doesn't exist in current directory\n"); 
    }
    
       //fscanf(fp, "%d ", &numOfShip);
 
    while ((fscanf(fp, "%d %d %d %d", &id, &sMaxCap, &actCap, &time) != EOF) && (fscanf(fp1, "%d %d %d %lf", &hId, &hMaxCap, &timeTake, &aPerUnit)!= EOF)) 
        
    {
        

        if( sMaxCap >= hMaxCap )
        {
          for(i = 0; i < 22; i++)
          {
            getLine[i]= id;
            getLine[i+1] = sMaxCap;
            getLine[i+2] = actCap;
            getLine[i+3] = time;
  
         }
       }
    }
    for(i = 0; i < 22; i++) // test
    {
        printf(" %d %d %d %d\n",  getLine[i],  getLine[i+1], getLine[i+2], getLine[i+3]);
        
    }

      return 0;
}

int main(void)
{
    int numOfbay;
    
    numOfShip = loadShip();
     
    return 0;
}

expect result

1234 25500 24000 1
1345 23523 21209 5
9865 12389 600 98
4536 43000 42500 105
321 36789 36789 55
67899 32100 28000 43
10987 12200 6879 205
6543 28500 24363 143

the actual result

6543 6543 6543 6543
 6543 6543 6543 6543
 6543 6543 6543 6543
 6543 6543 6543 6543
 6543 6543 6543 6543
 6543 6543 6543 6543
 6543 6543 6543 6543
 6543 6543 6543 6543
 6543 6543 6543 6543
....
 6543 28500 24363 143

Thank

if( sMaxCap >= hMaxCap )
        {
          for(i = 0; i < 22; i++)
          {
            getLine[i]= id;
            getLine[i+1] = sMaxCap;
            getLine[i+2] = actCap;
            getLine[i+3] = time;
  
         }
       }

I don't think this does what you want it to. Each time through the loop, i is incremented and the same id, sMaxCap, actCap and time are written to the array shifted one index over. It does this all the way to the end of the array, which gives you id over and over again.

Also, you're accessing getline[i+3] when i = 21, which gives you getline[24], but the array is only 23 long, so this gives a segfault.

Could you explain what you're trying to do exactly and maybe I can help you more.

I actually I try to write a program that simulated a Harbour unload system so We will import 2 file first information about harbour second about ship so if maximum capacity of ship is greater than maximum capacity of harbour so the ship can unload then display output

each line contain information of each ship
id, MaxCap actual Capacity , time that arrive
example of data2.txt


1234 25500 24000 1


each line contain information of each dock of harbour
id, MaxCap, timeTake, amount Per Unit
example of data.txt

2236 8300 16600 34.55

something like that

Do you know about structs? They're used to group data to represent one entity. I suggest keeping all the information of one ship in a struct like this:

struct ship {
  int id;
  int max_capacity;
  int actual_capacity;
  int timeTaken;
};

Then you can fill structs with the information like this:

struct ship s1;
s1.id = 5;
s1.max_capacity = 1000;
s1.actual_capacity = 900;
s1.timeTaken = 120;

struct ship s2;
s2.id = 6;
s2.max_capacity = 2000;
s2.actual_capacity = 1500;
s2.timeTaken = 100;

Now you can compare the max capacity of the two ships:

if (s1.max_capacity > s2.max_capacity) {
  printf("Ship %d has a larger capacity than ship %d.", s1.id, s2.id);
} else {
  printf("Ship %d has a larger capacity than ship %d.", s2.id, s1.id);
}

To get the information into structs, you can fill the structs as you read info in from the files.

So how link to my function ? becuz I dont really know how to use structure

This article has been dead for over six months. Start a new discussion instead.