Carl
6/1/2011 12:53:00 PM
On 6/1/2011 8:09 AM, Luca Forlizzi wrote:
> Hello,
>
> I would like to write a small example of I/O using standard library's
> streams.
> I would like the example to show how to test for possible errors
> occurred during
> file operations. Since I am not very experienced on the issue, I
> would like to ask
> if someone may give a look to my code and tell me if something is
> wrong.
> Of course the example works as I intended, in the implementation
> I'am using now, when no error occurs during file operations, but I
> don't have the possibility
> to check it on many other implementations, and I don't know how to
> make the file operations
> fail in order to check the successful detection of the failure.
> I hope no one is annoyed by the small comments in italian language.
>
> #include<stdio.h>
> #include<stdlib.h>
>
> #define NOME_FILE_BIN_NUMERICO "streamBinario_formatoNumerico.out"
> #define NOME_FILE_TEXT_NUMERICO "streamTesto_formatoNumerico.out"
> #define NOME_FILE_BIN_TESTUALE "streamBinario_formatoTestuale.out"
> #define NOME_FILE_TEXT_TESTUALE "streamTesto_formatoTestuale.out"
>
> /* Funzione che stampa un messaggio di errore ed esce indicando
> un fallimento all'ambiente operativo */
> void fallisci(void) {
> printf("Fallimento: Errore in un'operazione su file!\n");
> exit(EXIT_FAILURE);
> }
>
> int main(void) {
>
> int i;
> FILE *stream_bin_numerico, *stream_text_numerico,
> *stream_bin_testuale, *stream_text_testuale;
>
> struct {
> float f;
> unsigned char c1, c2;
> } dati[] = {
> { 12.34f, 5, '5' },
> { 130.0f, 10, 'a' },
> { 108.0f, 108, 130 },
> };
>
> /* Apre i file */
>
> /* apre il primo in modo poco conciso */
> stream_bin_numerico = fopen( NOME_FILE_BIN_NUMERICO, "wb");
> if (stream_bin_numerico == NULL) fallisci();
>
> /* apertura un po' piu' concisa */
> if ( (stream_text_numerico = fopen( NOME_FILE_TEXT_NUMERICO, "w"))
> == NULL) fallisci();
>
> /* ...ancora piu' conciso! */
> if ( !(stream_bin_testuale = fopen( NOME_FILE_BIN_TESTUALE, "wb")) )
> fallisci();
> if ( !(stream_text_testuale = fopen( NOME_FILE_TEXT_TESTUALE,
> "w")) ) fallisci();
>
> /* scrive nei file */
> for (i=0; i< (sizeof dati)/(sizeof dati[0]); i++) {
>
> /* output numerico */
> if ( fwrite(&dati[i].f, sizeof dati[i].f, 1,
> stream_bin_numerico )< 1) fallisci();
> if ( fputc( dati[i].c1, stream_bin_numerico ) == EOF ) fallisci();
> if ( fputc( dati[i].c2, stream_bin_numerico )< 0 ) fallisci();
>
> if ( fwrite(&dati[i].f, sizeof dati[i].f, 1,
> stream_text_numerico )< 1) fallisci();
> if ( fputc( dati[i].c1, stream_text_numerico ) == EOF )
> fallisci();
> if ( fputc( dati[i].c2, stream_text_numerico )< 0 ) fallisci();
>
>
> /* output testuale */
> if ( fprintf( stream_bin_testuale, "%f %c %c\n", dati[i].f,
> dati[i].c1, dati[i].c2 )< 0) fallisci();
> if ( fprintf( stream_text_testuale, "%f %c %c\n", dati[i].f,
> dati[i].c1, dati[i].c2 )< 0) fallisci();
>
> }
>
> /* Chiude i file, controlla anche il successo dell'operazione */
> if ( fclose( stream_bin_numerico ) == EOF ) fallisci();
> if ( fclose( stream_text_numerico ) != 0 ) fallisci();
> if ( fclose( stream_bin_testuale ) ) fallisci();
> if ( fclose( stream_text_testuale ) ) fallisci();
>
>
> return 0;
> }
I tried compiling it with gcc -Wall -Werror -ansi -pedantic with no
complaints, and lint only had minor complaints about unsigned char to
int upcasts.
From my cursory review it looks good.
--
Bill