fprintf??



  • hallo!
    ich soll in eine datei eine zeichenkette mittels der funktion fprintf() schreiben, hab aber keine ahnugn was die fprintf funktion amcht/wie sie funktioniert/wie die syntax ist...
    MfG

    Marco



  • Ich hoffe das erscheint jetzt nicht zu herzlos 😉
    [code]
    PRINTF(3) Linux Programmer's Manual PRINTF(3)

    NAME
    printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - formatted output conversion

    SYNOPSIS
    #include <stdio.h>

    int printf(const char *format, ...);
    int fprintf(FILE *stream, const char *format, ...);
    int sprintf(char *str, const char *format, ...);
    int snprintf(char *str, size_t size, const char *format, ...);

    #include <stdarg.h>

    int vprintf(const char *format, va_list ap);
    int vfprintf(FILE *stream, const char *format, va_list ap);
    int vsprintf(char *str, const char *format, va_list ap);
    int vsnprintf(char *str, size_t size, const char *format, va_list ap);

    DESCRIPTION
    The functions in the printf family produce output according to a format
    as described below. The functions printf and vprintf write output to
    stdout, the standard output stream; fprintf and vfprintf write output
    to the given output stream; sprintf, snprintf, vsprintf and vsnprintf
    write to the character string str.

    The functions vprintf, vfprintf, vsprintf, vsnprintf are equivalent to
    the functions printf, fprintf, sprintf, snprintf, respectively, except
    that they are called with a va_list instead of a variable number of
    arguments. These functions do not call the va_end macro. Consequently,
    the value of ap is undefined after the call. The application should
    call va_end(ap) itself afterwards.

    These eight functions write the output under the control of a format
    string that specifies how subsequent arguments (or arguments accessed
    via the variable-length argument facilities of stdarg(3)) are converted
    for output.

    Return value
    Upon successful return, these functions return the number of characters
    printed (not including the trailing '\0' used to end output to
    strings). The functions snprintf and vsnprintf do not write more than
    size bytes (including the trailing '\0'). If the output was truncated
    due to this limit then the return value is the number of characters
    (not including the trailing '\0') which would have been written to the
    final string if enough space had been available. Thus, a return value
    of size or more means that the output was truncated. (See also below
    under NOTES.) If an output error is encountered, a negative value is
    returned.
    [/code]

    Wenn man ein char Array specihern will, könnte es wie folgt aussehen:

    #include <stdio.h>
    
    int main()
    {
      char s[50];
      FILE *la;
    
      if((la = fopen("la.txt", "w")))
        return -1;
    
      fprintf(file, "%s", s);
    
      return 0;
    }
    

    Gruß
    grottenolm





  • if((la = fopen("la.txt", "w")))
    return -1;

    Diese Bedingung ist falsch.
    if( (la = fopen("la.txt", "w")) == NULL) oder if(!(la = fopen("la.txt", "w")))
    würde gehen.



  • Grüezi wohl!
    ich würde gerne simple die zahl 20 in ein txt schreiben, aber der char gibt immer 0 aus....ich glaub der is nicht ganz normal! 😮

    uges = 20;
    char s[255];
    sprintf(s,"%d",uges);
    fz = fopen("filecreate.txt","w");
    fprintf(fz,"%s",s);
    

    FILE und so es natürlech alles schon oben passiert, a dem sollte es nicht liegen...
    help plz!



  • Und dafür gräbst du einen 4 Jahre alten Hread raus? 🙄



  • jou! mann muss ja nicht immer einen neuen anfangen...dachte das passt hier hin...
    aber ned gescheit antwort wäre mir lieber gewesen...als agressives mobing! 😉



  • Tomsoja schrieb:

    jou! mann muss ja nicht immer einen neuen anfangen...dachte das passt hier hin...

    Nicht wirklich - du hast ein neues Problem, also kannst du gerne einen neuen Thread aufmachen 😉

    Zum Thema:
    Erstens: Wozu überhaupt der Umweg über einen String - du kannst die Zahl auch direkt mit fprintf() schreiben.
    Zweitens: Schonmal daran gedacht, den Debugger zu nutzen? Damit könntest du auf jeden Fall verfolgen, was wo eingetragen wird.



  • Vielleicht mal die Datei wieder schließen ..


Anmelden zum Antworten