Thread: weird problem with passing a value..

  1. #1
    Unregistered
    Guest

    Question weird problem with passing a value..

    from my main program to a function. I will first describe the problem then put my code after it. Thanks in Advance for your help!!

    Problem Description:
    I use sscanf to get data from each line I read using fgets (158 lines total). Each line has 5 fields separated by white space. A typical line looks like:

    1 0 0 1 XZ_632 (This would be the first line in the file)
    .
    .
    .
    158 0 3866 1 YY_314 (This would be the last line in the file)


    The problem is that the last (5th) field that I get from each of the 158 lines using sscanf is not getting passed properly from main to the function. When I look at all 158 array elements inside the function for the 5th value it's always the last value in the last line. In this case it would be element 5 from line number 158 (YY_314). It should be the last element from eah line. In other words I get "YY_314" all 158 times.


    Here is my code:

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


    #define MAXCHARS 80

    int i;
    int ip;
    int lp;
    int m;
    int n;
    int np;

    FILE *infp;

    char line1[MAXCHARS];

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


    int main()
    {
    struct CAR_STRUCT pCar;

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

    for (m=1; m<159; m++) {
    if(fgets(line1, MAXCHARS, infp) != NULL) {

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

    pCar.Index = atoi(t1);
    pCar.Matrix_Pos[m][1] = atoi(t2);
    pCar.Matrix_Pos[m][2] = atoi(t3);
    pCar.Matrix_Pos[m][3] = atoi(t4);
    pCar.Track[m] = t5;
    }
    }

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

    Load_Matrices(&pCar);

    return(0);
    }

    void Load_Matrices(CARTYPE *pCar)
    {
    for (m=1; m<159; m++){
    printf(" Track_Id = %s\n", pCar->Track[m]);
    }
    }


    <****HEADER FILE****>

    typedef struct CAR_STRUCT {
    int Index;
    int Track[158];
    int *pCar;
    int Matrix_Pos[158][11];
    } CARTYPE;

    void Load_Matrices(CARTYPE *pCar);

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    A lot of errors in you program. Try rewriting the code. But before that, try to learn Pointers in C, because pointers don't work the way in which you have coded.
    Last edited by shaik786; 06-07-2002 at 08:13 AM.

  3. #3
    Unregistered
    Guest
    Please elaborate! What is it that looks wrong?

  4. #4
    Unregistered
    Guest

    Unhappy

    Would somebody please help me ?

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    1. The variable Track in your structure is an integer array while t5 is a character array.
    Code:
    pCar.Track[m] = t5;
    This will not work.

    2. If you want to copy a string, use the strcpy function.

  6. #6
    Unregistered
    Guest

    Unhappy

    Monster,
    I wish it were only a strcpy problem. I tried that and got the exact same result. I have noticed that if I do not do the for loop in the main program it does pass the correct value. However it only does it one at a time. I need the entire 158 element array passed at one time to the function. Any ideas?
    Thanks!

  7. #7
    Unregistered
    Guest

    Unhappy

    How can I pass the array from main to the function Load_Matrices? No matter what I have tried the result id the same. All 158 elements have the same value. Can somebody see if my declarations look ok or not and let me know what's wrong?...please!

  8. #8
    Unregistered
    Guest

    Unhappy Hammer where areyou?...need help!!!!

    My array is being over written by subsequent passes through the for loop. Element one is good for the first pass only then gets over written by the second pass (element # 2's value). This is in the main program. Any idea as to what is causing this? I am bewildered by it and I'm not a novice C programmer. Take my code that I have posted, compile it and run it. I am using MS visual C++ developer studio as my dev environment if that matters. Thanks!

  9. #9
    Unregistered
    Guest
    I'm a bit of a novice but I hope I have understood what you have tried to do in this program and made helpful changes to it. It has been compiled and run with a dummy file (though not with 158 lines) and seems to work. I have commented some lines of code as they weren't needed as far as I could see. I have used comments to explain the changes. I have moved the header file into the main program for convenience you should take it back out.
    Personally, I would make all of your global variables local to confine them to the functions they are used in.


    Code:
    #include <ctype.h> 
    #include <stddef.h> 
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    //#include "train.h"
    
    #define MAXCHARS 80 /*don't think you need this anymore*/
    
    typedef struct CAR_STRUCT { 
    int Index; 
    
    /*changed from int to char and is now an array of 158 strings
    of length 80*/
    
    char Track[158][80];
    
    int *pCar;      /*pointer to what - I can't see where it is used in your program*/
    int Matrix_Pos[158][11]; /*it looks to me as if you don't use this array correctly
                               as later in the program you only access [m][1], [m][2]
                               & [m][3] if this is the case then you could change it 
                               to int Matrix_Pos[158][3]; and access elements
                               [m][0], [m][1] & [m][2] - arrays start at element 0*/ 
    } CARTYPE; 
    
    void Load_Matrices(CARTYPE *pCar);
    
    /*int i;    only the variable m seems to be used
    int ip;
    int lp;*/
    
    int m; 
    
    /*int n;
    int np;*/
    
    FILE *infp; 
    
    /*char line1[MAXCHARS]; don't need this*/
    
    /***** DO YOU WANT THESE STRINGS TO BE ABLE TO HOLD AS MANY AS 79 CHARS plus null***/
    char t1[80], t2[80], t3[80], t4[80], 
    t5[80]; 
    
    
    int main() 
    { 
      CARTYPE pCar;  /*I think this is all you need here*/
    
      /*A shorter way to do the same thing*/
      if((infp = fopen("Data.txt", "r"))== NULL)
      { 
        fprintf(stderr, "can't open %s\n", "Data.txt"); 
        exit(1); 
      } 
    
      /*start m = 0 as this is the first element of your array, 
        1 is the second element*/
      for (m=0; m<158; m++)  /*0 -> 157 = 158*/
      { 
        /*fscanf will do what the combination of fgets and sscanf does*/
        if((fscanf (infp, "%s %s %s %s %s",
                         t1, t2, t3, t4, t5)) != EOF)
        { 
          pCar.Index = atoi(t1);
          pCar.Matrix_Pos[m][1] = atoi(t2);
          pCar.Matrix_Pos[m][2] = atoi(t3);
          pCar.Matrix_Pos[m][3] = atoi(t4);
          
          /*copy one string to another, you had tried to store
            the string as an int. also, you cannot assign a value
            to a string, ie string1 = string2; you have to copy one 
            to another*/
    
          strcpy(pCar.Track[m], t5);
        }
        
      } 
    
      // Done reading input file at this point 
      fclose(infp); 
    
      Load_Matrices(&pCar);
    
      return(0); 
    } 
    
    void Load_Matrices(CARTYPE *pCar) 
    { 
      /*again, as above m should start at 0*/
      for (m=0; m<158; m++)
      { 
        printf(" Track_Id = %s\n", pCar->Track[m]); 
      }
    }

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Hammer where areyou?...need help!!!!
    Err.... OK I'm back.... hopefully someone else has sorted you out by now though!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Hammer where areyou?...need help!!!!
    You seem to be developing somewhat of a reputation, Hammer.

    -Prelude
    My best code is written with the delete key.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >You seem to be developing somewhat of a reputation, Hammer.
    ... and this'll be the first time it's a good one
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird problem with gets();...
    By Huskar in forum C Programming
    Replies: 2
    Last Post: 03-30-2009, 12:58 AM
  2. very weird problem (pointers I think)
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 10-11-2005, 06:45 AM
  3. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  4. Weird connection problem.
    By tilex in forum Networking/Device Communication
    Replies: 6
    Last Post: 02-06-2005, 10:11 AM
  5. Weird class problem!
    By aker_y3k in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2002, 06:12 AM