Mac Stops printing correctly

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]
 

chown33

Moderator
Staff member
Aug 9, 2009
8,839
5,263
vertical
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().
 

dougphd

macrumors member
Original poster
Apr 28, 2016
70
0
Thanks. That was stupid on my part. I usually count that right. I'm getting old.
 

chown33

Moderator
Staff member
Aug 9, 2009
8,839
5,263
vertical
Thanks. That was stupid on my part. I usually count that right. I'm getting old.
Hence the signpost for strlcpy() and strlcat(). Think of them as senior versions of the recklessly foolhardy strcpy() and strcat().
 

cqexbesd

macrumors regular
Jun 4, 2009
159
26
Germany
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.
 

dougphd

macrumors member
Original poster
Apr 28, 2016
70
0
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.
 

ghellquist

macrumors regular
Aug 21, 2011
146
5
Stockholm Sweden
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
 

dougphd

macrumors member
Original poster
Apr 28, 2016
70
0
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]
 

cqexbesd

macrumors regular
Jun 4, 2009
159
26
Germany
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.