Question:
How do you save the characters of a file into a string or array in C programming?
anonymous
2013-09-27 17:23:54 UTC
For example, if I had a file name random.txt, which read:

This is a string.
Abc
Zxy

How would you save the characters in random.txt to a string or array that includes all of the characters in the text file?
Three answers:
Jonathan
2013-09-27 21:39:40 UTC
Learn from:



#include

#include

#include

int fc( FILE * f ) {

    static int priorc;

    int c= fgetc( f );

        if ( f == stdin && c == '$' && priorc == '\n' ) {

            int d= fgetc( f );

            if ( d == '\n' ) return EOF;

            ungetc( d, f );

        }

    return priorc= c;

}

char * readfile( FILE * f ) {

    int c;

    char *result, *b;

    size_t maxn, extend, cn;

    enum { greedy, miserly } mode= greedy;

        if ( f == NULL ) return NULL;

        result= (char *) malloc( maxn= extend= 1000U );

        if ( result == NULL ) return NULL;

        for ( cn= 0, c= fc( f ); c != EOF; c= fc( f ) ) {

            while ( cn >= maxn && extend > 0 ) {

                b= (char *) realloc( result, maxn + extend );

                if ( b != NULL ) {

                    result= b;

                    maxn += extend;

                    if ( mode == greedy ) extend= maxn;

                    break;

                }

                mode= miserly;

                extend >>= 1;

            }

            if ( cn >= maxn && !extend ) {

                free( result );

                return NULL;

            }

            result[cn++]= c;

        }

        result[cn++]= '\0';

    return (char *) realloc( result, cn );

}

char * readfilename( const char * filename ) {

    FILE *f;

    char *result;

        if ( (f= fopen( filename, "r" )) != NULL ) {

            result= readfile( f );

            fclose( f );

        } else

            result= NULL;

    return result;

}

void writefile( const char *filename, const char *text ) {

    FILE *f;

    int c;

        if ( (f= fopen( filename, "w" )) == NULL ) return;

        while ( (c= (int) *text++) != '\0' )

            fputc( c, f );

        fclose( f );

    return;

}

int getoption( void ) {

    char buf[200];

    int option;

        printf( "\n0.\t Quit the program\n"

                "1.\t Read text from selected file into an array\n"

                "2.\t Write text from the array onto selected file\n"

                "3.\t Enter text from the keyboard into the array\n"

                "4.\t Display the array\n\n"

                "Option code: " );

        if ( fgets( buf, sizeof( buf ), stdin ) == NULL ) return 0;

        if ( sscanf( buf, "%d", &option ) != 1 ) return 0;

    return option;

}

char * getfilename( void ) {

    char buf[200], *s, *filename;

        printf( "Enter filename: " );

        if ( fgets( buf, sizeof( buf ), stdin ) == NULL ) return NULL;

        for ( s= buf; *s != '\0'; ++s )

            if ( *s != ' ' ) break;

        s= strtok( s, "\t\n\r" );

        if ( s == NULL ) return NULL;

        filename= (char *) malloc( strlen( s ) + 1 );

        if ( filename == NULL ) return NULL;

        strcpy( filename, s );

    return filename;

}

int main( void ) {

    int option, c;

    char *filename, *text= NULL;

        for ( option= getoption(); option != 0; option= getoption() )

            switch ( option ) {

            case 1:

                for ( ; ; ) {

                    filename= getfilename();

                    if ( filename == NULL ) break;

                    if ( text != NULL ) free( text );

                    text= readfilename( filename );

                    free( filename );

                    if ( text != NULL ) break;

                    printf( "Can't read the given filename.\n" );

                }

                break;

            case 2:

                if ( text == NULL ) {

                    printf( "Nothing to write. Empty array.\n" );

                    break;

                }

                filename= getfilename();

                if ( filename == NULL ) break;

                writefile( filename, text );

                free( filename );

                break;

            case 3:

                printf( "Enter text (Use '$' on a blank line to terminate input):\n" );

                text= readfile( stdin );

                break;

            case 4:

                if ( text == NULL )

                    printf( "Nothing to display. Empty array.\n" );

                else {

                    printf( "\n --- CONTENTS ---\n" );

                    while ( (c= (int) *text++) != '\0' )

                        fputc( c, stdout );

                    printf( " --- END CONTENTS ---\n\n" );

                }

                break;

            default:

                break;

            }

    return 0;

}
?
2013-09-28 01:54:35 UTC
Correction to the first reply: the fgets should be:



y = fgets(ip); or y = fgets(ip, 512);



You should always use fclose to close a file that's been opened, by the way, to free up resources.



You could, as a safety measure, use fstat to check file size before attempting the reads - to make sure you won't go over the declared array bound.



http://www.php.net/manual/en/function.fstat.php
James Bond
2013-09-28 01:00:11 UTC
I doubt your idea of bringing into an array is suitable for big files or not. Anyhow, the following may help you.



FILE *ip;

char x[5120],y[512]; //character arrays



ip=fopen("random.txt","r");

while(!feof(ip))

{

fgets(y,512,ip);

strcat(x,y);

}



Now, string x contains what you want


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...