0
#include <stdio.h>

int main()
{
    FILE *fisier;
    fisier = fopen("cnp.txt", "r");

    int cnpuri = 0;
    char cnp[10][13];

    while(1){
        fscanf(fisier, "%s", cnp[cnpuri]);
        cnpuri++;

        if(feof(fisier))
            break;
    }

    int i;
    for(i = 0; i < cnpuri; i++){
        printf("%s \n", cnp[i]);
    }

    fclose(fisier);
    return 0;
}

This is the text I'm reading:

1234567890123
1763578126765
4156156546489
5749684631654
5498654168763

And this is what it shows in terminal:

12345678901231763578126765415615654648957496846316545498654168763
1763578126765415615654648957496846316545498654168763
415615654648957496846316545498654168763
57496846316545498654168763
5498654168763
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    Strings in C need to be NUL terminated. So a 13 digit string requires a 14 char array to store. Increase the second dimension of the array by at least 1. – kaylum Apr 06 '22 at 21:41

1 Answers1

1

The problem is that the second size of the array

char cnp[10][13];

is not large enough to store the terminating zero character of entered strings.

Declare it at least like

char cnp[10][14];    

Also change the while loop the following way

while( cnpuri < 10 && fscanf(fisier, "%s", cnp[cnpuri]) == 1)
{
    cnpuri++;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335