-2

I want to make my c++ file read input from one text file and save output in another text file. This is to check my competitive programming questions.

  • 3
    Your question and description are announcing two different situations. The question asks to read some text to execute, the description asks to read some text as stdin. – Geno Chen Jun 14 '18 at 04:54

2 Answers2

0

Unclear of what you are asking. Since your question and your description seems asking two different situations.

For your question, you seems to ask how to input a command (like dir or systeminfo) from a file. You can use the function system() in the header file stdlib.h to execute the command read from that file.

For your description, you seems to ask how to input some data from a file to stdin and output to another file, you can look for command redirection, for example Redirect Windows cmd stdout and stderr to a single file.

Geno Chen
  • 4,916
  • 6
  • 21
  • 39
  • the description one is what I am asking. The nswer you shared does not work for windows. Pls help. – samarth singhal Jun 14 '18 at 05:30
  • @samarthsinghal What's your meaning of "does not work"? Any phenomenom? Does `executable < input.txt > output.txt` not work? – Geno Chen Jun 14 '18 at 05:42
  • Since your tag have 'powershell', you may refer to this https://stackoverflow.com/questions/6763086/why-is-input-redirect-not-implemented-in-powershell – Geno Chen Jun 14 '18 at 05:43
0

I think this solution works for you.

#include <iostream>
#include <fstream>
//#define cin fin
//#define cout fout
using namespace std;
int main(){
    //ofstream fout("output.txt");
    //ifstream fin("input.txt");
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    for(int i=0;i<n;i++){
        cout<<arr[i]<<endl;
    }
    return 0;
}

If you want to use as standard input output then you can use as it is. As per your problem you can use it by only removing two slashes(comments). In the above program "input.txt" is file name for taking input an "output.txt" is the file in which output will be stored.

Note: You only have to provide "input.txt" the program will automatically generate "output.txt".

You can rename "input.txt" and "output.txt" as per your requirement.

If you are not satisfied with the above solution then you can use a program named hightail. Hightail is an automatic tester for programming contest. To know more about hightail and its working click here.

Honey Yadav
  • 176
  • 1
  • 12