Tidy up; more/better use of BOOST_FOREACH.
[libdcp.git] / src / util.cc
index d5ba6fab67ea093113c29099f38fea5d74496acf..856bc9bd0001d0d958cbae4334df0824b6cc0a25 100644 (file)
@@ -403,18 +403,17 @@ dcp::file_to_string (boost::filesystem::path p, uintmax_t max_length)
                throw MiscError ("Unexpectedly long file");
        }
 
-       char* c = new char[len + 1];
-
        FILE* f = fopen_boost (p, "r");
        if (!f) {
-               return "";
+               throw FileError ("could not open file", p, errno);
        }
 
-       fread (c, 1, len, f);
+       char* c = new char[len];
+       /* This may read less than `len' if we are on Windows and we have CRLF in the file */
+       int const N = fread (c, 1, len, f);
        fclose (f);
-       c[len] = '\0';
 
-       string s (c);
+       string s (c, N);
        delete[] c;
 
        return s;