Visualizzazione dei risultati da 1 a 6 su 6

Discussione: La funzione fopen in C

  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2004
    Messaggi
    3

    La funzione fopen in C

    Buona sera a tutti. Avrei bisogno di un aiuto, voglio provare a compilare un programma in C che una volta eseguito mi apra un file di testo..Nelle pagine di programmazione di html.it ho trovato la funzione fopen con incluso il listato ma non riesco ad usarlo, ho provato ad usare quello nell'esempio senza mie modifiche ma il problema persiste. Se qualcuno può darmi una mano...grazie...

  2. #2
    Utente bannato
    Registrato dal
    Jan 2003
    Messaggi
    1,414
    Ti pasto un man fopen , probabilmente la migliore documentazione possibile

    codice:
    NAME
           fopen, fdopen, freopen - stream open functions
    
    SYNOPSIS
           #include <stdio.h>
    
           FILE *fopen(const char *path, const char *mode);
           FILE *fdopen(int fildes, const char *mode);
           FILE *freopen(const char *path, const char *mode, FILE *stream);
    
    DESCRIPTION
           The  fopen  function opens the file whose name is the string pointed to
           by path and associates a stream with it.
    
           The argument mode points to a string beginning with one of the  follow-
           ing sequences (Additional characters may follow these sequences.):
    
           r      Open  text  file  for  reading.  The stream is positioned at the
                  beginning of the file.
    
           r+     Open for reading and writing.  The stream is positioned  at  the
                  beginning of the file.
    
           w      Truncate  file  to  zero length or create text file for writing.
                  The stream is positioned at the beginning of the file.
    
           w+     Open for reading and writing.  The file is created  if  it  does
                  not  exist, otherwise it is truncated.  The stream is positioned
                  at the beginning of the file.
    
           a      Open for appending (writing at end of file).  The file  is  cre-
                  ated  if it does not exist.  The stream is positioned at the end
                  of the file.
    
           a+     Open for reading and appending (writing at end  of  file).   The
                  file  is created if it does not exist.  The stream is positioned
                  at the end of the file.
    The mode string can also include the letter  ``b''  either  as  a  last
           character  or  as a character between the characters in any of the two-
           character strings described above.  This is strictly for  compatibility
           with  ANSI  X3.159-1989  (``ANSI  C'')  and has no effect; the ``b'' is
           ignored on all POSIX conforming systems, including Linux.  (Other  sys-
           tems  may treat text files and binary files differently, and adding the
           ``b'' may be a good idea if you do I/O to a binary file and expect that
           your program may be ported to non-Unix environments.)
    
           Any         created         files         will         have        mode
           S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH (0666), as modified  by
           the process' umask value (see umask(2)).
    
           Reads  and writes may be intermixed on read/write streams in any order.
           Note that ANSI C requires that a file  positioning  function  intervene
           between  output and input, unless an input operation encounters end-of-
           file.  (If this condition is not met, then a read is allowed to  return
           the result of writes other than the most recent.)  Therefore it is good
           practice (and indeed sometimes necessary under Linux) to put  an  fseek
           or  fgetpos  operation  between  write  and  read  operations on such a
           stream.  This operation may be an apparent no-op (as in fseek(...,  0L,
           SEEK_CUR) called for its synchronizing side effect.
    
           Opening a file in append mode (a as the first character of mode) causes
           all subsequent write operations to this stream to occur at end-of-file,
           as if preceded by an
                  fseek(stream,0,SEEK_END);
           call.
    
           The fdopen function associates a stream with the existing file descrip-
           tor, fildes.  The mode of the stream (one of the values "r", "r+", "w",
           "w+",  "a", "a+") must be compatible with the mode of the file descrip-
           tor.  The file position indicator of the new  stream  is  set  to  that
           belonging  to  fildes,  and  the  error  and end-of-file indicators are
           cleared.  Modes "w" or "w+" do not cause truncation of the  file.   The
           file  descriptor is not dup'ed, and will be closed when the stream cre-
           ated by fdopen is closed.  The result of applying fdopen  to  a  shared
           memory object is undefined.
    The freopen function opens the file whose name is the string pointed to
           by path and associates the stream pointed to by stream  with  it.   The
           original  stream  (if  it exists) is closed.  The mode argument is used
           just as in the fopen function.  The primary use of the freopen function
           is  to  change the file associated with a standard text stream (stderr,
           stdin, or stdout).
    
    RETURN VALUE
           Upon successful completion fopen, fdopen  and  freopen  return  a  FILE
           pointer.   Otherwise, NULL is returned and the global variable errno is
           set to indicate the error.
    
    ERRORS
           EINVAL The mode provided to fopen, fdopen, or freopen was invalid.
    
           The fopen, fdopen and freopen functions may also fail and set errno for
           any of the errors specified for the routine malloc(3).
    
           The  fopen  function  may also fail and set errno for any of the errors
           specified for the routine open(2).
    
           The fdopen function may also fail and set errno for any of  the  errors
           specified for the routine fcntl(2).
    
           The  freopen function may also fail and set errno for any of the errors
           specified for the routines open(2), fclose(3) and fflush(3).

  3. #3

    Re: La funzione fopen in C

    Originariamente inviato da winzoz
    Buona sera a tutti. Avrei bisogno di un aiuto, voglio provare a compilare un programma in C che una volta eseguito mi apra un file di testo..Nelle pagine di programmazione di html.it ho trovato la funzione fopen con incluso il listato ma non riesco ad usarlo, ho provato ad usare quello nell'esempio senza mie modifiche ma il problema persiste. Se qualcuno può darmi una mano...grazie...
    Potresti dire qual'è il problema preciso che hai?

    E magari che compilatore e sistema operativo stai usando?

    per fare un semplice esempio:

    codice:
    char filename[] = "/home/raoul/testo.txt";
    FILE *fp
    fp = fopen(filename, "r"); /*apro il file in modalita "r", ovvero in lettura*/

  4. #4
    Utente di HTML.it
    Registrato dal
    Nov 2004
    Messaggi
    3
    Intanto voglio ringraziare maiosyet x il man fopen anche se sarebbe stato meglio qualcosa di italiano vista la mia ignoranza in inglese.

    X RaouL_BennetH, ecco per te le spiegazioni richieste.
    Ho un windows xp home edition e il compilatore della Borland, Borland C++ 5.02.
    Il mio problema sta nel fatto che riporto il seguente codice(preso da una guida di HTML.it)e mi genera un errore sulla stringa "stream = fopen("miofile.txt", "r");"dicendo:"Error: noname01.cpp(10,38):Extra parameter in call to fopen()"e un warning sulla parentesi graffa di chiusura programma dicendo:"Warn : noname01.cpp(11,3):'stream' is assigned a value that is never used".
    Il listato è :
    #include <stdio.h>
    int main()
    {
    FILE *stream, *fopen();
    stream = fopen("miofile.txt", "r");
    }
    Se adesso ai qualche consiglio....grazie...

  5. #5
    Originariamente inviato da winzoz
    Intanto voglio ringraziare maiosyet x il man fopen anche se sarebbe stato meglio qualcosa di italiano vista la mia ignoranza in inglese.

    X RaouL_BennetH, ecco per te le spiegazioni richieste.
    Ho un windows xp home edition e il compilatore della Borland, Borland C++ 5.02.
    Il mio problema sta nel fatto che riporto il seguente codice(preso da una guida di HTML.it)e mi genera un errore sulla stringa "stream = fopen("miofile.txt", "r");"dicendo:"Error: noname01.cpp(10,38):Extra parameter in call to fopen()"e un warning sulla parentesi graffa di chiusura programma dicendo:"Warn : noname01.cpp(11,3):'stream' is assigned a value that is never used".
    Il listato è :
    #include <stdio.h>
    int main()
    {
    FILE *stream, *fopen();
    stream = fopen("miofile.txt", "r");
    }
    Se adesso ai qualche consiglio....grazie...
    ecco il codice corretto! (dichiaravi come puntatore a file la funzione fopen() )
    codice:
    #include <stdio.h>
    int main(){
    
    FILE *stream;
    stream = fopen("miofile.txt", "r");
    }
    Vediamo..sogni che diventano professione...passioni che diventano vita... Free as in Freedom...

  6. #6
    Utente di HTML.it
    Registrato dal
    Nov 2004
    Messaggi
    3

    Grazie Mille andrea_NET-_DSL

    Grazie andrea_NET-_DSL, adesso è ok il mio listato ma ho un ultima domanda per te e x tutti coloro che vogliono rispondermi;
    mi scuso intanto x l'ignoranza ma io pensavo che l'istruzione fopen con opzione read, una volta avviato il programma mi aprisse il file .txt da me scelto, cosa ke non succede..è un errore mio ke non so mettere bene il percorso del file?(se è così fate per piacere un esempio)oppure è proprio l'istruzione che non prevede l'effettiva apertura del file?grazie x la pasienza..

    P.S. X ki si occupa della gestione della guida sul linguaggio C di HTML.it in merito alla funzione fopen e all'esempio del programma...ATTENZIONE perchè come dimostrato da andrea_NET-_DSL il listato è leggermente errato. Grazie cmq..fornite un buon servizio..

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.