Thread: fread creates a string with crap at the end

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    19

    fread creates a string with crap at the end

    I want to read a whole text file a once into a single string. I have used the fread() function in the following way:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	FILE *fInp;
    	char buffer[1000];
    
    	fInp = fopen("input.txt", "r");
    	fread(buffer, 2, 1000, fInp);
    
    	printf("%s", buffer);
    
    	getchar();
    
    }
    My input.txt is:
    Code:
    i want to read that into a single string
    However it produces a string:
    Code:
    i want to read that into a single stringĚĚĚĚĚĚĚĚĚ<loads of identical crap>ĚĚĚĚĚĚĚĚĚĚĚĚhűbĚĚĚĚĚĚĚĚHűbĚĚĚĚ«_·=\ű
    How to get rid of that crap if i do not know beforehand how many characters are my input.txt?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    fread returns number of items successfully. Read the doc!
    You need to null terminate('\0') at the end of your array to make it a C string. So that printf() and all other functions that operate on C string work properly.
    The second parameter of fread() is the size of each element, in this case sizeof(char) = 1.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    19
    Okay, I get that but how to find out in which place of my buffer array I need to put the null terminator?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    He already told you. fread() returns the number of items read successfully. Check fread()'s return value and use it. Also, why are you using 2 for the second parameter to fread()? It should be 1.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I would fix the fread call according to Bayint's advice first and see if it works. The only time you would need to insert one zero byte at the end would be if there wasn't one in the file already (or if you read a string of sufficient length greater than 1000) which I find strange.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    19
    Quote Originally Posted by itsme86 View Post
    He already told you. fread() returns the number of items read successfully. Check fread()'s return value and use it. Also, why are you using 2 for the second parameter to fread()? It should be 1.
    This is a typo, ofcourse it should be 1.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Oh wait, you're not even reading in binary mode. Use fgets instead of fread in that case.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    19
    fgets will read only one line of the file, what if there is more and i still want them all in one string?

    And that code below works, is that what you tried to tell me to fix?
    Code:
    #include <stdio.h>
    
    int main()
    {
    	FILE *fInp;
    	char buffer[1000];
    	int kutafon;
    
    	fInp = fopen("input.txt", "r");
    	kutafon = fread(buffer, 1, 1000, fInp);
    	buffer[kutafon] = '\0';
    	
    	printf("%s", buffer);
    
    	getchar();
    }
    Last edited by kulfon; 12-15-2010 at 10:18 AM.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Yes, But don't forget that you can't read 1000 all since you need to leave 1 more room for '\0'!
    Just imagine fread() reads 1000 chars, then return value will be 1000. buffer[1000] does not exist.(you don't own that space).
    kutafon should be declared as size_t.
    You might want to use feof(),ferror() to check if there's any error while reading file....

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    fgets will read only one line of the file, what if there is more and i still want them all in one string?
    Call fgets in a loop and concatenate all the strings together with say strncat, maybe?

  11. #11
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    I know this was just a learning exercise, but standard programming practice is to put the file-to-string conversion into a function and make it reusable. A rudimentary example function might take a file name and max size as arguments, check if file exists and is the required size, read it into an appropriately-sized dynamically-allocated buffer, check errors, and return a pointer to the buffer or error...

    Not sure about Windows, but POSIX also has memory-mapped I/O, functions that treat files as memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM