无法将控制台输出写入C或C++中的文件
Unable to write console output to file in C or C++
我正在尝试将控制台输出写入该文件。文件已成功创建,但没有任何内容写入该文件。下面是我的代码。
void AtoB(char * input)
{
unsigned int ascii; //used to store ASCII number of a character
int length = strlen(input);
for(int x=0;x<length;x++) //repeat until the input is read
{
ascii = input[x];
bin(ascii);
freopen("D:\Testfiles\Output1.txt","w",stdout);
}
}
void bin(unsigned n)
{
unsigned i;
for (i = 1 << 7; i > 0; i = i / 2)
{
//(n & i)? cout<<"1":cout<<"0";//printf("1"): printf("0");
(n & i)?printf("1"): printf("0");
}
}
我试过printf和cout。文件已创建,但没有任何内容写入该文件。我做错了什么?有没有其他方法可以将这些值写入文件?注意:我在Win 7上使用VS 2010谢谢
遵循循环中的代码:
for(int x=0;x<length;x++)
{
// Retrieve a value
ascii = input[x];
// Write it to stdout
bin(ascii);
// Open the file, truncate it, and redirect stdout to it.
freopen("D:\Testfiles\Output1.txt","w",stdout);
}
由于您所做的最后一件事是打开文件并截断它,因此只剩下一个空文件。
在循环之前移动freopen
-您只需要做一次:
freopen("D:\Testfiles\Output1.txt","w",stdout);
for(int x=0;x<length;x++) //repeat until the input is read
{
ascii = input[x];
bin(ascii);
}
根据您的实现,以这种方式重定向标准流可能有效,也可能失败。C标准声明stdin、stdout和stderr是宏
是指向FILE对象的"指向FILE"类型的表达式分别与标准误差流、输入流和输出流相关联。
即使它有效,它也将是不可移植的,并且可能在下一版本的编译器中失败。
正确的方法是在C流(C)或std::ostream
引用(C++)上使用fprintf
,如果您想使用标准流,则将其分配给一个众所周知的流(C中的stdout或C++中的std::cout)。
您的流量控制是否正确?我建议
ascii = input[x];
freopen("D:\Testfiles\Output1.txt","w",stdout);
bin(ascii);
因此,printf
函数将在文件重定向后调用。希望这会有所帮助。
我正在上大学第二学期的课。我注意到我在课堂上学到的东西与我在这节课上看到的标准不同。
话虽如此,在C++中,我认为您需要使用文件流库来写入文件:
#include <fstream>
ofstream fout("example.txt");
使用fout,或您为流选择的任何名称对象,就像您使用cout 一样
fout << (n && i ? "1" : "0");
我没有使用C的经验,但在C++中,三元条件应该都在括号内。
编码快乐!
首先,如molbdnilo所述,在循环之前移动freopen
-您只需要执行一次,然后使用fprintf
在文件上打印。
相反,您也可以使用fopen
来编辑或写入文件。类似:
FILE *output;
output = fopen("D:\Testfiles\Output1.txt","w",stdout);
for(int x=0;x<length;x++) //repeat until the input is read
{
ascii = input[x];
bin(ascii);
fprintf(output, ascii);
}
以下代码编译干净,执行所需的函数,并正确检查错误。
#include <stdio.h> // freopen(), fprintf()
#include <stdlib.h> // exit(), EXIT_FAILURE
#include <string.h> // strlen()
void AtoB(char * input);
void bin(unsigned n);
int main( void )
{
char text[] = "abcd";
AtoB( text );
}
void AtoB(char * input)
{
unsigned int ascii; //used to store ASCII number of a character
size_t length = strlen(input);
//freopen("D:\Testfiles\Output1.txt","w",stdout);
if( NULL == freopen( "output1.txt", "w", stdout ) )
{
perror( "freopen failed" );
exit( EXIT_FAILURE );
}
for(size_t x=0; x<length; x++) //repeat until the input is read
{
ascii = (unsigned int)input[x];
bin(ascii);
}
}
void bin(unsigned n)
{
unsigned i;
for (i = 1 << 7; i > 0; i = i / 2)
{
(n & i)?printf("1"): printf("0");
}
}
结果输出为:
01100001011000100110001101100100
请注意输出缺乏可读性。建议在每个字节之间输出一个空格,并在末尾输出一个"\n">
发布的代码和我的回答并没有解决代码格式(缩进)缺乏一致性的问题。
为了便于阅读和理解:
- 始终缩进代码
- 在每个大括号"{"后缩进
- 每个右大括号"}"前都没有凹痕
- 建议每个缩进级别使用4个空格,因为即使使用可变宽度的字体,这也是可见的,并且仍然为多个缩进级别留出空间。建议不要像每个文字处理程序那样使用制表符进行缩进,编辑器根据个人喜好设置制表位/制表宽度
- 建议使用适当的水平间距以提高可读性,例如逗号后、括号内等
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 如何将内容数组写入文本文件?
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到