Read an input file into a single string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pxllyte
    New Member
    • Nov 2006
    • 3

    Read an input file into a single string

    The input file data size is unknown so I would have to use malloc( ) to allocate enough memory for the file data. In order to do this, I would need to read each character of the file possibly by using a counter and then returning the counter as "totalChar" and allocating memory for it. The code I have so far is:

    Code:
    #define FILENAME_SIZE 15
    #define FILESIZE_MAX 10000
    
    char filename[FILENAME_SIZE];
    char string[FILESIZE_MAX];
    
    FILE *ifp;
    printf("Enter the name of the file to analyze :");
    scanf("%s", filename);
    printf("\n");
    
    ifp = fopen(filename, "r");
    if (ifp == NULL)
    {
       fprintf(stderr, "Could not open file: '%s' ", filename);
       exit (-1);
    }
    
    int c = fgetc(ifp);
    
    while (c != EOF)
    {
    printf("%c", c);
    c=fgetc(ifp);
    }
    
    fclose(ifp);
    I tried to put in a counter in the while loop but that did not work at all. It just stopped the loop somehow. I'm stumped.

    My memory allocation would look like this:
    Code:
    string = (char) malloc(totalChar * sizeof(char));
    Am I on the right track of putting the input file into a single string?

    Thanks!
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you could get the file length using the fstat() function, create dynamic storage of the required size and then use read() to read that number of bytes into a char[], e.g.
    Code:
    // read file size and read file contents into a string
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    #define FILENAME_SIZE 15
    
    char filename[FILENAME_SIZE];
    char *string;
    
    int main()
    {
    struct stat file_stat;
    int         status, readSize;
    
    printf("Enter the name of the file to analyze :");
    scanf("%s", filename);
    printf("\n");
    int fildes = open(filename, O_RDONLY);             // open file
    if (fildes < 0)
    {
       fprintf(stderr, "Could not open file: '%s' ", filename);
       exit (-1);
    }
    
    // get file status for details see
    // http://www.opengroup.org/onlinepubs/007908799/xsh/fstat.html
    status = fstat(fildes, &file_stat);                 // get its status
    
    // print file size informations for details see
    //   http://www.opengroup.org/onlinepubs/007908799/xsh/sysstat.h.html
    printf("file %s size %d\n", filename, file_stat.st_size);        // print status
    
    // create storage for string and read it in
    string=calloc(file_stat.st_size+1, sizeof(char));
    readSize=read(fildes, string, file_stat.st_size);
    
    // terminate the string at number of characters read and print it
    string[readSize]='\0';
    printf("%s", string);
    close(fildes);
    getchar();
    getchar();
    return 0;
    }

    Comment

    Working...