简体   繁体   English

为什么我的 C++ 程序没有输出到文件?

[英]Why doesn't my C++ program is output to a file?

I have troubles with my C++ program.我的 C++ 程序有问题。 It doesn't output in a file.它不会在文件中输出。 I tried to change the file output with cout and the program is working, but I need it to output in the file.我试图用cout更改文件输出并且程序正在运行,但我需要它在文件中输出。 I verified if it outputs (in the file) a simple message "Hello World" but the output file was empty.我验证了它是否输出(在文件中)一条简单的消息“Hello World”,但输出文件为空。

Here's the code:这是代码:

#include<fstream>
#include<string.h>
using namespace std;
ifstream fi("alfabetar.in");
ofstream fo("alfabetar.out");
int n,i,j,x,maxim;
char A[101][201];
int main()
{
  fi>>n;
  for(i=0; i<n; i++)
  {
    fi>>A[i];
    x=strlen(A[i]);
    if(x>maxim)
      maxim=x;
  }
  for(i=maxim-1; i>=0; i--)
  {
    for(j=0; j<n; j++)
    {
      fo<<A[j][i];
    }
    fo<<"\n";
  }
  fi.close();
  fo.close();
  return 0;    
}

if you want to output in a text file you should modify如果你想在文本文件中输出你应该修改

ifstream fi("alfabetar.in.txt");
ofstream fo("alfabetar.out.txt");

That will get and put your information into a text file(for example notepad) also make sure the files you get your input and put your output are in the same director with the program.If not put the absolute path as mmahdich said.这将获取并将您的信息放入文本文件(例如记事本)中,并确保您获得输入和输出的文件与程序位于同一目录中。如果没有按照 mmahdich 所说的那样放置绝对路径。

I found out the problem, when I rotated the matrix there were some blank spaces (0s), and because of them the program wasn't working.我发现了问题,当我旋转矩阵时有一些空格(0),因此程序无法运行。 I also changed the output from <fscanf> to <stdio.h>我还将输出从<fscanf>更改为<stdio.h>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM