Reputation:
I am trying to read an array through input file in C. I am getting segmentation fault(core dumped) error after displaying array. Can anyone please solve this issue. Here is my c program
#include <stdio.h>
#include<stdlib.h>
int main()
{
int c, i, j, row, col, nl, cr,nt;
col = nl = cr = nt=0;
int k;
row=1;
FILE *fp = fopen("IO.txt", "r");
// Figure out how many rows and columns the text file has
while ((c = getc(fp)) != EOF)
{
if (c == '\n')
nl++;
if (c == '\r')
cr++;
if(c=='\t')
nt++;
col++;
if (c == '\n')
row++;
putchar(c);
}
//row=row+1;
col = (col - (nl + cr+nt));
col = (int) (col/row);
char **array = malloc(sizeof(char *) * row);
for(int k=0;k<row;k++)
{
array[c] = malloc(sizeof(char) * col);
}
if(fp)
{
for( ;; )
{
c = getc(fp);
if ( c == EOF )
{
break;
}
if ( c != '\n' && c != '\r' )
{
array[i][j] = c;
if ( ++j >= col )
{
j = 0;
if ( ++i >= row )
{
break;
}
}
}
}
fclose(fp);
}
for ( i = 0; i < row; i++ )
{
for ( j = 0; j < col; j++ )
{
putchar( array[i][j]);
}
putchar('\n');
}
free(array);
array=NULL;
return 0;
}
Here is my input file
IO.txt
0 0 0 0 0 0 0
0 0 1 0 2 0 0
0 1 0 4 3 6 0
0 0 4 0 0 5 0
0 2 3 0 0 7 8
0 0 0 0 8 0 9
0 0 0 0 8 9 0
Upvotes: 0
Views: 350
Reputation:
In this code:
for(int k=0;k<row;k++){
array[c] = malloc(sizeof(char) * col);
}
You should be using k
, not c
to access array
. c
is another variable entirely.
Your compiler should have warned you about this. Always take heed of warnings:
io.c: In function ‘main’: io.c:7:9: warning: unused variable ‘k’ [-Wunused-variable] int k; ^
Also, sizeof(char)
is explictly 1, so you never need to specify it.
So the loop should look like this:
for(int k=0;k<row;k++){
array[k] = malloc(col);
}
Also, you're forgetting to free the elements of array
before you free array
itself. You should do something like this:
for (int k=0; k<row; k++){
free(array[k]);
}
Also, you're not counting columns correctly. This code:
if(c=='\t')
nt++;
col++;
Will increment col
every time through the loop, because the col++
doesn't fall within the if(c=='\t')
. You need braces:
if (c =='\t') {
nt++;
col++;
}
Also, you've forgotten to close and then reopen your file, or seek back to the beginning, before you read it a second time. The if(fp)
doesn't do anything for you. You should do this:
/* Close and then reopen the file */
fclose(fp);
fp = fopen(fopen("IO.txt", "r"));
Or this:
/* Seek to the beginning of the file */
fseek(fp, 0, SEEK_SET);
Also, you forgot to initalise i
and j
to 0
. The compiler won't do it for you, so you need:
int c, i = 0, j = 0, row, col, nl, cr, nt;
Also, you're not counting columns or rows correctly. It's not very reliable to count rows by carriage returns, because there may not be a carriage return on the last line of the file, but assuming there is, initialise row
to 0
, and then the column calculation is:
col = (col / row) + 1;
Also, you're forgetting to check for tabs when you read the file the second time. The condition to add characters to your arrays should be:
if ( c != '\n' && c != '\r' && c != '\t')
That's all of the logical problems. The rest is just hygiene and style: check that the file opened successfully, check the result of malloc()
, etc.
Upvotes: 3