-
c/c++ help needed
This code is supposed to read the first four lines of a .dat file that looks like this"
-5.0 1.0 -4.0 -0.5
3.0 1.5 -3.0 2.1
-3.1 2.7 2.5 -4.2
1.6 0.3 2.4 -2.9
1.0 1.2 -1.0 -0.5
after compiling my code its reads out the 2nd-5th line and says segmentation failed core dumped, what should i do ?? please help!!!
this is just a function out of my code:
Code:
void problem(void)
{
FILE *Inf2;
int i, j, rows=4, cols=4, dim, linecount=0;
char line2[100];
double sum, **a, *b; /*for normalizing*/
dim=rows;
/*
if(cols > rows) dim = cols;
*/
Inf2 = fopen("matrix.dat", "r");
while(fgets(line2,100,Inf2) !=NULL)
{
linecount++;
//if(linecount <=4)
do{
//double **a;
a=(double**)calloc(dim, sizeof(double *));
if(a==NULL)
{
exit(2);
}
for(i=1; i<rows; i++)
{ a[i]=(double *)calloc(dim, sizeof(double));}
if(a[i]=NULL)
{
exit(3);
}
for(i=0;i<rows; i++)
for(j=0;j<cols; j++)
//*(a[i]+j)=(i+1)*(j+2);
fscanf(Inf2, "%lf", a[i]+j);
for(i=0;i<=rows; i++) /* it was i <rows originally*/
{
printf("a[%2d][]=",i);
for(j=0;j<cols; j++)
{
printf("%7.2f \n",a[i][j]);
if((j+1)%10 == 0) printf("\n");
//printf("a[%d][]=%d", i, *(a[i]+j));
//if((j+1)==cols)printf("\n");
}
}
}while (linecount != 5);
*/
fclose(Inf2);
//fclose(Inf2);
for(rows=0; rows<4; rows++) free(a[rows]);
free(a);
free(b);
return;
}
-
I added code tags, but it didn't improve matters.
SourceForge.net: Indentation - cpwiki
-
It's either C or C++. You can't have both, so pick one.