0
void main() {
    nodLista* LS=NULL;
    FILE* F=fopen("asaceva.txt","r");
    if(F!=NULL) {
        char buffer[100]; int id;float pret;
        fscanf(F,"d",&id);
        while(!feof(F)) {
            fscanf(F,"f",&pret);
            fscanf(F,"s",buffer);
            Produs* p= creareProdus(id,pret,buffer);
            LS=inserareSfarsit(LS,*p);
            fscanf(F,"%d",&id);
        }
        afisareLista(LS);
    }
    _getch();
}
  • afisareLista: displays the list
  • inserareSfarsit: inserts at the end

I don't understand why it doesn't get the data from the txt file. Can you explain why?

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
mariusxd
  • 39
  • 6
  • 2
    It's `int main`, not `void main`. [`while (!eof())` is wrong.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) You'd be better off using `` and `std::string` instead of C style IO. – chris Apr 08 '14 at 23:38
  • that's how i was taught to use them. why is the feof wrong? – mariusxd Apr 08 '14 at 23:41
  • 1
    @mariusxd The answer in the question he linked to answers that question. – 0x499602D2 Apr 08 '14 at 23:42
  • 2
    The format specifier for `fscanf` requires `%`. – Thomas Matthews Apr 08 '14 at 23:42
  • The link explains it pretty well, with the same logic applying to C IO. It can easily lead to bugs in reading alone, let alone that it doesn't check that the input succeeded before using it. You're also asking for a buffer overflow with `fscanf` reading into `buffer`. This overflow is avoided very easily by doing `someFileStream >> someCppString;` instead. – chris Apr 08 '14 at 23:42
  • You must check for each `fscanf`-invocation how many parameters were actually read. – Deduplicator Apr 08 '14 at 23:42
  • thank you for all the answers. i'm so blind i forgot the %. about the feof , thats how the teachers used it. thanks again for the info about it. – mariusxd Apr 08 '14 at 23:46
  • Proper way to say thanks is to accept the best correct answer. Shame there is none yet, as both have critical flaws. Anyway, you are welcome. – Deduplicator Apr 08 '14 at 23:48
  • @mariusxd: Taught by whom? – Lightness Races in Orbit Apr 08 '14 at 23:56
  • teachers have been using (!foef) since the first year of college. – mariusxd Apr 08 '14 at 23:58

1 Answers1

1

There are a couple of issues in your code:

  • main not returning integer.
  • not using fscanf consistently and correctly with placeholders.
  • you are not checking the return value of fscanf for failure.
  • NULL should be replaced with nullptr if you have C++11 support available.

The correct code should be like this:

int main() {
    nodLista* LS=NULL;
    FILE* F=fopen("asaceva.txt","r");
    if(F!=NULL) {
        char buffer[100]; int id;float pret;
        if (!fscanf(F,"%d",&id))
            cout << "Error happened: " << ferror(F) << ", error string: " << strerror(errno) << endl;
        while(!feof(F)) {
            if (!fscanf(F,"%f",&pret))
                cout << "Error happened: " << ferror(F) << ", error string: " << strerror(errno) << endl;
            if (!fscanf(F,"%s",buffer))
                cout << "Error happened: " << ferror(F) << ", error string: " << strerror(errno) << endl;
            Produs* p= creareProdus(id,pret,buffer);
            LS=inserareSfarsit(LS,*p);
            if (!fscanf(F,"%d",&id))
                cout << "Error happened: " << ferror(F) << ", error string: " << strerror(errno) << endl;
        }
        afisareLista(LS);
    }
    _getch();
    return 0;
}
lpapp
  • 47,035
  • 38
  • 95
  • 127
  • `main()` doesn't have to have a return statement. – 0x499602D2 Apr 08 '14 at 23:44
  • @0x499602D2: you misread the answer. I have not said it has to return zero. I said it has to return an integer! – lpapp Apr 08 '14 at 23:46
  • @0x499602D2: `main()` is a special case. – Deduplicator Apr 08 '14 at 23:46
  • @LaszloPapp: Still no proper checking for input failure. – Deduplicator Apr 08 '14 at 23:47
  • @Deduplicator: do not worry, that is not the main point here. – lpapp Apr 08 '14 at 23:48
  • @LaszloPapp: Shouldn't an answer address all flaws found, even if the OP was not aware of them? Well, you addressed the rest in the edit, so nevermind. – Deduplicator Apr 08 '14 at 23:51
  • 1
    @Deduplicator: depends... some people prefer to only point out the real issue, and some people prefer to point out every mistake even irrelevant one to the issue. It is all about personal taste, and not worth arguing about, really. That being said, I pleased you this time to avoid further arguing as I do not have strong about it. :) – lpapp Apr 08 '14 at 23:52
  • If i recall the relevant discussions on meta properly, one telling argument was that others often use code in answers, depending on the accepted answer having addressed all points of even passing interest. – Deduplicator Apr 08 '14 at 23:55
  • @Deduplicator: let us not have a lengthy discussion about it here. If you wish to bring this to Meta, you are free to do so. Mind you, you will probably enter the world of flame with different strong personal opinions. – lpapp Apr 08 '14 at 23:56