Stops printing correctly

Discussion in 'Mac Programming' started by dougphd, May 15, 2016.

  1. dougphd macrumors member

    Joined:
    Apr 28, 2016
    #1
    The output in bold shows a line from the file then what has supposedly been put into the structure. At line 193 something goes wrong.

    Code:
    /
    typedefstruct sauce
    
    {
        char date[7];
        float sauce;
        int index;
    } _sauce;
    
    _sauce* open_and_read(char * filename)
    {
    
         FILE *fp;
         _sauce  *input;
         int i;
         char line[200];
         input = (_sauce*)calloc(days_of_data, sizeof(*input));
         if ((fp = fopen(filename, "r")) == NULL)
            {
                printf("Couldn't open %s\n", filename);
                exit(0);
            };
        for(i = 0; i < days_of_data; i++)
           {
            fgets(line, 200,fp);
            printf("%s",line);
            strcpy(input[I].date,strtok(line,","));
            input[I].sauce = atof(strtok(NULL,","));
            input[I].index = i;
            printf("%d %s %f\n", input[I].index, input[I].date, input[I].sauce);
            }
        return input;
    };
    
    
    [B]10/5/99,113.319[/B]
    
    [B]189 10/5/99 113.319000[/B]
    
    [B]10/6/99,112.7414[/B]
    
    [B]190 10/6/99 112.741402[/B]
    
    [B]10/7/99,112.5105[/B]
    
    [B]191 10/7/99 112.510498[/B]
    
    [B]10/8/99,110.7106[/B]
    
    [B]192 10/8/99 110.710602[/B]
    
    [B]10/11/99,111.0172[/B]
    
    [B]193 10/11/99\316\336B\301 111.017197[/B]
    
    [B]10/12/99,112.4442[/B]
    
    [B]194 10/12/99n\343\340B\302 112.444199[/B]
    /[code]
    [/I][/I][/I][/I][/I][/I]
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Given:
    Code:
        char date[7];
    
    Question 1: What is the expected value of len?
    Code:
    int len = strlen( "10/11/99" );
    Question 2: Counting the nul terminator, how much memory is occupied by the string?

    Question 3: Will it fit in 'date'?

    Look at strlcpy() and strlcat().
     
  3. dougphd thread starter macrumors member

    Joined:
    Apr 28, 2016
    #3
    Thanks. That was stupid on my part. I usually count that right. I'm getting old.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Hence the signpost for strlcpy() and strlcat(). Think of them as senior versions of the recklessly foolhardy strcpy() and strcat().
     
  5. cqexbesd macrumors regular

    Joined:
    Jun 4, 2009
    #5
    It's great you have the solution. Can I suggest that for next time though you include a compilable sample along with some indication of what input is expected?

    Also, be careful starting your type names with underscore - there are rules about this. Basically almost everything starting with _ is reserve red and things that aren't (like _sauce you use here) are only meant to be used for things at file scope. My advice is never lead with an _ in an application and avoid the hassle.
     
  6. dougphd thread starter macrumors member

    Joined:
    Apr 28, 2016
    #6
    Thanks. Since I'm a senior, I'm hoping that an old dog doesn't have to learn new tricks but I guess that's not the case.
    --- Post Merged, May 15, 2016 ---
    I saw the underscore in some online example. I'll take it out.
     
  7. ghellquist macrumors regular

    Joined:
    Aug 21, 2011
    Location:
    Stockholm Sweden
    #7
    It's been a while since I programmed, but I do read this line:

    input = (_sauce*)calloc(days_of_data, sizeof(*input));

    As allocating an array of pointers. Given how you use input later, it should really be

    input = (_sauce*)calloc(days_of_data, sizeof(input));

    /Gunnar
     
  8. dougphd thread starter macrumors member

    Joined:
    Apr 28, 2016
    #8
    Lead to an error
    Code:
    /
        for(i = 0; i < days_of_data; i++)
    
        {
    
            fgets(line, 50,fp);
    
          //  printf("%s",line);
    
            strcpy(input[i].date,strtok(line,","));
    
            input[i].sauce = atof(strtok(NULL,",")); EXE_BAD_ACCESS
    
            input[i].index = i;
    
          //  printf("%d %s %f\n", input[i].index, input[i].date, input[i].sauce);
    
            }
    /[code]
     
  9. cqexbesd macrumors regular

    Joined:
    Jun 4, 2009
    #9
    No the point here is to allocate an array of sauce structs. If you only allocate pointers to source structs then somewhere else you need to allocate enough memory for the structs themselves. So the original code is correct here.
     

Share This Page