0

A few days ago, I learned about stress testing and wanted to ask how to make two different .txt files.

Like one for Output and one for my failed test case.

It would work like this:

output: it would have all the output, correct solution, and wrong solution

failedtestcase.txt: it would have only those test cases detail that have failed and which inputs were given for the particular failed test case.

`

int main() {
    freopen("output.txt", "w", stdout);
    //create a freopen for Failed test case

    srand(time(NULL));
    int t = rand() % 10;
    cout<< "======================================" << endl;
    cout << "            Test Case: "<< t << "           "<<  endl;
    cout<< "======================================" << endl;
  
    while(t--){
        int n = rand() % 11;
        cout << "Number of Element: " <<  n << endl;
        int d = rand() % n;
        cout << "Kth: " << d << endl;
        cout << "Araray: " << endl;
        int arr[n];
        for(int i=0; i< n;i++){
            arr[i] = rand() % 11;
            cout<< arr[i] << " ";
        }
        sort(arr, arr+n);
        cout << endl;

        KthSAndL(arr, n, d);

        freopen("FailedTestcase.txt", "w", stdout);

        pair<int, int> MySolution = KthSAndL(arr, n, d);
        pair<int, int> ActualSolution = kthSmallestLargest(arr, n, d);

        if(MySolution.first != ActualSolution.first && MySolution.second != ActualSolution.second){
            cout << "Wrong Answer" << endl;
            cout << "Your Answer: " << MySolution.first << " " << MySolution.second << endl;
            cout << "Actual Answer: " << ActualSolution.first << " " << ActualSolution.second << endl;
        }

        cout << endl;
        cout<< "======================================" << endl;
    }

    return 0;
}

`

This is the code and i'm getting this Kind of output

enter image description here

I was trying to have my all output in output.txt and only failed test case inputs will be shows in failedtestcase.txt

What I tried:

  1. Moving the Lines up down before cout'ing (Might work I thought)

and well actully I have no idea what I can do

UUC110
  • 1
  • 4
  • 2
    You are redirecting the stdout stream to different files... `freopen("output.txt", "w", stdout);` will write stdout to "output.txt" and then `freopen("FailedTestcase.txt", "w", stdout);` will cause it to write to FailedTestcase.txt... what exactly do you expect to happen? – Andro Nov 03 '22 at 07:50
  • [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai Nov 03 '22 at 08:04
  • 1
    use a `ofstream` to write to a file. Search for "How to write to a file? C++" – 463035818_is_not_an_ai Nov 03 '22 at 08:05
  • or if you want to stay without your approach you could redirect stderr – 463035818_is_not_an_ai Nov 03 '22 at 08:06
  • @Andro I'm trying to have different content in my both txt file. Like if you see my code, I'm outputting t, d, n , and array, and also the wrong array if my output doesn't match. but my code is not doing that. I want my failed cases with there respective inputs which caused wrong answer – UUC110 Nov 03 '22 at 08:09
  • @463035818_is_not_a_number Can you explain bit more – UUC110 Nov 03 '22 at 09:59
  • Imagine two tubes, that are joined at the end into a single tube and there is a switch, that determines, to which tube the water will flow. Whatever you write to the `stdout` is the water. Calls to the `freopen` just flips the switch. In the beginning, the water goes to the 'output.txt' tube and then it goes to the 'FailedTestCase.txt' tube... – Andro Nov 03 '22 at 10:04
  • 1
    frankly its a little odd that you use `freopen` which can be considered the advanced way to write to a file in C++. The easy beginner friednly way is what you can find all over in tuturials and books. DId you learn C++ from coding challenges? (that would explain `freopen` and the lack of `ofstream`) Don't do that, try here: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 463035818_is_not_an_ai Nov 03 '22 at 10:06
  • @463035818_is_not_a_number yea i learned it through coding challanges – UUC110 Nov 05 '22 at 10:05

0 Answers0