hello everyone... i ve got this txt file and i want to scan it and insert the 2nd row digits at profit[ncustomer] matrix, where ncustomer is the 1st digit which readed from file.
After that i want to add all the other digits in a 2 dimensional matrix dis[ncustomer][ncustomer] ...

this is the code i wrote but there is a problem with the dis[0][0] . All the other are OK
I heard that this is easier with pointer and ??malloc ????? function but i dont know these yet... if anyone can help me??
I ll be grateful...
thanks in advance!!

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

main()
{
      FILE*fp=fopen("txt.txt","r+");
      int i,j,k;                       
      int ncustomer;                
      float data;
      fscanf(fp,"%d",&ncustomer);
      printf("\nncustomer=%d\n",ncustomer);
      float profit[ncustomer];
      float dis[ncustomer][ncustomer];
      rewind(fp);                   
      
//=================================
// read this file and import the data in profit[ncustomer] and dis[ncostumer][ncostumer]
//=================================
       i=1;                        
      
      while((fscanf(fp,"%f",&data))!=EOF)
      {
            if(i==0)
            {
                   continue ;          
            }
            else if (i==1)
            {
                   for(j=0;j<ncustomer;j++)
                   {
                        fscanf(fp,"%f",&profit[j]);
                   }
                  
            }
            else
            {
                for(k=0;k<ncustomer;k++)
                {
                   for(j=0;j<ncustomer;j++)
                   {
                        fscanf(fp,"%f",&dis[k][j]);
                   }
                }
            }
      i++;
      }
      fclose(fp);
//=================================      
//  matrixes print
     printf("\nprofit=\n");
     for(j=0;j<ncustomer;j++)
     {
          printf("%.1f\t",profit[j]);
     }

     printf("\ndis=\n");
     for(i=0;i<ncustomer;i++)
     {
          for(j=0;j<ncustomer;j++)
          {
              printf("%.1f\t",dis[i][j-1]);
          }     
          printf("\n");
     }

//=================================
      
      getchar();
}

Recommended Answers

All 6 Replies

Any idea what "a problem with the dis[0][0]" is? Since I didn't run the program I can't tell what it might be.

Member Avatar for Mouche
for(i=0;i<ncustomer;i++)
     {
          for(j=0;j<ncustomer;j++)
          {
              printf("%.1f\t",dis[i][j-1]);
          }     
          printf("\n");
     }

In this section, the first printf() executes when i = 0 and j = 0. That means it will try to print d[0][-1]. That is outside the bounds of your array.

On an unrelated note, why did you use rewind()? If the first integer in the file is the number of customers, then you don't want to rewind and read that in again as customer data.

my fault, i think that i attached the .txt file
so, tis is the file i want to read, the 1st element is the 'ncustomer', the row below is the profit[ncustomer], and the other elements are the
dis[ncustomer][ncustomer]
the problem is that, in dis[][] the element which must be dis[0][0]= 12 , doesnt printed as 12 but with other number like :12315351351000000...

6
1	2	3	4	5	6
12	32	9	10	11	12
13	14	15	16	17	18
19	20	18	22	23	24
45	26	27	28	29	30
31	32	33	34	35	36
37	38	39	55	41	90

for(i=0;i<ncustomer;i++)
{
for(j=0;j<ncustomer;j++)
{
printf("%.1f\t",dis[j-1]);
}
printf("\n");
}

In this section, the first printf() executes when i = 0 and j = 0. That means it will try to print d[0][-1]. That is outside the bounds of your array.

i made this with trial and error method and the dis[j-1] display my the best solution, from my completely falt solutions...

Member Avatar for Mouche

I don't recommend programming by trial and error. If you don't understand the File I/O functions and how to traverse arrays, then read up on them before you use them.

Here's how you can get your code working:

1) Remove the rewind()

2) Remove the while loop "while((fscanf(fp,"%f",&data))!=EOF)" and just leave the block inside of it

3) Remove all the if statements inside that while loop and just leave the for loops

thanks you very much. and when i said "trial and error method" i dont mean that i made all the source code like this. i started writting the ...!=EOF {} because with this way always, alwayws??the last 4months which im learning C, used to use the fscanf. and also i used rewind because without it displaying me a problem at compile...

nevertheless thanks you very much

Member Avatar for Mouche

Using rewind() may have helped you avoid a compiler error, but that was not the solution. Your while loop condition would have been appropriate if you were reading one float into the variable "data" at a time, but you weren't. You never even used "data". You were only using the for loops to get useful data. The fscanf() in the while condition was reading some data you were expecting to read in the for loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.