feof verhindert zählen



  • Hallo,

    ich habe mal wieder probleme mit der feof funktion, ich muss ein projekt schreiben, welches bmp datein bearbeiten kann, ohne jetzt auf die details einzugehen (was für das problem es egal ist).
    ALso hier der Code:

    int main(int argc, char *argv[])
    {
        unsigned int charakterwandler=0,j=0;
        FILE *pFile=NULL;
        unsigned char *pMemory=NULL;
        unsigned char zeichen;
        unsigned int charaktercounter=0;
    
        pLoad=fopen("input.bmp",rt");//Das Bild ist 2x2 Pixel groß
        for(charaktercounter=0;!feof(pLoad);charaktercounter++)
        {
            fscanf(pLoad,"%c",&zeichen);
            printf("%x",zeichen);
        }
    

    Im Prinzip brauche nur die Anzahl der Charakters, für das weitere Programm.
    Der Code funktioniert für kleinere Bilder, aber für größere hört die Schleife frühzeitg auf???
    wenn ihr mehr infos brauch kann euch sie natürlich geben^^
    also meine vermutung ist, dass das irgendwas mit dem feof() schief geht.
    ich hoffe ihr könnt mir weiterhelfen



  • Probiere es mal mit:

    fopen("input.bmp","rb");
    


  • Danke VIELMALS Wutz, du hast mich sehr weit vorangebracht, was heist das "rb" überhaupt?? read binary??



  • Ja. "rt" bedeutet analog read text.



  • Nein, tut es nicht. "rt" ist undefiniert.

    ISO/IEC 9899:1999 7.19.5.3 (3) schrieb:

    The argument mode points to a string. If the string is one of the following, the file is open in the indicated mode. Otherwise, the behavior is undefined.237)

    [i][b]r[/b][/i]                  open text file for reading
    [i][b]w[/b][/i]                  truncate to zero length or create text file for writing
    [i][b]a[/b][/i]                  append; open or create text file for writing at end-of-file
    [i][b]rb[/b][/i]                 open binary file for reading
    [i][b]wb[/b][/i]                 truncate to zero length or create binary file for writing
    [i][b]ab[/b][/i]                 append; open or create binary file for writing at end-of-file
    [i][b]r+[/b][/i]                 open text file for update (reading and writing)
    [i][b]w+[/b][/i]                 truncate to zero length or create text file for update
    [i][b]a+[/b][/i]                 append; open or create text file for update, writing at end-of-file
    [i][b]r+b[/b] or [b]rb+[/b][/i]         open binary file for update (reading and writing)
    [i][b]w+b[/b] or [b]wb+[/b][/i]         truncate to zero length or create binary file for update
    [i][b]a+b[/b] or [b]ab+[/b][/i]         append; open or create binary file for update, writing at end-of-file
    

    237) If the string begins with one of the above sequences, the implementation might choose to ignore the remaining characters, or it might use them to select different kinds of a file (some of which might not conform to the properties in 7.19.2).



  • seldon schrieb:

    Nein, tut es nicht. "rt" ist undefiniert.

    ISO/IEC 9899:1999 7.19.5.3 (3) schrieb:

    The argument mode points to a string. If the string is one of the following, the file is open in the indicated mode. Otherwise, the behavior is undefined.237)

    [i][b]r[/b][/i]                  open text file for reading
    [i][b]w[/b][/i]                  truncate to zero length or create text file for writing
    [i][b]a[/b][/i]                  append; open or create text file for writing at end-of-file
    [i][b]rb[/b][/i]                 open binary file for reading
    [i][b]wb[/b][/i]                 truncate to zero length or create binary file for writing
    [i][b]ab[/b][/i]                 append; open or create binary file for writing at end-of-file
    [i][b]r+[/b][/i]                 open text file for update (reading and writing)
    [i][b]w+[/b][/i]                 truncate to zero length or create text file for update
    [i][b]a+[/b][/i]                 append; open or create text file for update, writing at end-of-file
    [i][b]r+b[/b] or [b]rb+[/b][/i]         open binary file for update (reading and writing)
    [i][b]w+b[/b] or [b]wb+[/b][/i]         truncate to zero length or create binary file for update
    [i][b]a+b[/b] or [b]ab+[/b][/i]         append; open or create binary file for update, writing at end-of-file
    

    237) If the string begins with one of the above sequences, the implementation might choose to ignore the remaining characters, or it might use them to select different kinds of a file (some of which might not conform to the properties in 7.19.2).

    Ich verstehe es aber auch so, dass rt zwar nicht in der Liste definiert ist, aber zumindest trotzdem gültig ist, da zumindest r definiert ist. Was nun aus dem t wird ist dann allerdings fraglich und gleitet dann doch ins undefinierte.



  • Das ist eigentlich ziemlich eindeutig. "Wenn der String in der folgenden Liste ist, wird die Datei im angezeigten Modus geöffnet. Sonst ist das Verhalten undefiniert." Die Fußnote macht lediglich einen Vorschlag, was die Implementation tun könnte, und auch darin ist "rt" nicht näher definiert (nicht mal genannt).

    Streng nach C-Standard kann eine Implementation damit alles mögliche machen. Es ist zwar nicht zu erwarten, dass sie die Festplatte formatiert, aber ich würde mich zumindest darauf einstellen, dass mir so was irgendwann mal NULL zurückgeben oder andere Datenströme als eine Datei aufmachen kann.

    In jedem Fall steht "rt" nicht für "read text", und auch, wenn es sich auf bestimmten Plattformen derzeit so verhalten mag, ist es wichtig, seinen Code nicht so zu schreiben, dass er nur zufällig funktioniert. Am Ende dieses Weges liegt Code, wie Jürgen Wolf ihn in seine Bücher schreibt.



  • Also mein Programmier Proffessor hat einen eigene C-Helper geschrieben und schreibt folgendes:

    DESCRIPTION(of fopen)

    Opens the file which name is stored in the filename string and returns a pointer to the file (stream). Operations allowed to the file returned are defined by the mode parameter:

    "r" Open a file for reading. The file must exist.
    "w" Create an empty file for writing. If a file with the same name already exists its content is erased.
    "a" Append to a file. Writing operations append data at the end of the file. The file is created if it doesn't exist.
    "r+" Open a file for reading and writing. The file must exist.
    "w+" Create an empty file for reading and writing. If a file with the same name already exists its content is erased before it is opened.
    "a+" Open a file for reading and appending. All writing operations are done at the end of the file protecting the previous content to be overwritten. You can reposition (fseek, rewind) the pointer to anywhere in the file for reading, but writing operations will move back to the end of file. The file is created if it doesn't exist.

    The mode parameter serves also to specify whether we want to open the file as text or binary, adding t or b characters to this access mode string.

    t Text mode. In text mode the end of file is assumed to be at first Ctrl-Z character. Some conversions can occur reading and writing with End Of Line / Feedback characters depending on your compiler and your Operating System.
    b Binary mode. End of file is reached at last byte of the file. No conversions.

    This additional t or b characters can be appended to the read/write mode at the end of the string (like "rb", "wt", "r+t", "w+b" ...) or can be inserted between the letter and the "+" character (like "rt+", "wb+").
    If t nor b are given, it is used the default method (commonly t). In most compilers this default method is specified by the global variable _fmode defined in "stdio.h".



  • transcend schrieb:

    Also mein Programmier Proffessor hat einen eigene C-Helper geschrieben und schreibt folgendes:
    ...
    t Text mode.
    ...

    Und? Seit wann macht der den Standard?



  • er hat diesen C-Helper aus anderen Quellen zusammengefasst!!



  • transcend schrieb:

    er hat diesen C-Helper aus anderen Quellen zusammengefasst!!

    Man sieht, was dabei rauskommt. Wozu soll das eigentlich gut sein, wenn man auch einfach im Standard nachschlagen kann?



  • This ANSI-C helper collection is compiled from the sources of various different sites found on the internet, in particular:

    Cplusplus Ressources: http://www.cplusplus.com/
    C Programming Reference by Martin Leslie: http://www.phim.unibe.ch/comp_doc/c_manual/C/cref.html
    (german translation: http://home.fhtw-berlin.de/~junghans/cref/index.html)
    The C Library Reference Guide: http://www.acm.uiuc.edu/webmonkeys/book/c_guide/index.html



  • Wenn ein Compiler "rt" unterstützt und das auch dokumentiert, und man sich dessen bewusst ist, kann man das doch verwenden, was spricht dagegen? Mal abgesehen davon dass es völlig sinnlos ist, da Textmodus der Default ist.



  • transcend schrieb:

    This ANSI-C helper collection is compiled from the sources of various different sites found on the internet, in particular:
    ...

    Hat der eine Emailadresse? Dann könnte man ihn fragen, warum er den zahllosen halbherzigen C-Referenzen eine weitere hinzufügt. Es ist ärgerlich, dass solche Referenzen selten hinreichend zwischen Standard-C und den Erweiterungen unterscheiden.

    In text mode the end of file is assumed to be at first Ctrl-Z character.

    Was ist ein Ctrl-Z character?

    transcend schrieb:

    Wenn ein Compiler "rt" unterstützt und das auch dokumentiert, und man sich dessen bewusst ist, kann man das doch verwenden, was spricht dagegen? Mal abgesehen davon dass es völlig sinnlos ist, da Textmodus der Default ist.

    Abgesehen davon nichts, denke ich. Aber wenn man bedenkt, dass ein Programm bei einem fopen(..., "rt") eigentlich machen darf, was es will, wird man wohl lieber beim Standard bleiben, solange man nicht in Not kommt.



  • Bashar schrieb:

    Wenn ein Compiler "rt" unterstützt und das auch dokumentiert, und man sich dessen bewusst ist, kann man das doch verwenden, was spricht dagegen? Mal abgesehen davon dass es völlig sinnlos ist, da Textmodus der Default ist.

    Dass man sich unnötig Mehrarbeit macht, wenn man den Kram mal portieren will.

    Ctrl+Z ist das Kontrollzeichen SUB in ASCII. In der DOS-Konsole wurde (wird?) wird die Tastenkombination allerdings als end-of-transmission benutzt, analog zu Ctrl+D überall sonst. Mit Textmodus hat das natürlich herzlich wenig zu tun, wobei der Textmodus unter DOS bei einer ganzen Reihe von Kontrollzeichen abbricht, wenn diese tatsächlich in der Datei stehen.

    Übrigens:

    transcend schrieb:

    In most compilers this default method is specified by the global variable _fmode defined in "stdio.h".

    Dazu kann ich nur sagen:

    $ cat test.c
    #include <stdio.h>
    
    int main(void) {
      _fmode;
    }
    $ gcc test.c
    test.c: In Funktion »main«:
    test.c:4:3: Fehler: »_fmode« undeclared (first use in this function)
    test.c:4:3: Anmerkung: each undeclared identifier is reported only once for each function it appears in
    $
    

    Der Link zu Martin Leslies Referenz ist tot, und der andere zeigt auf eine Referenz von 1997. Meine Vermutung ist, dass er vor zehn, zwölf Jahren einen Hiwi damit beauftragt hat, ihm etwas zusammenzustellen, dass er seinen Studenten in die Hand drücken kann, und dass der halt den erstbesten Kram aus einer Suchmaschine zusammenkopiert hat. Die Info scheint für DOS-Compiler durchaus richtig zu sein, und möglicherweise unterstützt MSVC den Kram sogar noch (Microsoft schmeißt selten ein Interface weg) - nur ist das alles halt kein ANSI-C.



  • seldon schrieb:

    Meine Vermutung ist, dass er vor zehn, zwölf Jahren einen Hiwi damit beauftragt hat, ihm etwas zusammenzustellen, dass er seinen Studenten in die Hand drücken kann, und dass der halt den erstbesten Kram aus einer Suchmaschine zusammenkopiert hat.

    Ach was, der hätte zumindest whose name is stored geschrieben.
    🙂


Anmelden zum Antworten