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