0

I'm trying to determine how big a file i'm reading is in bytes so I used Fseek to jump to the end and it triggered the error: file.exe has triggered a breakpoint. Heses the code: FileUtils.cpp: #include "FileUtils.h"

namespace impact {

    std::string read_file(const char* filepath)
    {
        FILE* file = fopen(filepath, "rt");
        fseek(file, 0, SEEK_END);
        unsigned long length = ftell(file);
        char* data = new char[length + 1];
        memset(data, 0, length + 1);
        fseek(file, 0 ,SEEK_SET);
        fread(data, 1, length, file);
        fclose(file);

        std::string result(data);
        delete[] data;
        return result;
    }

}

FileUtils.h:

    #pragma once
#include <stdio.h>
#include <string>
#include <fstream>


namespace impact {
    std::string read_file(const char* filepath);
}

If more info is required just ask me for it I would be more than happy to provide more!

  • 1
    `FILE* file = fopen(filepath, "rt");` -- You did not check if the file opened successfully. – PaulMcKenzie Feb 27 '20 at 18:27
  • For more details about how to determine the size of a file using fstream,I suggest you could refer to the links: https://stackoverflow.com/questions/2409504/using-c-filestreams-fstream-how-can-you-determine-the-size-of-a-file https://stackoverflow.com/questions/5840148/how-can-i-get-a-files-size-in-c – Jeaninez - MSFT Feb 28 '20 at 08:42

1 Answers1

0

You are doing this in the C way, C++ has much better (in my opinion) ways of handling files.

Your error looks like it may be caused because the file didn't open correctly (you need to check if file != nullptr).

To do this in C++17 you should use the standard library filesystem (Note: You can also do this with C++11 experimental/filesystem using std::experimental::filesystem namespace)

Example:

std::string read_file(const std::filesystem::path& filepath) {
    auto f_size = std::filesystem::file_size(filepath);
    ...
}

Additionally to read a file in C++ you do not need to know the size of the file. You can use streams:

std::string read_file(const std::filesystem::path& filepath) {
   std::ifstream file(filepath); // Open the file

   // Throw if failed to open the file
   if (!file) throw std::runtime_error("File failed to open");

   std::stringstream data; // Create the buffer
   data << file.rdbuf(); // Read into the buffer the internal buffer of the file
   return data.str(); // Convert the stringstream to string and return it
}

As you can see, the C++ way of doing it is much shorter and much easier to debug (helpful exceptions with descriptions are thrown when something goes wrong)

Firefly
  • 1,488
  • 3
  • 15
  • It says that file_size is not a member of filesystem. Is there a fix? – UbuntuGuyPerson250 Feb 27 '20 at 19:25
  • file_size was added in C++17 so make sure you are using a C++17 compiler and the (not experimental/filesystem) header. Else use C++11's experiment/filesystem and std::experimental::filesystem – Firefly Feb 27 '20 at 19:28
  • I tried to use filesystem instead of experimental/filesystem but when i did it said filesystem is not a namespace – UbuntuGuyPerson250 Feb 27 '20 at 19:35
  • `std::filesystem` im guessing your missing the std part? – Firefly Feb 27 '20 at 19:36
  • Ive included that too also im using visual studio 2019 – UbuntuGuyPerson250 Feb 27 '20 at 19:38
  • So you are saying writing ``` #include \n int main() {std::filesystem::file_size("."); return 0;}``` give you an error? – Firefly Feb 27 '20 at 19:40
  • not exactly but pretty much I have included filesystem and ive used std::filesystem::file_size and filesystem throws an error – UbuntuGuyPerson250 Feb 27 '20 at 19:42
  • Try the code above, youve probably spelled `filesystem` wrong or something (also you are passing something to file_size right?) – Firefly Feb 27 '20 at 19:48
  • With VS2019 it's not in C++17 mode by default. You need to right-click your project, click "properties" and under "General" make sure that "C++ Language Standard" is set to "ISO C++17 Standard (std:c++17)" to use the `` header correctly. Damned annoying, but the default is C++14 - https://docs.microsoft.com/en-us/cpp/build/reference/std-specify-language-standard-version?view=vs-2019 – Kevin Anderson Feb 27 '20 at 19:55
  • That fixed it but im running in to an other problem on the line where it creates the buffer it throws the error incomplete type not allowed on data – UbuntuGuyPerson250 Feb 28 '20 at 00:04
  • Sounds like your missing the iostream header – Firefly Feb 28 '20 at 00:13
  • Im still having the problem – UbuntuGuyPerson250 Feb 28 '20 at 00:18
  • You need `filesystem` `string` `iostream` `fstream` and `sstream` for this to compile – Firefly Feb 28 '20 at 00:36
  • It is still giving me that error it stops when i put a zero in the square brackets but that throws a different error – UbuntuGuyPerson250 Feb 28 '20 at 01:55
  • What square brackets??? And are you sure it's not throwing the runtime error which i added to that to purposefull check the file is opened? There are no square brackets i assure you – Firefly Feb 28 '20 at 09:51
  • Sorry somehow there where square brackets now there is an unresolved external error on the line where I'm testing the code: std::string contents = read_file("Include.h"); std::cout << contents << std::endl; – UbuntuGuyPerson250 Feb 28 '20 at 14:10
  • I fixed the problem but now its saying Unhandled exception at 0x77233DB2 – UbuntuGuyPerson250 Feb 28 '20 at 18:30