c - Scanning double number in a matrix -
i scan matrix text file seems code wrote works integer not float.
#include <stdlib.h> #include <stdio.h> #define max 10000 int main(void) { file *fp; int i, j; char s[max], ch; if ((fp = fopen("3.txt", "r")) == null) { printf("can not open file!\n"); exit(1); } int row = 0; while (fgets(s, max, fp) != null) //count line row++; rewind(fp); // int col = 0; //count col while (ch != '\n') //(ch=fgetc(fp))!='\n'&&(ch=fgetc(fp))!='\r' { if (ch == ' ') col++; ch = fgetc(fp); } col++; // double** jz = malloc(row * sizeof(double*)); //allocate spaces (i = 0; < row; i++) jz[i] = malloc(col * sizeof(double)); rewind(fp); (i = 0; < row; i++) //read in data (j = 0; j < col; j++) { if (!fscanf(fp, "%lf", &jz[i][j])) { break; } } (i = 0; < row; i++) //print matrix (j = 0; j < col; j++) { printf("%lf\t", jz[i][j]); if (j + 1 == col) printf("\n"); } fclose(fp); }
for example, 1*3 matrix 56.154 59.365 98.3333
appear 0.0000
i know did wrong.
Comments
Post a Comment