Question: In Visual studio 2019, I have a nuget package installed in a web api project which I need to debug whose source code is in a seprate solution. I have followed these steps

1. I uninstalled this nuget package
2. Right clicked on solution and added existing project.
3. Right clicked on my web project and added reference and ticked this project in the reference manager window.

I was initially getting the following error and my break points were not getting hit. I clicked on `Disable Just My Code and Continue` but that did not help and I'm still unable to hit the break points. Any ideas what I am doing wrong? I have tried few solutions given in similar questions but nothing has helped.

[![enter image description here](https://i.stack.imgur.com/ErLz9.png)](https://i.stack.imgur.com/ErLz9.png)

Answer: 
**Off by 1**

Code attempts to read into `len+1` elements of `tmp[]`.

```
data tmp[len];
for (i=0; i<=len; i++) {  // too many
    fscanf(database, "%s %s %d %lf \n", &tmp[i].name, &tmp[i].surname, &tmp[i].age, &tmp[i].wage);
}

```

> 
> I saw that no values are assigned to the strings (OP)
> 
> 
> 

Better code would use width limits and test `fscanf()` result before using the data scanned.

```
//         v---- < not <=
for (i=0; i<len; i++) { 
    if (fscanf(database, "%63s %63s %d %lf",  
        &tmp[i].name, &tmp[i].surname, &tmp[i].age, &tmp[i].wage) != 4) {
        //                                                        ^^^^ test!
      break;
    }

```

Even better code would read a *line* with `fgets()` into a *stirng* and then attempt to parse the string.

**Off by 2**

Code's attempt to find number of lines can be short by 1 if the last *line* does not end with a `'\n'`.

Alternative

```
size_t fullcheck(void) {
    FILE* database = fopen("database.txt", "r");
    if (database == NULL) {
        return 0;
    }
    int previous = '\n';
    int c;
    size_t r=0;
    while((c=getc(database))!=EOF) {
      if (previous == '\n') r++;
      previous = c;
    }
    fclose(database);
    return r;
}

```

**Missing `fclose()`**

[fullcheck() doesn't close the file after opening it.](https://stackoverflow.com/questions/61486657/c-problems-with-fscanf/61487036#comment108767363_61486657)

**`int`**

Use an `int` to distinguish the typically 257 different returns values of `fgetc()`. Note when char` is *unsinged*, OP's code is an infinite loop.

**More woes**

The loop to print the sorted list is off-by-one, too short. And the sorting itself only looks at the first letter of each name, should use `strcmp`. 
[@user3386109](https://stackoverflow.com/questions/61486657/c-problems-with-fscanf?noredirect=1#comment108768590_61486657)

**Maybe more?**
Make sure to close the file here, before every return:

```
int fullcheck() {
    char c;
    int r=0;
    FILE* database;
    if ((database=fopen("database.txt", "r"))==NULL) {
        fclose(database);
        return 0;
    }
    else {
        while((c=getc(database))!=EOF) {
            if(c=='\n')
                r++;
        }
        fclose(database);
        return r;
    }
}

```

And also a little bit fixing in the loop\_counters here: 

```
void sort() {
    char tmpstr[MAX];
    int len=fullcheck(), i, a, b;
    data tmp[len];
    FILE* database;
    FILE* sorted;
    database=fopen("database.txt", "r");
    sorted=fopen("sorted.txt", "w+");
    for (i=0; i<len; i++) {
        fscanf(database, "%s %s %d %lf \n", &tmp[i].name, &tmp[i].surname, &tmp[i].age, &tmp[i].wage);
        printf("%s", tmp[b+1].surname);
                system("pause");
    }
    for (a=0 ; a<(len); a++) {
        for (b=0; b<(len); b++) {
            if ((tolower(tmp[b].surname[0]))>(tolower(tmp[b+1].surname[0]))) {
                strcpy(tmpstr, tmp[b].surname);
                strcpy(tmp[b].surname, tmp[b+1].surname);
                strcpy(tmp[b+1].surname, tmpstr);
                printf("%s", tmp[b+1].surname);
                system("pause");
            }
        }
    }
    for (a=0; a<(len); a++) {
        fprintf(sorted, "%s %s %d %.0lf \n", tmp[a].name, tmp[a].surname, tmp[a].age, tmp[a].wage);
    }
    fclose(sorted); 
    fclose(database); 
} 

```