Hi, I having problem with enter file name to read file it give me result like this

Please Enter Ship File path or Ship File name: data
Segmentation fault: 11

Here is my code

#include <stdio.h>
#include <stdlib.h>

struct harbour 
{
    int harbourId;
    int maxCap;
    int timeUpload;
    double amount;
    
};

struct ship
{

    int number;
    int shipId;
    int sMaxCap;
    int sActCap;
    int aTime;
};

int sort(const void *x, const void *y)
{
    const struct ship
    *ship1 = (struct ship *)x,
    *ship2 = (struct ship *)y;
    
    if ( ship1->aTime > ship2->aTime ) return 1;
    if ( ship1->aTime < ship2->aTime ) return -1;
    
    return 0;
}
void readHarbour( struct harbour *dock)
{
    char *fname;
    int i=0, lineCount=0;
	FILE *harbourf;
	
    printf("Please Enter Harbour File path or Harbour File name: ");
    scanf("%s", fname);
    harbourf = fopen(fname,"r");
    
    if (harbourf == NULL) 
    { 
        printf("Error: This file could not be opened or File not be found.\n"); 
    } 
    do 
    {
        fscanf(harbourf, "%d %d %d %lf", &dock[i].harbourId, &dock[i].maxCap, &dock[i].timeUpload, &dock[i].amount);
        
        lineCount++;
        i++;
    } while (feof(harbourf) == 0);
 
     
}

void readShip( struct ship *ship)
{
    char *fname2;
    int i=0, lineCount=0; 
	FILE *shipf;
    
    printf("Please Enter Ship File path or Ship File name: ");
    scanf("%s", fname2);
	shipf = fopen(fname2,"r");
    
    if (shipf == NULL)
    { 
        printf("Error: This file could not be opened or File not be found.\n"); 
    } 
    do 
    {
        fscanf(shipf, "%d %d %d %d", &ship[i].shipId, &ship[i].sMaxCap, &ship[i].sActCap, &ship[i].aTime);
        
        lineCount++;
        i++;
    } while (feof(shipf) == 0);
    


}

void processing(struct harbour h[100], struct ship s[100])
{
    int i, lineCount =24;
    int count0=1 ,count1=1, count2=1, count3=1, count4=1, count5=1, count6=1, count7=1,count8=1, count9=1, count10=1, count=1;
    int upload;
    double totalAmount;
    
    // Sort array Here 
    qsort(s, lineCount, sizeof(struct ship), sort);
   
    for(i=0;i<lineCount;i++)
    {
        if(s[i].sMaxCap <= h[0,1,2,3,4,5,6,7,8,9].maxCap)
        {
            printf("%d %d %d %d", s[i].shipId, s[i].sMaxCap, s[i].sActCap, s[i].aTime);
        }
                 if(s[i].sMaxCap <= h[0].maxCap)
                 {
                     if(count0 <= 2)
                     {                     
                     
                     totalAmount = s[i].sActCap * h[0].amount;
                     
                     //}
                     printf("\n");
                     printf("Processing complete\n");
                     printf("####################\n");
                     printf("Harbour id# %d\n", h[0].harbourId);
                     printf("Arrive Time: %d\n", s[i].aTime);
                     printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
                     printf(" total payment $ %.2f\n", totalAmount); 
                     printf("\n");
                         
                         count0++;
                     }

                 }
                 else if(s[i].sMaxCap <= h[1].maxCap)
                 {
                     if(count1 <= 2)
                     {
              
                     totalAmount = s[i].sActCap * h[1].amount;
                     
                    
                     printf("\n");
                     printf("Processing complete\n");
                     printf("####################\n");
                     printf("Harbour id# %d\n", h[1].harbourId);
                     printf("Arrive Time: %d\n", s[i].aTime);
                     printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
                     printf(" total payment $ %.2f\n", totalAmount); 
                     printf("\n");
                         
                         count1++;
                     }
                 }
                 else if(s[i].sMaxCap <= h[2].maxCap)
                 {
                     if(count2 <= 2)
                     {
                     
                     totalAmount = s[i].sActCap * h[2].amount;
                     
                     
                     printf("\n");
                     printf("Processing complete\n");
                     printf("####################\n");
                     printf("Harbour id# %d\n", h[2].harbourId);
                     printf("Arrive Time: %d\n", s[i].aTime);
                     printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
                     printf(" total payment $ %.2f\n", totalAmount); 
                     printf("\n");
                         
                         count2++;
                     }
                 }
                 else if(s[i].sMaxCap <= h[3].maxCap)
                 {
                     if(count3 <= 2)
                     {
                    
                     totalAmount = s[i].sActCap * h[3].amount;
                     
                     
                     printf("\n");
                     printf("Processing complete\n");
                     printf("####################\n");
                     printf("Harbour id# %d\n", h[3].harbourId);
                     printf("Arrive Time: %d\n", s[i].aTime);
                     printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
                     printf(" total payment $ %.2f\n", totalAmount); 
                     printf("\n");
                         
                         count3++;
                     }

                 }
                 else if(s[i].sMaxCap <= h[4].maxCap)
                 {
                     if(count4 <= 2)
                     {
                     
                     totalAmount = s[i].sActCap * h[4].amount;
                     
                     
                     printf("\n");
                     printf("Processing complete\n");
                     printf("####################\n");
                     printf("Harbour id# %d\n", h[4].harbourId);
                     printf("Arrive Time: %d\n", s[i].aTime);
                     printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
                     printf(" total payment $ %.2f\n", totalAmount); 
                     printf("\n");
                         
                         count4++;
                     }
                 }
                 else if(s[i].sMaxCap <= h[5].maxCap)
                 {
                     if(count5 <= 2)
                     {
                     
                     totalAmount = s[i].sActCap * h[5].amount;
                     
                    
                     printf("\n");
                     printf("Processing complete\n");
                     printf("####################\n");
                     printf("Harbour id# %d\n", h[6].harbourId);
                     printf("Arrive Time: %d\n", s[i].aTime);
                     printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
                     printf(" total payment $ %.2f\n", totalAmount); 
                     printf("\n");
                         
                         count5++;
                     }
                 }
                 else if(s[i].sMaxCap <= h[6].maxCap)
                 {
                     if(count6 <= 2)
                     {
                    
                     totalAmount = s[i].sActCap * h[6].amount;
                     
                     
                     printf("\n");
                     printf("Processing complete\n");
                     printf("####################\n");
                     printf("Harbour id# %d\n", h[6].harbourId);
                     printf("Arrive Time: %d\n", s[i].aTime);
                     printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
                     printf(" total payment $ %.2f\n", totalAmount); 
                     printf("\n");
                         
                         count6++;
                     }

                 }
                 else if(s[i].sMaxCap <= h[7].maxCap)
                 {
                     if(count7 <= 2)
                     {
                     
                     totalAmount = s[i].sActCap * h[7].amount;
                     
                     
                     printf("\n");
                     printf("Processing complete\n");
                     printf("####################\n");
                     printf("Harbour id# %d\n", h[7].harbourId);
                     printf("Arrive Time: %d\n", s[i].aTime);
                     printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
                     printf(" total payment $ %.2f\n", totalAmount); 
                     printf("\n");
                         
                         count7++;
                     }
                         
                 }
                 else if(s[i].sMaxCap <= h[8].maxCap)
                 {
                     if(count8 <= 2)
                     {
                         
                         totalAmount = s[i].sActCap * h[8].amount;
                    
                     printf("\n");
                     printf("Processing complete\n");
                     printf("####################\n");
                     printf("Harbour id# %d\n", h[8].harbourId);
                     printf("Arrive Time: %d\n", s[i].aTime);
                     printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
                     printf(" total payment $ %.2f\n", totalAmount); 
                     printf("\n");
                     
                         
                     count8++;
                     }
                 }
                 else if(s[i].sMaxCap <= h[9].maxCap)
                 {
                     if(count9 <= 2)
                     {
                     
                     totalAmount = s[i].sActCap * h[9].amount;
                     
                     
                     printf("\n");
                     printf("Processing complete\n");
                     printf("####################\n");
                     printf("Harbour id# %d\n", h[9].harbourId);
                     printf("Arrive Time: %d\n", s[i].aTime);
                     printf(" Ship id# %d Ship Capacity# %d\n", s[i].shipId, s[i].sActCap);
                     printf(" total payment $ %.2f\n", totalAmount); 
                     printf("\n");
                         
                         count9++;
                     }

                 }
  
                else if(s[i].sMaxCap >= h[0,1,2,3,4,5,6,7,8,9].maxCap)
                {
                    printf(" Ship id# %d Ship Capacity# %d Cannot be unload in this harbour\n", s[i].shipId, s[i].sActCap);
                }
    }
}

int main() 
{
    struct harbour h[100];
    struct ship s[100];
    
    readShip(s);
	readHarbour(h);    
    processing(h, s);
    return 0;
}

Thank

erm I forgot to take off but after I take off it still error

You don't have any space to store the file name read in. All you did is allocate a pointer but it doesn't point anywhere useful.

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