无法将控制台输出写入C或C++中的文件

Unable to write console output to file in C or C++

本文关键字:C++ 文件 控制台 输出      更新时间:2023-10-16

我正在尝试将控制台输出写入该文件。文件已成功创建,但没有任何内容写入该文件。下面是我的代码。

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">

发布的代码和我的回答并没有解决代码格式(缩进)缺乏一致性的问题。

为了便于阅读和理解:

  1. 始终缩进代码
  2. 在每个大括号"{"后缩进
  3. 每个右大括号"}"前都没有凹痕
  4. 建议每个缩进级别使用4个空格,因为即使使用可变宽度的字体,这也是可见的,并且仍然为多个缩进级别留出空间。建议不要像每个文字处理程序那样使用制表符进行缩进,编辑器根据个人喜好设置制表位/制表宽度
  5. 建议使用适当的水平间距以提高可读性,例如逗号后、括号内等