• C

Read from file not working

Dear experts,

When I output the information from a file the screen fills with 0s not the values that I want there.

The code is:
#include <stdio.h>
char places[100];
     int distances[10][10];
     struct places
    {
       char places[10];
     };
     int menu;
     char userinput1[10];
     char userinput2[10];
     int cost;
     int other;
     int a;
     int b;
     struct places names[10];

main()
{
     FILE *fp;/*creates a file pointed named fp*/
     
/* reading in data from file*\
  do
  {
     printf("Enter file name");
     scanf("%s", places);
     fp = fopen(places,"r");
     if (fp == NULL)
     printf("File %s does not exist\n",place);
  }
  while(fp ==NULL);

  while(feof(fp) ==0)
{
   for(int i = 0; i < 10; i++)
   {
     fscanf(fp, "%s", &names[i].places);
   }
     for(int i = 0; i < 10; i ++)
     {
       for(int j = 0; j < 10; j++)
       {
         fscanf(fp, "%d", &distances[i][j]);
       }
     }

}


/* menu */

do
{
   printf("what would you like to do\n");
   printf("option 1:display mileage table");
   printf("option 2: calulate distances between points");
   printf("option 3: calulate cost of journy");
   printf("option 4: exit");
   scanf("%d", &menu);

 switch(menu)
 {
  case 1:
         display(distances, names);
         break;
  case 2:
        distance(distances, names);
        break;
  case 3:
        calc(distances,names);
        break;
  case 4:
        exit(0);
        break;
 }

}
while(menu<5 && menu > 0);

}

/* display mileage chart*/

display( distances[10][10], struct places names[10])
{
printf(" mileage chart");
printf("Burton Derby Swad Stoke Staff Lincoln Walsall Telford Cannock Warwick”)

for(int i = 0; i<10; i++)
{
  for(int j = 0; j < 10; j++)
  {
   printf("%7s",names[i].places);
   printf("%7d", distances[i][j]);

   }
}
}

/*distance between points*/
distance( distances[10][10], struct places names[10])
{

printf("Burton");
printf("Derby");
printf("Swad");
printf("Stoke");
printf("Stafford");
printf("Lincoln");
printf("Walsall");
printf("Telford");
printf("Cannock");
printf("Warwick");

printf("Enter the first point from the above list");
scanf("%s", &userinput1);

for (int i = 0; i<10; i++)
{
  if(strcmp(userinput1, names[i].places) ==0)
  a = i;
 
   else
   {
        printf("Please enter a place from the list");
   }

printf("Enter the second point from the above list");
scanf("%s", &userinput2);

for(int j = 0; j <10;j++)
{
 if(strcmp(userinput2, names[j].places) == 0)
b = j;

else
{
     printf("Please enter a place from the list");
}

printf("The distance is %d", distances[a][b]);

}

/*calculations between points*/
calc( distances[10][10], struct places names[10])
{
printf("Burton");
printf("Lincoln");
printf("Walsall");
printf("Telford");
printf("Cannock");
printf(“Warwick”);
printf("Derby");
printf("Swad");
printf("Stoke");
printf("Stafford");

printf("Enter the first point from the list");
scanf("%s", userinput1);

for (int i = 0; i <10; i++)
{
  if(strcmp(userinput1, names[i].places) == 0)
  a = i;

  else
  {
    Printf(“Please enter a point from the list”);
  }
}


printf("Enter the second point from the list");
scanf("%s", userinput2);

for(int j = 0; j < 10; j++)
{
   if(strcmp(userinput2, names[j].places) ==0)
   b = j;

   else
   {
      Printf(“Enter a point from the list”);
   }
}

if(distances[a][b] <= 100)
{
    cost = (distances[a][b] * 40)/100;
     printf("The cost is %d", cost);
}
else
{
   other = distances[a][b] - 100;
   other = other * 30;
    cost = (100 * 40) + other;
     cost = cost/100;
      printf("The cost of the journy is %d", cost);
  }
}

The file is:

Burton Derby Swad Stoke Stafford Lincoln Walsall Telford Cannock Warwick

0 122 233 100 399 188 522 188 923 912
122 0 121 188 299 982 847 122 388 123
233 121 0 290 111 182 293 192 389 199
100 188 290 0 222 123 432 178 901 231
399 299 111 222 0 839 122 281 112 941
188 982 182 123 839 0 144 542 486 323
522 847 293 432 122 144 0 451 222 114
188 122 192 178 281 542 451 0 177 873
923 388 389 901 112 486 222 177 0 175
912 123 199 231 941 323 114 873 175 0

Thank you
Halt123Asked:
Who is Participating?
 
grg99Commented:
A few things:

Your code that asks for place names does search the list, but then for each non-match it prints "please enter a name from the list".
You need to rearrange the logic there.  Also need to loop back and ask again.

Also you're not reading the ending \n from each line, so the second read is unlikely to work correctly.

also you don't initialize "a" or "b" to some impossible value, so the code is falling through and using improper balues for a and b sometimes.

How about you check the return value from fscanf?  Very useful information there.

0
 
ozoCommented:
#include <stdio.h>
char places[100];
     int distances[10][10];
     struct places
    {
       char places[10];
     };
     int menu;
     char userinput1[10];
     char userinput2[10];
     int cost;
     int other;
     int a;
     int b;
     struct places names[10];

main()
{
     FILE *fp;/*creates a file pointed named fp*/
     
     /* reading in data from file*/
  do
  {
     printf("Enter file name");
     scanf("%s", places);
     fp = fopen(places,"r");
     if (fp == NULL)
     printf("File %s does not exist\n",places);
  }
  while(fp ==NULL);

  while(feof(fp) ==0)
{
   for(int i = 0; i < 10; i++)
   {
     fscanf(fp, "%s", &names[i].places);
   }
     for(int i = 0; i < 10; i ++)
     {
       for(int j = 0; j < 10; j++)
       {
         fscanf(fp, "%d", &distances[i][j]);
       }
     }

}


/* menu */

do
{
   printf("what would you like to do\n");
   printf("option 1:display mileage table");
   printf("option 2: calulate distances between points");
   printf("option 3: calulate cost of journy");
   printf("option 4: exit");
   scanf("%d", &menu);

 switch(menu)
 {
  case 1:
         display(distances, names);
         break;
  case 2:
        distance(distances, names);
        break;
  case 3:
        calc(distances,names);
        break;
  case 4:
        exit(0);
        break;
 }

}
while(menu<5 && menu > 0);

}

/* display mileage chart*/

display( int distances[10][10], struct places names[10])
{
printf(" mileage chart");
 printf("Burton Derby Swad Stoke Staff Lincoln Walsall Telford Cannock Warwick");

for(int i = 0; i<10; i++)
{
  for(int j = 0; j < 10; j++)
  {
   printf("%7s",names[i].places);
   printf("%7d\n", distances[i][j]);
   }
}
}

/*distance between points*/
distance( int distances[10][10], struct places names[10])
{

printf("Burton");
printf("Derby");
printf("Swad");
printf("Stoke");
printf("Stafford");
printf("Lincoln");
printf("Walsall");
printf("Telford");
printf("Cannock");
printf("Warwick");

printf("Enter the first point from the above list");
scanf("%s", &userinput1);

for (int i = 0; i<10; i++)
{
  if(strcmp(userinput1, names[i].places) ==0)
  a = i;
 
   else
   {
        printf("Please enter a place from the list");
   }

printf("Enter the second point from the above list");
scanf("%s", &userinput2);

for(int j = 0; j <10;j++)
{
 if(strcmp(userinput2, names[j].places) == 0)
b = j;

else
{
     printf("Please enter a place from the list");
}

printf("The distance is %d", distances[a][b]);

}
}
}
/*calculations between points*/
calc( int distances[10][10], struct places names[10])
{
printf("Burton");
printf("Lincoln");
printf("Walsall");
printf("Telford");
printf("Cannock");
printf("Warwick");
printf("Derby");
printf("Swad");
printf("Stoke");
printf("Stafford");

printf("Enter the first point from the list");
scanf("%s", userinput1);

for (int i = 0; i <10; i++)
{
  if(strcmp(userinput1, names[i].places) == 0)
  a = i;

  else
  {
    printf("Please enter a point from the list");
  }
}


printf("Enter the second point from the list");
scanf("%s", userinput2);

for(int j = 0; j < 10; j++)
{
   if(strcmp(userinput2, names[j].places) ==0)
   b = j;

   else
   {
      printf("Enter a point from the list");
   }
}

if(distances[a][b] <= 100)
{
    cost = (distances[a][b] * 40)/100;
     printf("The cost is %d", cost);
}
else
{
   other = distances[a][b] - 100;
   other = other * 30;
    cost = (100 * 40) + other;
     cost = cost/100;
      printf("The cost of the journy is %d", cost);
  }
}
0
 
Halt123Author Commented:
>>>.printf("%7d\n", distances[i][j]);
is this the only part you have changed,
I've changed it to that but now it just prints out a long line of 0s

thank you for your help
0
The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

 
ozoCommented:
I changed several other things that kept the original from compiling.
With the changes it compiled and printed
Enter file name21803262.file
what would you like to do
option 1:display mileage tableoption 2: calulate distances between pointsoption 3: calulate cost of journyoption 4: exit1
 mileage chartBurton Derby Swad Stoke Staff Lincoln Walsall Telford Cannock Warwick Burton      0 Burton    122 Burton    233 Burton    100 Burton    399 Burton    188 Burton    522 Burton    188 Burton    923 Burton    912
  Derby    122  Derby      0  Derby    121  Derby    188  Derby    299  Derby    982  Derby    847  Derby    122  Derby    388  Derby    123
   Swad    233   Swad    121   Swad      0   Swad    290   Swad    111   Swad    182   Swad    293   Swad    192   Swad    389   Swad    199
  Stoke    100  Stoke    188  Stoke    290  Stoke      0  Stoke    222  Stoke    123  Stoke    432  Stoke    178  Stoke    901  Stoke    231
Stafford    399Stafford    299Stafford    111Stafford    222Stafford      0Stafford    839Stafford    122Stafford    281Stafford    112Stafford    941
Lincoln    188Lincoln    982Lincoln    182Lincoln    123Lincoln    839Lincoln      0Lincoln    144Lincoln    542Lincoln    486Lincoln    323
Walsall    522Walsall    847Walsall    293Walsall    432Walsall    122Walsall    144Walsall      0Walsall    451Walsall    222Walsall    114
Telford    188Telford    122Telford    192Telford    178Telford    281Telford    542Telford    451Telford      0Telford    177Telford    873
Cannock    923Cannock    388Cannock    389Cannock    901Cannock    112Cannock    486Cannock    222Cannock    177Cannock      0Cannock    175
Warwick    912Warwick    123Warwick    199Warwick    231Warwick    941Warwick    323Warwick    114Warwick    873Warwick    175Warwick      0
what would you like to do
option 1:display mileage tableoption 2: calulate distances between pointsoption 3: calulate cost of journyoption 4: exit
0
 
Halt123Author Commented:
Could you point out everything you have changed please, sorry but it still seems to be printing a line of 0s just wondered if I have missed something
0
 
Halt123Author Commented:
>>>>>Your code that asks for place names does search the list, but then for each non-match it prints "please enter a name from the list".
You need to rearrange the logic there.  Also need to loop back and ask again.

How could I go about doing this, im a bit confused

thank you
0
 
ozoCommented:
You might want to take grg99's suggestion to check the return value from fscanf
     for(int i = 0; i < 10; i ++)
     {
       for(int j = 0; j < 10; j++)
       {
         if( !fscanf(fp, "%d", &distances[i][j]) ){
           printf("scanf distances[%d][%d] failed\n",i,j);
         }
       }
     }
0
 
Halt123Author Commented:
My program seems to be missing out asking the user for the file name.

The code is:

#include <stdio.h>
  char places[100];
  int distances[10][10];
struct places
{
  char places[100];
};
char userinput1[10];
char userinput2[10];
int cost;
int a = 0;
int other;
int b = 0;
int menu;

struct places names[10];

main()
{
  FILE *fp;/*creates a file pointed named fp*/



/* reading in data from file*\
  do
  {
   printf("Enter file name");
    scanf("%s", places);
    fp = fopen(places,"r");
    if (fp == NULL)
   printf("File %s does not exist\n",place);
  }
  while(fp ==NULL);


while(feof(fp) ==0)
{
   for(int i = 0; i < 10; i++)
   {
     fscanf(fp, "%s", &names[i].places);
   }

     for(int i = 0; i < 10; i ++)

     {
       for(int j = 0; j < 10; j++)
       {
        if(!fscanf(fp, "%d", &distances[i][j]))
          {
             printf("scanf dustances[%d][%d]failed\n,i,j);
           }
       }
     }

}




/* menu */

do
{
printf("\n");
printf("what would you like to do\n");
printf("option 1:display mileage table\n");
printf("option 2: calulate distances between points\n");
printf("option 3: calulate cost of journy\n");
printf("option 4: exit\n");
scanf("%d", &menu);


switch(menu)
{
case 1:
       display(distances, names);
       break;
case 2:
        distance(distances, names);
        break;
case 3:
        calc(distances,names);
        break;
case 4:
        exit(0);
        break;
}

}
while(menu<5 && menu > 0);

}
/* display mileage chart*/

 display(int distances[10][10], struct places names[10])
{
printf(" mileage chart\n");

printf("Burton Derby Swad Stoke Staff Lincoln Walsall Telford Cannock Warwick");

 for(int i = 0; i<10; i++)
 {

   printf("%7s",names[i].places);

  for(int j = 0; j < 10; j++)
  {
   printf("%7d", distances[i][j]);
   }

  }

}


/*distance between points*/
 distance(int distances[10][10], struct places names[10])
{

printf("Burton\n");
printf("Derby\n");
printf("Swad\n");
printf("Stoke\n");
printf("Stafford\n");
printf("Lincoln\n");
printf("Walsall\n");
printf("Telford\n");
printf("Cannock\n");
printf("Warwick\n");

printf("Enter the first point from the above list\n");
scanf("%s", &userinput1);

for (int i = 0; i<10; i++)
{
  if(strcmp(userinput1, names[i].places) ==0)
 a = i;
 }
     printf("Enter the second point from the above list\n");
     scanf("%s", &userinput2);

 for(int j = 0; j <10;j++)
 {
    if(strcmp(userinput2, names[j].places) == 0)
     b = j;
 }
      printf("The distance is %d\n", distances[a][b]);

}

/*calculations between points*/
 calc(int distances[10][10], struct places names[10])
{
printf("Burton\n");
printf("Lincoln\n");
printf("Walsall\n");
printf("Telford\n");
printf("Cannock\n");
printf("Warwick\n");
printf("Derby\n");
printf("Swad\n");
printf("Stoke\n");
printf("Stafford\n");

printf("Enter the first point from the list");
scanf("%s", userinput1);

 for (int i = 0; i <10; i++)
 {
     if(strcmp(userinput1, names[i].places) == 0)
       a = i;
  }

printf("Enter the second point from the list");
scanf("%s", userinput2);

for(int j = 0; j < 10; j++)
 {
      if(strcmp(userinput2, names[j].places) ==0)
      b = j;
 }

if(distances[a][b] <= 100)
{
     cost = (distances[a][b] * 40)/100;
      printf("The cost is %d", cost);
 }
  else
  {
       other = distances[a][b] - 100;
       other = other * 30;
        cost = (100 * 40) + other;
        cost = cost/100;
        printf("The cost of the journy is %d", cost);
    }

}
0
 
Halt123Author Commented:
Do i need to add an fclose in somewhere?
0
 
Halt123Author Commented:
I've changed the output file code to

int main(void)
{
  FILE *fp;/*creates a file pointed named fp*/
  fp = fopen("places.c", "r");

   for(int i = 0; i < 10; i++)
  {
         for(int j = 0; j <10; j++)
          {
              fscanf(fp, "%d", &distances[i][j]);
           }

    }

however its still just ouputting 0s

0
 
ozoCommented:
What does places.c contain?
      fscanf(fp, "%d", &distances[i][j]);
will fail when it sees
Burton Derby Swad Stoke Stafford Lincoln Walsall Telford Cannock Warwick
0
 
Halt123Author Commented:
Burton Derby Swad Stoke Stafford Lincoln Walsall Telford Cannock Warwick

0 122 233 100 399 188 522 188 923 912
122 0 121 188 299 982 847 122 388 123
233 121 0 290 111 182 293 192 389 199
100 188 290 0 222 123 432 178 901 231
399 299 111 222 0 839 122 281 112 941
188 982 182 123 839 0 144 542 486 323
522 847 293 432 122 144 0 451 222 114
188 122 192 178 281 542 451 0 177 873
923 388 389 901 112 486 222 177 0 175
912 123 199 231 941 323 114 873 175 0
0
 
Halt123Author Commented:
I like the outuput to look the same as the file as well
0
 
Halt123Author Commented:
But I cant seem to be able to write the code to do it

thank you for all your help
0
 
ozoCommented:
Burton does not look like "%d", so the fscanf(fp, "%d", &distances[i][j]) will fail
0
 
Halt123Author Commented:
i've tried to run the code you have wrote but its throwing the error
invalid statement. <badstmt>

thank you for your help
0
 
imladrisCommented:
Note that your comment:

/* reading in data from file*\


is not terminated correctly. The effect will be that all the code until the menu comment is treated as a comment.
0
 
imladrisCommented:
Did any of that help?

If so, then it is now time to grade the answer(s) and close the question.

If not, perhaps a clarifying question would be of assistance.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.