简体   繁体   中英

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

I have troubles with my C++ program. 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. I verified if it outputs (in the file) a simple message "Hello World" but the output file was empty.

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.

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. I also changed the output from <fscanf> to <stdio.h>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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