This is my code and what am I trying to do is to fscanf from file that is structured like this:
Tom Veri 1234567890 HJA
Peter Schmit 9874561230 ABA
Jhon Smart 0192837465 DPA
Harry Tompson 0912354876 PHA
And after compiling my output is weird why that happens: Code is below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int nOfLines(FILE* file);
struct inputAbc
{
char ime[20+1];
char prezime[20+1];
char jmbg[20+1];
char sign[3+1];
};
int main()
{
int nLines, i;
FILE* input = NULL;
FILE* output;
struct inputAbc model_arr[10];
input = fopen("input.txt", "r");
output = fopen("output.txt", "w");
nLines = nOfLines(input);
//printf("%d\n", nLines+1);
for(i = 0; i < nLines+1; i++)
{
fscanf(input ,"%s %s %s %s",
model_arr[i].ime, model_arr[i].prezime,
model_arr[i].jmbg, model_arr[i].sign);
}
for(i = 0; i < nLines+1; i++)
{
fprintf(output,
"ime: %s\nprezime: %s\njmbg: %s\nznakovlje: %s\n\n",
model_arr[i].ime, model_arr[i].prezime,
model_arr[i].jmbg, model_arr[i].sign);
}
return 0;
}
int nOfLines(FILE *file)
{
int lines = 0;
int index;
while(!feof(file))
{
index = fgetc(file);
if(index == '\n')
lines++;
}
return lines;
}