Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Uhhh... you did use feof correctly now, but it somewhat complicates the code unnecessarily, and then there's the buffer overflow vulnerability. Here's what I suggest, with optional an assert to do a pre-condition check:Originally Posted by ArakelTheDragon
Notice how I made the result string a pointer as an out parameter instead of using a global or static local variable, and observe how I structure the loop so as to both avoid buffer overflow and correctly control the loop using the return value of fgetc.Code:/* Reads the content of a file into a string and returns the string. * filename: name of the file that will be read. * result: pointer to the first character of the array that will contain the * string read. * result_size: size of the array that will contain the string read, including * the terminating null character. */ char *ReadFileIntoString(const char *filename, char *result, size_t result_size) { assert(filename && result && result_size > 0); FILE *fp = fopen(filename, "r"); if (!fp) { return NULL; } size_t result_len = 0; if (result_size > 1) { int c; --result_size; while (result_len < result_size && (c = fgetc(fp)) != EOF) { result[result_len++] = c; } } fclose(fp); result[result_len] = '\0'; return result; }
Now you can call this function:
Code:int main(void) { char file_content[5000]; if (ReadFileIntoString("somefile.txt", file_content, sizeof(file_content))) { // do something with file_content // ... } else { printf("The dialog was canceled by the user, the file doesn't exists or doesn't have the right permissions!"); } return 0; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Of course, if your file have more bytes than your array, it is better to dynamically allocate the buffer... Something like this:
Code:#include <stdlib.h> #include <stdio.h> #include <sys/stat.h> static size_t read_from_file( char *, char ** ); int main( void ) { size_t fsize; char *buffp; if ( fsize = read_from_file( "myfile.txt", &buffp ) ) { printf( "Read %zu bytes from file:\n" "-----------------------------------------------\n" "%s\n", fsize, buffp ); free( buffp ); } else { fputs( "ERROR reading file.\n", stderr ); return EXIT_FAILURE; } return EXIT_SUCCESS; } size_t read_from_file( char *fname, char **buffpp ) { FILE *f; struct stat st; char *p; // Tries to open the file... if ( f = fopen( fname, "r" ) ) { // Tries to get the file size... if ( fstat( fileno( f ), &st ) ) { fclose( f ); return 0; } // Tries to allocate the buffer. if ( p = malloc( st.st_size + 1 ) ) { // Tries to read the entire file. // This should work for really big files as well, since // fread() uses size_t for an element size. // Notice that fread() returns the number of elements read. if ( fread( p, st.st_size, 1, f ) == 1 ) { fclose( f ); p[st.st_size] = '\0'; // Last byte is NUL. *buffpp = p; return st.st_size; } } fclose( f ); } return 0; }
Notice that I did use something a little bit dangerous for inexperienced programmers in C: Inside the if statement there are assignment operators (not the equal conditional operator ==).
And sorry for the double posting (#18 and #19 above)... I don't know how to delete my own posts here!...
Last edited by flp1969; 03-17-2019 at 10:24 PM.