Thread: Trouble with string output...Help!

  1. #1
    Unregistered
    Guest

    Unhappy Trouble with string output...Help!

    I need help with trying to determine why my array is being overwritten. The problem is that when I run this code and then print out the results, array Var[m] gets filled from beginning to end with the last value that sscanf got from the data file "Data.txt". If you look at the code the sscanf puts the last field of data into a variable called t5. Therefore I only see "S2_536" from the data file in all 15 array elements. When I ran the code through the debugger I saw the first element get the correct value ("S2_632" from the data file), then the second time through the loop the first and second elements both had the second elements value (or "S2_627" from the data file)... and so on until all 15 elements contained the last or 15th elements value. Each element for Var[m] should have it's respective data from line one through line 15. All other arrays that I print out look good except this one. Why is it doing this? What am I doing wrong? Please run this code below to see exactly what I'm saying. Thanks for your help!



    #include <ctype.h>
    #include <stddef.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define MAXCHARS 80

    int m;

    FILE *infp;

    char line1[MAXCHARS];

    char t1[20], t2[20], t3[20], t4[20], t5[20];

    int int Index[25], Pos[25][11], int Var[25];


    int main()
    {

    fp = fopen("Data.txt", "r");

    if(fp == NULL) {
    fprintf(stderr, "can't open %s\n", "Data.txt");
    exit(EXIT_FAILURE);
    }

    for (m=0; m<15; m++) {

    if(fgets(line1, MAXCHARS, infp) != NULL) {

    sscanf (line1, "%s %s %s %s %s", t1, t2, t3, t4, t5);

    Index[m] = atoi(t1);
    Pos[m][1] = atoi(t2);
    Pos[m][2] = atoi(t3);
    Pos[m][3] = atoi(t4);
    Var[m] = t5;

    }
    }

    for (m=0; m<15; m++) {
    printf("Index = %i\n",Index);
    printf("Index = %i\n",Pos[m][1]);
    printf("Index = %i\n",Pos[m][2]);
    printf("Index = %i\n",Pos[m][3]);
    printf("Index = %s\n",Var[m]);
    }

    // Done reading input file at this point
    fclose(infp);

    return(0);
    }

    Data.txt is a text file, 15 lines long, each field separated by a space and looks exactly like this:

    1 0 0 1 S2_632
    2 21 21 2 S2_627
    3 30 51 2 S2_621
    4 18 69 0 S2_613
    5 18 87 0 S2_606
    6 19 106 0 S2_599
    7 57 163 1 S2_595
    8 14 177 0 S2_589
    9 18 195 0 S2_587
    10 15 210 0 S2_579
    11 14 224 0 S2_572
    12 21 245 0 S2_566
    13 21 266 0 S2_554
    14 14 280 0 S2_542
    15 13 293 0 S2_536

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    I didn't test it. But I noticed that MAXCHARS is 80 and the sum of lenghts of t1, t2, t3, t4 and t5 is 100.

    Further I'm wondering why m runs from 0 to 15 and the arrays have length 25.

    And finally note that it fgets() returns NULL, then there are less then 16 lines read.

  3. #3
    Unregistered
    Guest
    Shiro,
    Should MAXCHARS be the same length as the sum of t1, t2, t3, t4, and t5? Is it a problem that the arrays are 25 and I am only running 15 passes? Please keep replying. Thanks!

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    I'm not quite sure how sscanf works, but it seems to me that when you have read a buffer of 80 chars and when you store it in a piece of memory of 100 chars, then there are 20 chars which are not filled. So they have random values.

    Same with the arrays. The have length 25 but are filled until 16 (0-15). So at least 9 are not filled and therefor random. I say at least, since if fgets() is NULL at least 1 time, then the real number of filled arrays is less than 16. But you print 16 lines, so that would explain why you get strange strings on the screen.

  5. #5
    Unregistered
    Guest
    Shiro,
    I changed MAXCHARS to 100 and changed the array sizes to match the loop...still the same results. I read the sscanf documentation and I think I'm using it correctly. Can you run the code to see what's going on? Thanks! Barb

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    There are a couple compiler errors in this code, so there isn't really any way to check it right now I'm afraid. Could you please make sure you copy and paste exactly the same code you have that compiles but gives wrong answer?
    Callou collei we'll code the way
    Of prime numbers and pings!

  7. #7
    Unregistered
    Guest
    Oops! Here is the correct version I'm running. Thanks for pointing that out!! Barb

    #include <ctype.h>
    #include <stddef.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define MAXCHARS 80

    int m;

    FILE *fp;

    char line1[MAXCHARS];

    char t1[20], t2[20], t3[20], t4[20], t5[20];

    int Index[25], Pos[25][11];

    char *Var[25];


    int main()
    {

    fp = fopen("Data.txt", "r");

    if(fp == NULL) {
    fprintf(stderr, "can't open %s\n", "Data.txt");
    exit(EXIT_FAILURE);
    }

    for (m=0; m<15; m++) {

    if(fgets(line1, MAXCHARS, fp) != NULL) {

    sscanf (line1, "%s %s %s %s %s", t1, t2, t3, t4, t5);

    Index[m] = atoi(t1);
    Pos[m][1] = atoi(t2);
    Pos[m][2] = atoi(t3);
    Pos[m][3] = atoi(t4);
    Var[m] = t5;
    }
    }

    for (m=0; m<15; m++) {
    printf("Data Element 1 = %i\n",Index[m]);
    printf("Data Element 2 = %i\n",Pos[m][1]);
    printf("Data Element 3 = %i\n",Pos[m][2]);
    printf("Data Element 4 = %i\n",Pos[m][3]);
    printf("Data Element 5 = %s\n\n",Var[m]);
    }

    // Done reading input file at this point
    fclose(fp);

    return(0);
    }

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    There were some compiler errors. But after putting some printf's into the code, I found that this:

    Var[m] = (int) t5;

    is going wrong. Var is an array of ints, but you want to print it as a string. Why not keeping it a string? You should copy the string t5 in an array of strings.

    BTW, there is no need to make the variables global in this program, just put them inside the main.

    Code:
    #include <ctype.h>
    #include <stddef.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXCHARS 100
    
    int main()
    {
        int m;
        FILE *infp;
        char line1[MAXCHARS];
        char t1[20], t2[20], t3[20], t4[20], t5[20];
        int Index[25], Pos[25][11], Var[25];
    
        infp = fopen("Data.txt", "r");
    
        if(infp == NULL) 
        {
            fprintf(stderr, "can't open %s\n", "Data.txt");
            exit(EXIT_FAILURE);
        }
    
        for (m=0; m<15; m++)
        {
    
            if(fgets(line1, MAXCHARS, infp) != NULL)
            {
                sscanf (line1, "%s %s %s %s %s", t1, t2, t3, t4, t5);
    
                Index[m] = atoi(t1);
                Pos[m][1] = atoi(t2);
                Pos[m][2] = atoi(t3);
                Pos[m][3] = atoi(t4);
                Var[m] = (int) t5;
                printf ("m %d t5 %s\n", m, t5);
            }
        }
    /*
        for (m=0; m<15; m++)
        {
            printf("%i Index = %i\n",m,Index[m]);
            printf("%i Index = %i\n",m,Pos[m][1]);
            printf("%i Index = %i\n",m,Pos[m][2]);
            printf("%i Index = %i\n",m,Pos[m][3]);
            printf("%i Index = %s\n",m,Var[m]);
        }
    */
        // Done reading input file at this point
        fclose(infp);
    
        return(0);
    }
    [edit]
    If you run that code, then you'll see on the screen that t5 has correct values. Instead of assigning t5 to an int, you should use something like strcpy to copy the string into an array of strings.
    [/edit]

  9. #9
    Unregistered
    Guest
    Shiro,
    I fixed the code errors (see my last post) due to cut and paste of wrong version of code...I know that doesn't help. I know I'm doing something wrong. It didn't make any difference on the MAXCHARS being 80 of 100 so I put it back to 80. This code is so simple and I am just amazed that this is happening. Please keep giving me advice. Thanks! Barb

  10. #10
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    I've declared Var as follows:

    char Var [15][25];

    And used it in the for-loop as follows:

    strcpy (Var[m], t5);

    Now the string in t5 is correctly copied into the array of strings which Var is.

  11. #11
    Unregistered
    Guest
    Shiro,
    I forgot to add that I ran the version of the code you have posted (with the printf statements uncommented). I still got the same result. I always have had good values when I have looked at the results inside the loop where the sscanf is being done. It's when you look at them outside the loop htey're bad. I also ran a version with Var[25] as a character array and did a strcpy of t5 to the array...same result. This is bizzare! Keep helping please!! Barb

  12. #12
    Unregistered
    Guest

    Smile

    Shiro,
    You fixed it and I understand what I was doing wrong. Thanks!!!
    Barb

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 11:20 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM