File reading

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mjbauer95
    New Member
    • Jan 2009
    • 13

    File reading

    What is the best way to read a file in C?
    I have heard of using fread but then how do you find the size of the file?

    This is my example code but it seems to have odd errors.
    Code:
    char * read(char * filename){
    	FILE * fo;
    
    	fo = fopen(filename, "r");
    	if(fo){
    		char * buffer = NULL;
    		size_t result;
    		int length;
    		length = file_length(fo);
    		result = fread(buffer,1,length,fo);
    		fclose(fo);
    		return buffer;
    	} else {
    		fclose(fo);
    		return NULL;
    	}
    }
  • l034n
    New Member
    • Oct 2008
    • 15

    #2
    Well, it does indeed have errors. Check out this one.

    Code:
    unsigned char* ReadFile(const char* szFileName, long& length);
    
    int main(int argc, char* argv[])
    {
    	long len = 0;
    	const char* filename = "c:\\test.txt";
    	unsigned char* buffer = ReadFile(filename, len);
    
    	if(buffer)
    	{
    		// Do something with the buffer
    		//.....
    
    		// And clean up after yourself
    		delete[] buffer;
    		buffer = NULL;
    	}
    
    	return 0;
    }
    
    unsigned char* ReadFile(const char* szFileName, long& length)
    {
    	length = 0;
    	unsigned char* buffer = NULL;
    	FILE* fo = fopen(szFileName, "r");
    
    	if(fo)
    	{
    		fseek(fo, 0, SEEK_END);
    		length = ftell(fo);
    		fseek(fo, 0, SEEK_SET);
    
    		if(length)
    		{
    			buffer = new unsigned char[length];
    			fread(buffer, length, 1, fo);
    		}
    
    		fclose(fo);
    	}
    
    	return buffer;
    }
    You may also consider using the *_s fuctions (like fopen_s).

    Cheers

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You should be able to seek to the end of the file (SEEK_END) and then do a tell to get the offset from the beginning.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        As long as you opened the file in binary mode, if you opened it in text mode then that is not a reliable method of getting the file size.

        Comment

        • mjbauer95
          New Member
          • Jan 2009
          • 13

          #5
          Does the buffer have to be unsigned char * ?
          Can it be const char * ?

          I am new to C, sorry.

          Comment

          • l034n
            New Member
            • Oct 2008
            • 15

            #6
            Yes, it can be a const char* if you don't want to modify it. So, the line:

            Code:
            unsigned char* buffer = ReadFile(filename, len);
            would be:

            Code:
            const unsigned char* buffer = ReadFile(filename, len);
            And also change the return value of ReadFile(). I wrote it as unsigned char* again because it was how i did it in the first example. But if you want to use char* instead of unsigned char* it doesn't matter, since they are of the same size (1 byte), just interpreted differently. However, i don't know why do you want to use a char*. The manipulation of bytes is better with unsigned char, hence BYTE is defined as unsigned char.

            Comment

            Working...