Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将其打印到输出文件?(c+;+;) #包括 #包括 #包括 使用名称空间std; int main(){ bool done=false; cout n; 对于(长i=1;i_C++_Fstream - Fatal编程技术网

C++ 如何将其打印到输出文件?(c+;+;) #包括 #包括 #包括 使用名称空间std; int main(){ bool done=false; cout n; 对于(长i=1;i

C++ 如何将其打印到输出文件?(c+;+;) #包括 #包括 #包括 使用名称空间std; int main(){ bool done=false; cout n; 对于(长i=1;i,c++,fstream,C++,Fstream,它不会写入文件,因为您将outfile定义为std::ifstream ifstream用于输入文件。 将它定义为流的,它应该可以工作 例如: 流出管的(“out.txt”); 您应该定义“ofstream”输出文件(“out.txt”);而不是“ifstream”输出文件(“out.txt”);您需要查看 #include <iostream> #include <iomanip> #include <fstream> using namespace st

它不会写入文件,因为您将
outfile
定义为
std::ifstream
ifstream
用于输入文件。 将它定义为流的
,它应该可以工作

例如:

流出管的
(“out.txt”);
您应该定义“ofstream”输出文件(“out.txt”);而不是“ifstream”输出文件(“out.txt”);您需要查看
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main() {
   bool done = false;
   cout << setprecision(20) << endl;
    ifstream infile("in.txt");
    ifstream outfile("out.txt");
    while(!infile.eof()) {
           int sign = 1;
           double pi = 0;
           long n;
           infile >> n;

           for(long i = 1; i < n; i += 2) {
               pi += sign/(double)i;
               sign = -sign;
           }
           pi *= 4;
           cout << "value of pi for n = " << n << " is " << pi << endl;

       }


   return 0;
}