所以即时编写c ++程序,从输入文件中取一个整数,乘以2并输出到输出文件.所以代码是 -
#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
int n;
FILE * inFile;
FILE * outFile;
inFile = fopen ("reiz.in","r");
outFile = fopen ("reiz.out","r+");
fscanf (inFile, "%s", n);
int m = n * 2;
fprintf (outFile, "%n", n);
fclose (inFile);
fclose (outFile);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但有些事情是错的.在reiz.in文件中有2号,运行程序后它应该在reiz.out中输出4,但它只显示不发送错误.我的剧本究竟出了什么问题?最好的问候,Y2oK
编辑1:好的,现在看起来像这样 -
#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
int n;
FILE * inFile;
FILE * outFile;
inFile = fopen ("reiz.in","r");
outFile = fopen ("reiz.out","r+");
fscanf (inFile, "%d", &n);
int m = n * 2;
fprintf (outFile, "%d", m);
fclose (inFile);
fclose (outFile);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是在运行reiz.exe文件时它仍然给出了相同的不发送错误,并且它没有在输出文件上写任何东西我现在有点困惑,并且不知道选择谁作为最佳答案,所以我将选择最"+1"的人.但是,谢谢大家!
这是一个C程序(除了using namespace std;).在C++中,您应该使用流和格式化的I/O,如下所示:
#include <fstream>
int main() {
std::ifstream input_file("reiz.in");
int n;
input_file >> n; // read one integer
std::ostream output_file("reis.out");
output_file << n * 2 << std::endl; // calculate n * 2 and write the result
// to a file. std::endl adds a newline and
// flushes the buffer
return 0;
}
Run Code Online (Sandbox Code Playgroud)