Thread: Why do I get conflicting types on this header file?

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ArakelTheDragon View Post
    Can you give an example about "i++" and "++i", since I am a young padawan.

    c - What is the difference between ++i and i++? - Stack Overflow
    You already have an example from that StackOverflow post, so why do you need another one?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ArakelTheDragon
    Here is my new code:
    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:
    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;
    }
    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.

    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    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;
    }

  4. #19
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conflicting Types
    By TKEsquad in forum C Programming
    Replies: 5
    Last Post: 05-08-2015, 08:31 AM
  2. Replies: 7
    Last Post: 01-22-2014, 09:48 AM
  3. conflicting types
    By Sotiris Kaniras in forum C Programming
    Replies: 27
    Last Post: 01-01-2013, 09:18 AM
  4. Conflicting types???
    By kwikness in forum C Programming
    Replies: 11
    Last Post: 10-07-2007, 11:53 PM
  5. conflicting types for...
    By dudinka in forum C Programming
    Replies: 3
    Last Post: 05-14-2005, 07:03 AM

Tags for this Thread