0

I want to read a string with fread() from a file. It seems to me that fread() does not zero terminate a string on its own as I did not find any evidence in the documentation. On all trials I did with fread() the strings were in fact zero terminated but that is nothing I want to rely on. So my plan would be to just allocate an aditional byte for the string/buffer and set it to zero. But what happens if the string already was zero terminated. Now it would be doubled zero terminated. Could I run into problems there?

notAuser
  • 31
  • 3
  • 2
    `On all trials I did with fread() the strings were in fact zero terminated` Are you sure? I mean, are you sure the terminator was put there by `fread` and not already present by accident? `Could I run into problems there?` No. – tkausl Oct 09 '21 at 01:35
  • 1
    Sure you can include an extra zero terminator. It's a waste of a byte, but you can do it and the string would process correctly using C string functions. What you need to do, though, whenever using a C library function that handles a string, is to read the documentation for the function. It will tell you how it handles zero termination, especially in the case where it is returning a string. If you want to read strings from a file, why not use `fgets`? – lurker Oct 09 '21 at 01:35
  • 4
    You're correct that ```fread()``` does not zero-terminate a string. ```fread()``` doesn't interpret what it reads, it just reads bytes. – sj95126 Oct 09 '21 at 01:36
  • `fread()` doesn't read strings _per se_. It will read whatever byes are present in the file and write them to your buffer, so it will only write NUL if it is present in the stream. Please add some code to your question that show what you've tries - that will make it much easier to find out you why you are seeing NUL-terminated strings. – Ken Wayne VanderLinde Oct 09 '21 at 01:38
  • @tkausl of course that would be the most logical explaination and somehow holds true for my case. It could be from the editor I used to "test" this but it also seems to be the case from a http response I used. But could be all coincidents and it seems likely that the zero is already there. – notAuser Oct 09 '21 at 01:40
  • @lurker for the fgets() part. I want to save an entire file as a string. fread() in combination with fseek() seemed like a good option in comparison to read line by line. But I could be wrong. – notAuser Oct 09 '21 at 01:46
  • You mean the file is one long zero terminated string with no zero terminations in between? Then yes as others have noted you need to process the null terminator explicitly yourself since `fread` knows nothing about "strings" *per se*. If you are reading a fixed length each time, you could preload a 0 at the appropriate point. Otherwise, append 0 byte after each read. – lurker Oct 09 '21 at 01:49
  • `You mean the file is one long zero terminated string with no zero terminations in between?` Yes exactly. But like I said in my usecase the zero is often, if not always there at the end of the file. My concern is some unexpected bahaviour if I add a zero after the zero. But as I understand functions which process strings just stop at the first zero they find. So I guess it should be fine. – notAuser Oct 09 '21 at 01:56
  • according to the MSDN DOCS, it recommends that you SHOULD NULL terminate your buffer. Since the converted data may be shorter than the stream data copied into the buffer, data past `buffer[return_value * size]` (where return_value is the return value from fread) may contain unconverted data from the file. For this reason, we recommend you null-terminate character data at `buffer[return_value * size]` if the intent of the buffer is to act as a C-style string. [link]https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fread?view=msvc-160 – dmaelect Oct 09 '21 at 02:03

0 Answers0