Thread: wrong result,while debugger shows it right

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    47

    wrong result,while debugger shows it right

    i had made a thread yesterday where i had problems with passing values to a struct. Now, i fixed that but, i can't open the files.(i have started programming on linux)

    Here is the code
    Code:
    void read_inputs(bankT array[],int size,FILE* input_file){
        int i,nscan;
        char termch;
        for(i=0; i<size; i++){
            nscan = fscanf(input_file,"%30[^,], %d, %lf, %d%c",array[i].epwnymo,&array[i].ypolipo,&array[i].epitokio,&array[i].hmeromhnia,&termch);
            if (nscan == EOF) break;
            if (nscan != 5 || termch != '\n'){
                Error("Improper file format");
            }
        }
    I get the "Improper file format" even though the results with debugging are:
    Code:
     nscan = 5
    termch= 10'\n'
    Sorry for many posts, but since i am new to linux, i have some troubles

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, that's tough. As a temporary debugging measure, try printing the value of nscan and termch to see if they match with what your debugger tells you.

    Instead of this termch idea, maybe it would be easier if you use fgets to read the line, then sscanf to parse it? You would also be better able to differentiate between an error in reading and an error in parsing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    47
    if i remove this :
    Code:
     if (nscan != 5 || termch != '\n'){            Error("Improper file format");
            }
    It works fine, and I can continue to next steps of my exercise, but I need a warning in case someone makes a mistake.

    With this:
    Code:
        printf("%d %c",nscan,termch);
    The result is:
    Code:
     //5
    //5

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, what about this instead?
    Code:
    void read_inputs(bankT array[], int size, FILE* input_file) {
        char line[BUFSIZ];
        int i;
        for (i = 0; i < size && fgets(line, sizeof(line), input_file); i++) {
            if (sscanf(line, "%30[^,], %d, %lf, %d", array[i].epwnymo, &array[i].ypolipo, &array[i].epitokio, &array[i].hmeromhnia) != 4) {
                Error("Improper file format");
            }
        }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Please post runnable programs instead of just snippets. What you describe in your first post is, on the surface, impossible, which suggests you've left out something important. You should also show your input file. Try doing a cat -E of it (which displays a $ at the end of each line) to see if there's extraneous space at the end of any lines.

    laserlight's suggestion does not quite do the job you seem to be requesting as it doesn't ensure that there are no extra characters on the line.

    Maybe something like this:
    Code:
    void read_inputs(bankT array[], int size, FILE* input_file){
      int i, nscan, termch;
      for (i = 0; i < size; i++){
        nscan = fscanf(input_file, "%30[^,], %d, %lf, %d",
                       array[i].epwnymo, &array[i].ypolipo,
                       &array[i].epitokio, &array[i].hmeromhnia);
        if (nscan == EOF) break;
        while (isspace(termch = fgetc(input_file)) && termch != '\n')
          ;
        if (nscan != 4 || termch != '\n')
          Error("Improper file format\n");
      }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong result.
    By zaiken in forum C Programming
    Replies: 4
    Last Post: 11-12-2014, 08:19 AM
  2. wrong variable value from debugger on x64
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 07-11-2008, 01:57 AM
  3. Replies: 8
    Last Post: 10-18-2006, 03:14 PM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  5. wrong result?
    By o0o in forum C++ Programming
    Replies: 5
    Last Post: 01-11-2004, 09:53 AM