Thread: Reading numbers from a file, how can I do it?

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    14

    Reading numbers from a file, how can I do it?

    Hi all,
    I have a file of thousand of lines and with some numbers on every lines as:
    161 110 139 139 161 139 110 161 161 110 161 195 220 139 139 139 161
    208 139 69 110 139 110 139 161 161 110 139 139 179 139 161 139 110
    425 429 451 423 465 395 472 448 416 507 466 464 432 463 494 458 484
    ...
    ...
    ...
    289 248 220 161 161 110 179 139 139 110 110 161 161 139 161 139 179
    366 364 340 256 283 208 240 208 195 208 179 208 208 195 220 208 264
    271 300 347 300 304 294 337 294 277 309 300 326 314 309 322 277 283

    I can't know how much lines I have.
    Ihave simply to put all the values in a NxM matrix of float.
    I can't solve on how to find the end of line without using chars...
    This is the code I used:

    while(res=fscanf(pFile, "%f",&value)!=EOF){
    while(res!="\n"){
    printf ("%d",(int)*value);
    res=fscanf(pFile, "%f",&value);
    }
    printf("\n");
    }

    it doesn't work! res cannot be compared to "\n"...

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    14
    and... another question..
    is this code correct to allocate space for the matrix containing the datas?

    int dimN=5000,dimM=1000;
    float **arrayF;

    arrayF= (float**) malloc(dimN* sizeof(float *));
    for (i=0;i<dimN;i++)
    arrayF[i]=(float *) malloc(dimM*sizeof(float));

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Learn operator precedence.
    What does fscanf() returns ?
    Did you read the documentation of the function?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem reading numbers in a txt file
    By nimamc in forum C Programming
    Replies: 3
    Last Post: 06-03-2009, 02:35 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM