2

The Method readFile should read all chars of the file into a string and return it. After opening it, the open status will be printed to the console. When i am compiling a Debug-Build, the returned string matches the content of the file. But when i am compiling a Release-Build, the line with getline throws an Debug Assertion fail and shows a popup with the following text:

Debug Assertion Failed!

File: minkernel\crts\ucrt\src\appcrt\lowio\read.cpp

Expression: _osfile(fh) & FOPEN

CODE:

#include <string>
#include <iostream>
#include <fstream>

using namespace std;

string readFile(const string& fileName) {
    cout << '\'' << fileName << '\'' << endl;
    ifstream file{ fileName };

    cout << file.is_open() << endl;

    string str;
    string content;
    cout << "START" << endl;

    while (getline(file, str)) {
        cout << "READ" << endl;
        content += str;
        content.push_back('\n');
    }

    file.close();

    return content;
}
Community
  • 1
  • 1
Ulrich Stark
  • 401
  • 3
  • 12

1 Answers1

0

I just ran into the same problem, and I found a solution here: Dan Nissenbaum's answer

What I did was just changing the runtime library linking mode to /MTd. And you can change it in Project Settings -> Configuration Properties -> C/C++ -> Code generation -> runtime library

folkboat
  • 167
  • 1
  • 7