jjzjj

c++ - 无法使用 __gnu_cxx::stdio_filebuf 进行流式传输

这会创建文件,但不会写入任何内容。std::ofstreamoutstream;FILE*outfile;outfile=fopen("/usr7/cs/test_file.txt","w");__gnu_cxx::stdio_filebuffilebuf(outfile,std::ios::out);outstream.std::ios::rdbuf(&filebuf);outstream我知道还有其他简单的解决方案可以实现输出,但我需要在编辑时使用这个非标准的filebuf来锁定文件,这样其他进程就无法打开文件。我不知道为什么这不起作用。 最佳答案

c++ - 在C/C++中使用stdio文件操作时如何检测磁盘空间不足?

我正在做一个小程序如下:voidreserve_file_space(char*file_path,size_tamount){FILE*fp=fopen(file_path,"w+b");if(!fp){printf("couldnotcreateanewfile\n");return;}intfseek_ret=fseek(fp,amount,SEEK_SET);if(fseek_ret!=0){printf("couldnotseektothedesiredposition\n");fclose(fp);return;}chargarbage=1;size_tret=fwrite

c++ io streams sync_with_stdio 没有区别

出于某种原因,我无法使我的输出流使用该行运行得更快std::ios_base::sync_with_stdio(false);包含在我程序的开头。我正在用这两个程序对此进行测试:#includeintmain(){for(inti=0;i和#includeintmain(){std::ios_base::sync_with_stdio(false);for(inti=0;i每个程序的运行时间如下第一次测试(同步)real0m1.095suser0m0.472ssys0m0.299ssecond_test(关闭同步)real0m1.091suser0m0.471ssys0m0.299s我

c++ - 为什么 std::ios_base::sync_with_stdio 没有在 libc++ (clang) 中实现?

让我们看一下这个代码示例:#includeintmain(){std::ios_base::sync_with_stdio(false);intn;std::cin>>n;for(inti=0;i>buf;}}此代码示例对这样的输入的性能:1000000001...9999999在我的机器上:g++-5-O2-std=c++11:./a.outclang-700.0.72-O2-std=c++11:./a.out经过一些分析后,我发现libc++根本没有禁用同步。然后我查看了他们的代码,发现了这个:https://github.com/llvm-mirror/libcxx/blob/6

c++ - mingw-w64 : slow sprintf in <cstdio>

是吗C++中的header包含与相同的功能但输入std命名空间?我在使用mingw-w64编译的程序中遇到了奇怪的效率问题,它比在linux上慢十倍以上。经过一些测试,我发现问题出在sprintf中。.然后我做了如下测试:#include//#include//usingstd::sprintf;intmain(){inti;for(i=0;i使用编译时它比使用快15倍.这是时间:$time./stdioreal0m0.557suser0m0.046ssys0m0.046s$time./cstdioreal0m7.465suser0m0.031ssys0m0.077s$g++--ver

c++ - 为什么 fscanf() 从不返回 EOF?

在下面的代码片段中,fscanf()总是返回0,而不是-1(EOF)。结果,while循环永远不会退出。知道为什么吗?while((n_items_read=fscanf(ifd,"%la,%la,%la\n",&x,&y,&z))!=EOF){...}完整代码如下:voidparse_test_file(constchar*filename){doublex,y,z;intn_items_read,n_lines_read;FILE*ifd=fopen(filename,"r");if(NULL==ifd){fprintf(stderr,"Failedtoopen%sforreadi

c++ - 我可以跨 DLL 边界传递 FILE 对象吗?

我有一个C++框架,其中一些计算被委托(delegate)给(有时是自动生成的)C函数或具有外部“C”链接的C++函数。这些是低级例程,必须以极快的速度和最小的开销进行评估,它们通常驻留在单独的共享对象/DLL中。他们目前的签名是这样的:intmy_generated_function(constdouble*input,double*output,double*work);它将驻留在共享库中,在POSIX上使用dlopen或在Windows上使用LoadLibrary加载。在POSIX上使用dlsym(handle,"my_generated_function")或在Windows上

c++ - 关于linux中的fork系统调用

好的,我正在linux中使用以下C/C++代码:intmain(){printf("hello");Pid=fork();if(pid>0)printf("I’mtheparent!");elseprintf("I’mthechild");return0;}这是我的输出:我的CS教授的笔记如下:Afteranewchildprocessiscreated,bothprocesseswillexecutethenextinstructionfollowingthefork()systemcall.PleasenotethatUnixwillmakeanexactcopyofthepare

c++ - 为什么在 C++ 中使用 <cstdio> 而不是 <stdio.h> 时 "std::printf"和 "printf"都会编译?

这个问题在这里已经有了答案:WhenusingCheadersinC++,shouldweusefunctionsfromstd::ortheglobalnamespace?(8个答案)关闭5年前。据我所知,cxyz格式的header与xyz.h相同,唯一的区别是cxyz将所有命名空间std下的xyz.h的内容。为什么以下程序在GCC4.9和clang6.0上都能编译?#includeintmain(){printf("Testing...");return0;}和第二个程序:#includeintmain(){std::printf("Testing...");return0;}FI

c++ - 使用新的控制台窗口创建进程,但覆盖一些标准的 i/o 句柄

如果您使用带有标志CREATE_NEW_CONSOLE的CreateProcess,新进程会将其标准输入、输出和错误句柄定向到新的控制台窗口。如果您想覆盖I/O流,您可以通过在STARTUPINFO字段hStdOutput、hStdInput和hStdError中设置句柄并设置标志STARTF_USESTDHANDLES来实现。但是如果您只想覆盖其中一个句柄怎么办?例如,我可能想将stderr重定向到一个文件,同时让stdout和stdin连接到新的控制台窗口。STARTF_USESTDHANDLES标志告诉CreateProcess替换所有句柄,而不是将它们连接到新控制台窗口的句柄。