Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array and pointer

Status
Not open for further replies.

kreop

Technical User
Oct 9, 2005
14
JP
Code:
goon.dat
[COLOR=red]1.23[/color], 2.34, 3.45
[COLOR=red]1.34[/color], 2.12, 3.56
[COLOR=red]1.44[/color], 4.56, 6.78

Code:
double data[34][4];
void read_data()
{
	char file_name[30]="goon.dat";
	int i,j;
	FILE  *fp;
	double X,Y,Z;
	
	fp=fopen(file_name,"r");
	if(fp==NULL){
		printf("Cannot Open File?\n");
		exit(1);
	}
for(i=0;i<34;i++){
	j=0;
	X=data[i][j];
	Y=data[i][j+1];
	Z=data[i][j+2];
	
fscanf(fp,"%lf,%lf,%lf\n", &data[i][j],&data[i][j+1],&data[i][j+2]);	 printf("%f,%f,%f\n",data[i][j],data[i][j+1],data[i][j+2]);
double a[]={data[i][j]};
printf("%f",a[0]);
     }
fclose(fp);	
}

After run the codes, result showed
Code:
[COLOR=red]1.23[/color],[COLOR=red] 1.34[/color],[COLOR=red] 1.44[/color]

Why not the result is 1.23.I tried to get each value using pointer or array.
 
The result of the snipped above is as expected, but you must check end of file condition, for example:
Code:
if (fscanf(...) != 3) /* next 3 elements assigned? */
   break;

What's your result? This program prints 4 numbers on every loop (with a slightly strange alignment: no \n in the last fprintf).

Why so exotic:
Code:
    j=0;
    X=data[i][j];
    Y=data[i][j+1];
    Z=data[i][j+2];
/* it's exactly: */
    X = data[i][0];
    Y = data[i][1];
    ...
Don't use all-capital var names: as usually (traditionally), it's macros name.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top