-5
#include<stdio.h>

void main()
{
    FILE *a[10];  
    int i,j,k;
    float b[10][4][4];
    for(i=0;i<8;i++)
    {
        char filename[100];
        sprintf(filename,"infile%d.txt",i);
        a[i]=fopen(filename,"r");
    }

    for(i=0;i<8;i++)
    {
        for(j=0;j<2;j++)
        {
            for (k=0;k<3;k++)
            { 
                fscanf(a[i],"%f",&b[i][j][k]);
            }
        }
    }

    for (i=0;i<8;i++)
    {
        printf("\n-----------------%d--------------------",i);
        for(j=0;j<2;j++)
        {
            for(k=0;k<3;k++)
            {
                printf("\nb[%d][%d][%d]=%f",i,j,k,b[i][j][k]);
            }
        }
    }
}

I have written the above code in C. Which just reads 8 different files and print it in terminal. The file name is infile0, infile1 and son on up to infile7. Though the code runs, it shows segmentation fault core dumped in the terminal. I couldn't figure out why this happens at all. Can some one help me figure out the mistake in the code.

r3mus n0x
  • 5,954
  • 1
  • 13
  • 34
user135580
  • 105
  • 1
  • 7

2 Answers2

3

Compiling with -g and running trough valgrind gives an error line 19 when no files are present.
Maybe you should add

if(!a[i])continue;

before scanf. (and set some message or default values).

also have a look at Input validation using scanf(), as you should check scanf return value.

I hope it helps, this my my first so contribution.

EDIT: the -g flag is for your compiler, e.g

gcc -g myfile.c -o myfile

then run your binary trough valgrind.
It will tell you any problems with memory: leaks, invalid red/write...

valgrind ./myfile

Don't forget to install valgrind if it is not on your system.

fennecdjay
  • 56
  • 1
  • 6
1

A lot of remarks can be done from your code

1) In

 void main()

main returns an int, not void, so must be

int main()

2) In

 FILE *a[10];  
 ..
 for(i=0;i<8;i++)
 {
    ...
    a[i]=fopen(filename,"r");

why do you have an array with 10 entries to only use 8 of them ?, better to have

  FILE *a[8];

3) 10 (becoming 8) is used a lot of times in your code, if you decide to change the number of elements you will have to change it every where, it is more simple to use a #define or sizeof(a)/sizeof(a[0])

4) In

char filename[100];
sprintf(filename,"infile%d.txt",i);

you are very generous with the needed size, even your int are in 64b and you increase so much the number of entries in a (no change to have enough stack nor file description anyway) 20 digits are enough for a positive number, so char filename[20+10+1]; is enough

5) In

float b[10][4][4];
...
for(j=0;j<2;j++)
{
   for (k=0;k<3;k++)

like for 2) there is no reason to use a so large array if you do not use all the entries

6) In

 fscanf(a[i],"%f",&b[i][j][k]);
  • you do not check if the corresponding file was open so if a[i] is not NULL, probably your segmentation fault comes because a file wasn't open

  • you do not detect the end of file nor if the file doesn't contain a valid float, you need to do for instance if (fscanf(a[i],"%f",&b[i][j][k]) != 1) { ...error management... }

bruno
  • 32,421
  • 7
  • 25
  • 37