简体   繁体   中英

write to a txt file through a dll

I want to write to a text file through a dll, it compiles fine but it doesn't output the file with following code. When I compile the project to a.exe and run it, it can creat the file and write to the file.

What do I need to do to write from the dll?? I want to output some data inside the dll for debugging purpose, instead of return these intermediate data from the dll. I'm calling the dll from python/

credit.cpp

#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;

double*  _stdcall credit(double* in_array, double a, double b)
{

    ofstream myfile;
    myfile.open("example.txt");
    myfile << "Hi!\n";
    myfile.close();

    return in_array;
}

credit.def

LIBRARY "pass"
;DESCRIPTION 'call dll from python'
EXPORTS
    credit

credit.h

#pragma once
double* __stdcall credit(double a, double b, double *in_array);

From a DLL try using an absoulute path "C:\\Temp\\example.txt", make sure the folder is created first that should fix your problem.

I use the following bellow to write simple timing logs from a dll.

void Log(const std::string& text) {
    std::ofstream log("C:\\logs\\performance.log", std::ofstream::app | std::ofstream::out);
    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
        std::chrono::system_clock::now().time_since_epoch()).count();
    auto p = std::chrono::system_clock::now();
    auto t = std::chrono::system_clock::to_time_t(p);
    log << toString(text) << ", Since Epoch: " << toString(ms) << " time: " << toString(std::ctime(&t)) << std::endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM