Thread: file reading problem

  1. #31
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by but View Post
    thanks a lot - pls explain the point of this line -

    printf("bad line: j=%d i=%d\n[%s]\n", j, i, line); return EXIT_FAILURE;
    Code:
                if (sscanf(line, "%*f %*f %lf %lf %lf", &x, &y, &z) != 3) {
                    printf("bad line: j=%d i=%d\n[%s]\n", j, i, line);
                    return EXIT_FAILURE;
                }
    If sscanf returns 3 it means that it properly read 3 fields. If it doesn't return 3 then something went wrong, so I print an error message and exit. EXIT_FAILURE is just a macro that's equal to a non-zero value appropriate for indicating failure on the system. It's equal to 1 on linux.

  2. #32
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    By the way after looking at the input file, it looks to me like you shouldn't be discarding the second line of each data set. This line appears to be the number of lines in the data set. This number really should be used in your loops instead of that magic number (13968). This way your program should read the file no matter how many lines are in the data set.

    Jim

  3. #33
    Registered User
    Join Date
    Jan 2016
    Posts
    39
    omg - i was sooooo correct when i said it was hydra headed !!

    the monster is still not slain, even after all your inputs - now the last (z) column is getting screwed up =

    Pls see the screen shot - all the integers from 0 to 10 are alternately showing up instead of the z value !!1

    file reading problem-issue2-jpg

  4. #34
    Registered User
    Join Date
    Jan 2016
    Posts
    39
    Quote Originally Posted by jimblumberg View Post
    By the way after looking at the input file, it looks to me like you shouldn't be discarding the second line of each data set. This line appears to be the number of lines in the data set. This number really should be used in your loops instead of that magic number (13968). This way your program should read the file no matter how many lines are in the data set.

    Jim
    my magic number IS that same number. the number is always the same for each set. it need not be read and certainly cannot be written in the final output file.

  5. #35
    Registered User
    Join Date
    Jan 2016
    Posts
    39
    is there a way to tell from the wrong answer its spitting out as to what the real problem could be - one time it was the position of the x (ie, 4, 8, 12 etc) instead of the value in the 4th, 8th place. SO that is easier to trace- this one is all random - but i think it could be the integer part of the complete value.

  6. #36
    Registered User
    Join Date
    Jan 2016
    Posts
    39
    i checked - yes its "rounding up" - so every 8.6 is becoming a 9 and so on.

    I havent checked to see if those z values where the fraction is less than .5 is getting rounded up or down.

    So i am guess this has to be with how its being read - we are reading an decimal number as an integer -

  7. #37
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    Please show your current code.

    Jim

  8. #38
    Registered User
    Join Date
    Jan 2016
    Posts
    39
    i think i could be the hydra myself - it was a typo from me!!!

    so pls ignore the last few posts

    NOBODY"S FAULT BUT MINE
    Last edited by but; 04-05-2016 at 11:46 PM.

  9. #39
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    my magic number IS that same number. the number is always the same for each set.
    It may be for now, but what if the program generating these data sets is changes to output more or less lines per data set?

    it need not be read and certainly cannot be written in the final output file.
    The line must be read, and then it can be parsed to replace your magic number still not be written to the final output file.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with reading from file... problem with strings
    By Ruben Marques in forum C Programming
    Replies: 58
    Last Post: 07-02-2014, 03:23 PM
  2. Problem with reading of file
    By skumarisation in forum C++ Programming
    Replies: 3
    Last Post: 05-28-2010, 06:01 AM
  3. file reading problem
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-19-2005, 08:58 AM
  4. Problem reading a file
    By caduardo21 in forum C Programming
    Replies: 8
    Last Post: 04-18-2005, 11:51 AM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM

Tags for this Thread