1

I am trying to read values from a file and after some operation write to another file. Here facing a slight issue as I am also trying to save values in a 2D Array and displaying it. My file read and file write are showing correct results but my program throws an exception when it comes to display matrix part.

#include <stdio.h>
#include <ctype.h>

#ifndef NULL
#define NULL   ((void *) 0)
#endif

int main(void)
{
    FILE *file  = NULL; //for file read
    FILE *fptr  = NULL; //for file write

    int mat[182][274];

    // code to read and display number from file
    // open file for reading
    file = fopen("file.txt", "r");
    fptr = fopen("file1.txt", "w");
    int i = 0,j=0;

    fscanf (file, "%d", &i);    
    while (!feof (file))
    {
        symbol = fgetc(file);

        if (symbol == '\n' || feof(file))
        {
            fprintf (fptr,"\n");
            printf("\n");
        }
        else{
            j=255-i;

            mat[i][j]=j;

            fprintf (fptr,"%d ", j);
            fprintf (fptr," ");
            printf ("%d ", j);
        }

        fscanf (file, "%d", &i);

   }
   fclose (file);
   fclose (fptr);


   //Facing issue in this part
   int k;
   int l;
   for (k=0;k<=182;k++)
   {

       for(l=0;l<=274;l++)
       {

           printf("%d ", mat[k][l]);
       }
   }

   return 0;
}
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
  • 1
    while printing inside `for` loop at the end of code, this `mat[i][j]` --> `mat[k][l]`. Also `k<=182` --> `k < 182` and `l<=274` --> `l < 274` as `<=` tends to access out of bound array elements, causes undefined behaviour. – Achal Dec 26 '18 at 18:38
  • done that . Still program terminates unexpectedly. I guess it has some thing to do with mat[i][j]=j; line. This is source of the problem. – Arslan Majid Dec 26 '18 at 18:42
  • 1
    The dimensions of array `int mat[182][274];` is strange, you are indexing as `mat[i][j]=j;` when `j=255-i;` So `j` can never be `> 255` unless `i < 0` in which case you have problems with the array indexing. – Weather Vane Dec 26 '18 at 18:43
  • may be j=255-i is just an operation. Lets say i have a value 40 in file number 1. 40 will be subtracted from 255 and the result will be stored in second file. Now i want this number to be stored at matrix location (0,0) and so on repeat this process till the end of the loop. – Arslan Majid Dec 26 '18 at 18:52
  • 1
    As an aside, you might take a look at _[why !feof(file) is always problematic](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong)_, and not recommended. – ryyker Dec 26 '18 at 18:53
  • @ryyker that's true, but in this case `feof` is used to check the validity of the previous read operation. Still not to be recommended, it makes for awkward code. – Weather Vane Dec 26 '18 at 19:17

1 Answers1

4

Arrays in C start at 0 and end at (array_size - 1).

As you're accessing memory just outside the array, you're most likely experiencing segmentation faults.

To fix this issue, change these lines:

 for (k=0;k<182;k++)
 {

  for(l=0;l<274;l++)
  {

      printf("%d ", mat[k][l]);
  }
 }

Notice that I changed the relational operators from <= and >= to < and >, respectively.

Along with that, you may need to fully initialize your array. Odd values may be printed if the array is not initialized. (@Weather Vane).

However, to best be sure if this is the case, we need file.txt and file1.txt.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76