0

Inside main, i have the following procedure to get numbers from a file:

FILE *f = fopen("numbers.txt", "r");
if(f != NULL) {
    char line[BUFFER_SIZE];
    while(fgets(line, sizeof(line), f) != NULL) {
        char *start = line;
        int field;
        int n;
        while(sscanf(start, "%d", &field, &n) == 1) {
            printf("%d \n", field);
            start += n;
        }
    }
    fclose(f);
}

If I add an integer array above this, e.g. int num[100], I get an access violation.

It seems that this somehow causes problem with the file-reading, but I can't see how at the moment.

5
  • 1
    must be UB. you have a bug in your code, the extra array just makes it visible. Commented Jun 30, 2013 at 0:11
  • 3
    Show all your code and be explicit about the error that you get back, please. Commented Jun 30, 2013 at 0:12
  • @mbratch: sizeof(line) is guaranteed to be the same as BUFFER_SIZE, not 4 or 8. Where did "4 or 8" come from? Arrays don't decay to pointers under sizeof. Commented Jun 30, 2013 at 0:20
  • 1
    maybe "%d"---> "%d%n" Commented Jun 30, 2013 at 0:28
  • @AndreyT yes, indeed, sorry my mistake. Commented Jun 30, 2013 at 2:15

3 Answers 3

2
sscanf(start, "%d", &field, &n)

You have too many arguments for your function call.

Sign up to request clarification or add additional context in comments.

5 Comments

True, and that should be corrected, but I believe excess arguments are ignored.
Thanks, I somehow deleted that. It should be while(sscanf(start, "%d%n", &field, &n) == 1) {
@Helm Hammerhand: So, does it crash in this form?
Nope, that solved it appararently. So simple, and yet I've been looking at the code for minutes and didn't see it.
@HelmHammerhand You should always turn on all warnings in your compiler options and fix them all. This will save you a lot of time.
0
while(sscanf(start, "%d", &field, &n)

It's been a while since I've done C but I don't think n is filled in as the length of the chars.

Do fscanf straight from your file and check the return to see if it got anything.

Comments

0

Your 'access violation' is no doubt coming from the fact that your variable 'n' is not getting initialized. Your sscanf(start, "%d", &field, &n) has, probably, not enough format specifiers so only the variable 'field' is getting set from that sscanf. This sscanf is not touching variable n so it is still uninitialized.

When you then add this uninitialized 'n' to your pointer 'start', you end up with an invalid address, and ultimately your access violation error.

1 Comment

BTW: Anytime you are doing pointer arithmetic, you really, really should do bounds testing.. i.e. make sure adding 'n' to your pointer doesn't push it past the boundary of your target data...

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.