无法将输出写入cpp程序中的文本文件
Cannot write the output to a text file in cpp program
我有这个输入文件"https://code.google.com/codejam/contest/351101/dashboard/do/A-large-practice.in?cmd=GetInputFile&问题=374101&input_id=1&filename=A-大练习.in&redownload_last=1&agent=网站&csrfmiddlewaretoken=OWMxNTVmMTUyODBiYjhhN2Q2OTM3ZGJiMTNhNDkwMDF8fDEzNzIxNzI1NTE3ODAzMjA%3D"我试着读取这个文件:-使用freopen("filename.txt",r,stdin);然后我想把写的输出写到另一个文本文件中,我可以在这个代码堵塞练习题中上传给法官。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int t,k=0,a[2000];
freopen("ab.txt","r",stdin);
scanf("%d",&t);
while(t--)
{
freopen("cb.txt","w",stdout);
int c;
scanf("%d",&c);
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Case #%d: ",++k);
for(int i=0;i<n-1;i++)
{for(int j=i+1;j<n;j++)
if((a[i]+a[j])==c)
{printf("%d %dn",i+1,j+1);
i=n;}
}
}
return 0;
}
这是我的密码。现在的问题是输出文件cb.txt只包含输入的最后一行。我希望将整个输出写入cb.txt,那么我该怎么办。
您可能将控制台输入(scanf
)与文件输入(fscanf
)混淆。
文件I/O的C样式函数有一个f前缀
参见:fprintf, fscanf, fgets, and fputs
。
我弄错了,我放了freopen("cb.txt","w",stdout);在循环中,所以每次输出都被覆盖,现在我把这行放在循环外,它就可以工作了
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- C++检查应用程序中的文本框值
- 在程序和基于文本的游戏和C++内重新启动程序
- 在程序/基于文本的游戏/C++内重新启动程序
- 我的C++程序只写入文本文件最后一个条目
- 如何在Windows窗体应用程序中打开txt文件并将行导入文本框
- C++程序的 Python 文本解析器?
- 在图像上覆盖文本的程序会产生无休止的字符串错误
- 创建程序以从给定的.txt文件中查找文本,替换并计算单词
- opengl 飞行模拟器应用程序中的文本呈现问题
- 在C Windows程序中显示文本文件内容
- C ++,否则文本程序不会进入我想要的途径
- 编写一个 C++ 程序以将文本文件转换为 HTML 文件
- C++可以从文本文件复制并粘贴到打开的Word程序中
- C :如何从格式的文本文件中读取许多数据到程序中
- C++ 从另一个程序窗口的内存中编辑文本
- 崇高文本 3 - 在终端中编译并运行 C++ 程序(路径包含空格)
- 如何在MFC多字节应用程序中显示西里尔文本?
- 将文本添加到另一个程序文本框 C++