简体   繁体   English

C / C ++和Fortran写入同一文件

[英]C/C++ and Fortran writing to same file

I have a C/C++ library whose functions are called inside a Fortran program. 我有一个C / C ++库,其功能在Fortran程序中被调用。 I would like to write some output generated in my library on the same file the Fortran program outputs to. 我想将库中生成的一些输出写在Fortran程序输出到的同一文件上。 I tried to pass the filename, open it in C++, write to it and finally close it with this sample code: 我试图传递文件名,在C ++中将其打开,写入文件,最后使用以下示例代码将其关闭:

std::ofstream output;
output.open(name, ofstream::out | ofstream::app);

/*
   Some calculations...
 */    

output << "Result is " << result << std::endl;

output.close();

Nothing is written to file, unless I remove ofstream::app but then most part of what is written by the Fortran code is destroyed... I also tried using fprintf with a similar sample code: 除非我删除ofstream :: app,否则什么都不会写入文件,但是Fortran代码编写的大部分内容都被破坏了...我也尝试使用fprintf和类似的示例代码:

FILE * pFile = fopen(name, "a");

 /*
   Some calculations...
 */    

fprintf(pFile, "Result is = %.10E", result);

fclose(pFile);

with the same results. 具有相同的结果。 Any clue as to how to do this? 关于如何执行此操作的任何线索?

即使您找到了针对一组运行时库执行此操作的方法,也可能因另一组(例如,不同的编译器集合)或另一个操作系统而失败...请不要这样做,请将您要写入的数据传递给最初打开文件的系统的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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