0
while (fgets(buff, sizeof (char*)*100, file)) {
    if (line == 0) {
        fgets(buff, sizeof (char*)*100, file);
        line++;
    }

    int i = 0;
    char *p = strtok(buff, ",");
    char *array[10];

    while (p != NULL) {
        array[i++] = p;
        p = strtok(NULL, ",");
    }
    if (!strcmp(array[1], "03/04/2020")) {
        *inf += atof(array[i - 2]);
        *dead += atof(array[i - 1]);
        *rec += atof(array[i]);
    }

}

I'm trying to split the line which has the following format:

3245,03/04/2020,Jiangxi,Mainland China,2020-03-04T01:33:07,935.0,1.0,884.0

i tried using strtok with "," delimiter but I kept getting this `

RUN FAILED (exit value -1,073,741,819, total time: 1s

If my code is not clear PLEASE DO NOT HESITATE TO ASK ME ABOUT IT

16
  • 3
    Post a minimal reproducible example. As is, others can not successfully compile the posted code. Commented Mar 15, 2020 at 21:33
  • -1,073,741,819 is what you get when you print the DWORD 3,221,225,477 as a signed integer. 3,221,225,477 aka 0xC0000005 is what Windows uses to signal a Protection Fault ("SIGSEGV" in unix parlance). It means you are dereferencing a pointer variable that contains NULL or some other bad value. Commented Mar 15, 2020 at 21:41
  • Make sure i >= 2 after the loop, otherwise array[i-2] will access outside the array. Commented Mar 15, 2020 at 21:45
  • Errors will happen if there's a blank line in the file, since there won't be any tokens. Commented Mar 15, 2020 at 21:47
  • 1
    Instead of if (line == 0) test every time through the loop, skip the heading line by calling fgets() once before the loop. Commented Mar 15, 2020 at 21:48

1 Answer 1

1

There's probably a blank line in the file. It won't have any tokens, so i will be 0 after the loop. array[i] will be uninitialized, and array[i-1] and array[i-2] will be outside the array, so accessing them causes undefined behavior.

Check that you've gotten enough tokens, and skip the line otherwise.

Also, since you increment i after assigning the token, array[i] is after the elements that you assigned. You should be using i-3, i-2, and i-1 to get the last 3 tokens.

// skip header line
fgets(buff, sizeof (char*)*100, file);

while (fgets(buff, sizeof (char*)*100, file)) {
    int i = 0;
    char *p = strtok(buff, ",");
    char *array[10];

    while (p != NULL) {
        array[i++] = p;
        p = strtok(NULL, ",");
    }
    if (i <= 3) { // skip lines that don't have enough tokens
        continue;
    }
    if (!strcmp(array[1], "03/04/2020")) {
        *inf += atof(array[i - 3]);
        *dead += atof(array[i - 2]);
        *rec += atof(array[i-1]);
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

the problem is i cannot skip any line because i need to extract the last three numbers from each line. Also, im sure that the file does not contain blank lines :(
There may be a blank line at the end.
and by the way, some lines may miss some elements such as : 3245,03/04/2020,,Mainland China,2020-03-04T01:33:07,935.0,1.0,884.0
true, i just checked and found that theres a blank line at the end. what would you suggest doing to fix this ?
That shouldn't be a problem since you don't use the empty fields. strtok() will store those as empty tokens.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.