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

时间:2017-01-13 12:16:39

标签: c++ c

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

var doc= new Document();
var vLineHeight=5;
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
   shape.Font.Position = -(shape.Height / 2 - vLineHeight);
}

我尝试了printf和cout.File,但是没有写入文件。我究竟做错了什么?有没有其他方法将这些值写入文件? 注意:我在Win 7上使用VS 2010 感谢。

6 个答案:

答案 0 :(得分:4)

按照循环中的代码:

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);
}

答案 1 :(得分:2)

根据您的实施方式,重定向标准流可能会起作用或失败。 C标准声明stdin,stdout和stderr是

  

是“FILE指针”类型的表达式,指向FILE对象   分别与标准错误,输入和输出流相关联。

即使它有效,它也是不可移植的,并且可能会因下一版本的编译器而失败。

正确的方法是在C流(C)或fprintf引用(C ++)上使用std::ostream并为其分配一个众所周知的流(在C或std :: cout中的stdout)在C ++中)如果你想使用标准流。

答案 2 :(得分:1)

您的流量控制是否正确?我建议

ascii = input[x];
freopen("D:\\Testfiles\\Output1.txt","w",stdout);
bin(ascii);

因此,在文件重定向后将调用printf个函数。 希望这会有所帮助。

答案 3 :(得分:1)

我在大学第二学期上课。我注意到我在课堂上学到的东西与我在课堂上看到的标准不同。

话虽如此,在C ++中,我相信您需要使用文件流库来写入文件:

#include <fstream>

ofstream fout("example.txt");

使用fout,或您为流选择的任何名称     对象,就像你使用cout

一样
fout << (n && i ? "1" : "0");

我没有使用C的经验,但在C ++中,你的三元条件都应该在括号内。

快乐的编码!

答案 4 :(得分:0)

首先,正如 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);
}

答案 5 :(得分:0)

以下代码干净地编译,执行所需的功能,并正确检查错误。

#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

请注意输出缺乏可读性。建议在每个字节之间输出一个空格,最后输出一个&#39; \ n&#39;

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

为了便于阅读和理解:

  1. 始终缩进代码
  2. 在每个开口支撑后缩进&#39; {&#39;
  3. 在每个闭幕式之前取消注意&#39;}&#39;
  4. 建议每个缩进级别使用4个空格,因为即使使用可变宽度字体也可以看到这个空格,并且仍然留有多个缩进级别的空间。建议不要使用制表符作为每个文字处理器缩进,编辑器具有为个人喜好设置的制表位/制表符宽度
  5. 建议使用适当的水平间距以提高可读性,例如逗号之后,内部等等