-1

For example: #1 Tutti/Leeloo/853811356; N

And this is my code:

typedef struct{
    int redni;
    char prezime[50+1];
    char ime[50+1];
    char osobna[50+1];
    char glasao[10];

} Biraliste;


int nBiraci=0;

while(fscanf(biralisteTxt,  "%d %[^/]/%[^/]/%[^;];%[^\n]",
             biraci[n].redni, biraci[n].prezime, biraci[n].ime, biraci[n].osobna, biraci[n].glasao  ) == 5)
{
    nBiraci++;

}

for(i=0;i<nBiraci;i++)
{

        fprintf(statistikaTxt, "%d %s %s %s %s", 
&biraci[i].redni, biraci[i].prezime, biraci[i].ime, biraci[i].osobna, biraci[i].glasao );


}

Can someone help mi with right fscanf and fprintf, and is it ok to fscanf redni with %d or it should be %s.

lOVOR
  • 29
  • 4
  • 2
    Well, for starters, `biraci[n].redni` as the first argument is wrong. It should be `&biraci[n].redni` . That, assuming `n` is really somewhere in the *real* code, which should have been posted in the first place. I'm pressed to see how you're skipping that leading `#` regardless, but that's another matter, I suspect. – WhozCraig Jan 10 '20 at 14:28
  • `redni` is `int` type so it must be `%d`. `%s` is for `char *` – Lucas Gras Jan 10 '20 at 14:29
  • 1
    Well, for starters, [don't do this with `scanf`](https://stackoverflow.com/questions/58403537/what-can-i-use-for-input-conversion-instead-of-scanf). The \*scanf family of functions are broken-as-specified and should never be used for _anything_; in this case, the most important reason why they're not fit for purpose is that they don't respect the size limits on the fields of `Biraliste`. – zwol Jan 10 '20 at 14:33
  • I fixed &biraci[n].redni . But how should I scanf it – lOVOR Jan 10 '20 at 14:38
  • nBiraci is my counter @WhozCraig – lOVOR Jan 10 '20 at 14:39
  • Or i should count number of lines first ? – lOVOR Jan 10 '20 at 14:39
  • I suggest to read an input line with `fgets`, use `strtok` or `strchr` to separate the fields. Then you can handle your string fields with `str*`functions and numbers with `strtol` or similar. – Bodo Jan 10 '20 at 14:44
  • No, I need only a number – lOVOR Jan 10 '20 at 14:52
  • int p=0; while(fscanf(biralisteTxt, "%c %d %[^/]/%[^/]/%[^;];%[^\n]", biraci[p].hash,&biraci[p].redni, biraci[p].prezime, biraci[p].ime, biraci[p].osobna, biraci[p].glasao ) == 5) { p++; } – lOVOR Jan 10 '20 at 14:58
  • There is an & too much in front of `&biraci[i].redni` in the second loop. – chmike Jan 10 '20 at 15:10

2 Answers2

2

" #%d %[^/]/%[^/]/%[^;];%[^\n]" - this is the right answer, thank you

lOVOR
  • 29
  • 4
0

The following code fixes two problems.

  • scanf must get the address of the variable to fill, this &biraci...
  • the index must be nBiraci and not n, thus &biraci[nBiraci]...
  • there must be a # in front of %d
int nBiraci=0;
while(fscanf(biralisteTxt,  " #%d %[^/]/%[^/]/%[^;];%[^\n]",
             &biraci[nBiraci].redni, (char*)&biraci[nBiraci].prezime,
             (char*)&biraci[nBiraci].ime, (char*)&biraci[nBiraci].osobna,
             (char*)&biraci[nBiraci].glasao) == 5)
{
    nBiraci++;
}
chmike
  • 20,922
  • 21
  • 83
  • 106