Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dougphd

macrumors member
Original poster
Apr 28, 2016
70
0
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]
 
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().
 
Thanks. That was stupid on my part. I usually count that right. I'm getting old.
 
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.
 
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.
[doublepost=1463349187][/doublepost]I saw the underscore in some online example. I'll take it out.
 
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
 
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]
 
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));

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.
 
  • Like
Reactions: chown33
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.