0

Hello i have a little problem with my project. I want to scan the text from .TXT file into my struct except from the 1st line in my text file. I tried to do this with fgets() function but it only works the first time.

here is a little effort from my work.

 for (i=1;i<=number;i++){
    fgets(s,100,fr);
    fgets(p_akt->signatura,12,fr);
    fgets(p_akt->isbn,15,fr);
    fgets(p_akt->kniha,100,fr);
    fgets(p_akt->autori,100,fr);
    fscanf(fr,"%d",&p_akt->datum);
    fscanf(fr,"%d",&p_akt->preukaz);

    printf("%d.\n",i);
    printf("signatura: %s",p_akt->signatura);
    printf("isbn: %s",p_akt->isbn);
    printf("kniha: %s",p_akt->kniha);
    printf("autori: %s",p_akt->autori);
    printf("datum: %d\n",p_akt->datum);
    printf("preukaz: %d\n",p_akt->preukaz);

    p_akt->p_dalsi = NULL;
}

As I said it runs exactly like I want only for the first loop ... When it enters the second cycle it moves everysingle information by 1 so in isbn i have signatura, in kniha i have isbn etc. I hope i made everything clear. Thanks

EDIT://

---
DE612301
9783161484100
Gesammelte Werke 3. Logik der Forschung
Karl R. Popper
20120508
56432
---
EN3123123
9780061092190
Men at Arms
Terry Pratchett
20101010
45612

This is my text file and i want to fill my struct with those information except the ---

Grzegorz Piwowarek
  • 13,172
  • 8
  • 62
  • 93
Toesmash
  • 307
  • 4
  • 20

1 Answers1

1

Because you read the fields date and preukaz using fscanf:

fscanf(fr,"%d",&p_akt->datum);
fscanf(fr,"%d",&p_akt->preukaz);

the newline is not read after preukaz is filled. After changing the format from %d to %d\n:

fscanf(fr,"%d\n",&p_akt->datum);
fscanf(fr,"%d\n",&p_akt->preukaz);

the newline char will be read as well and in the next loop, fgets(s,100,fr); will read the delimiter --- correctly.

harpun
  • 4,022
  • 1
  • 36
  • 40
  • 1
    That did not actually work that good... It ran good for the first loop I will upload the text file maybe it will help you guys to understand – Toesmash May 05 '13 at 12:56
  • Please add an excerpt of your input file (first 2-3 complete records) in the original question. – harpun May 05 '13 at 12:58
  • @Toesmash: take a look at my updated answer. You're skipping the first line correctly and having trouble with reading the integers. – harpun May 05 '13 at 13:12