Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How can I use PIPE in the input file to run the test cases?

+8 votes
asked Oct 13, 2022 by Nirali Patel (750 points)

2 Answers

0 votes
answered Oct 13, 2022 by Peter Minarik (84,720 points)

TL;DR;

MyProgram < MyTestCases

Assume we have a simple program called ToUpper (or ToUpper.exe on Windows), that's implemented like this:

#include <stdio.h>
#include <ctype.h>

#define BUFFER_SIZE 512

int main()
{
    char buffer[BUFFER_SIZE];
    while (fgets(buffer, BUFFER_SIZE, stdin) != NULL)
    {
        for (int i = 0; i < BUFFER_SIZE; i++)    
            buffer[i] = toupper(buffer[i]);
        printf("%s", buffer);
    }
    
    printf("All done!\n");

    return 0;
}

In this case, if you have a text file called MyText.txt, that you would like to turn all upper case, you could do it with the following command:

ToUpper < MyText.txt

and it will print all the upper-case characters to the screen.

If you want to save it in a file, you can do it like this:

ToUpper < MyText.txt > MyUppercaseText.txt

I hope this helps.

commented Oct 15, 2022 by Nirali Patel (750 points)
I want to run all the test cases using pipe which I never done before, so I have no idea. And how can I write code of push and pop functions for processing the arithmetic expressions.
Here is the code and the input strings. please tell me where I am wrong and why the functions are not executing except the main function? Thank you.
code: https://onlinegdb.com/hcVpRmypa
input strings text file: https://onlinegdb.com/PRUrMhIHd
commented Oct 15, 2022 by Peter Minarik (84,720 points)
My comment above works if you have your executable and your input file.

If you want to do it via OnlineGDB, use the

Standard Input: ( ) Interactive Console        (*) Text

option below your code editor and fill in the TEXT part with the content of your TestCases.txt file.

Please, see how the std::stack<T> should be used. Check this site: https://cplusplus.com/reference/stack/stack/push/

Notice that std::stack::pop() returns the element.

I am not sure what you need to do, but similarly to the stack class, your own push() function should have an argument (what you push into the stack) and your own pop() function should normally return a value (what you removed from the stack), unless you have a specific logic and the items are not passed in/returned by the push()/pop() functions.
0 votes
answered Oct 16, 2022 by xDELLx (10,500 points)
----@pop-os:/tmp$ echo -e "y\nasdasd\nn" | ./a.out 
Project 2 for CS 341
Section number: 007
Semester: Fall 2022
Written by: Nirali Patel
Instructor: Marvin Nakayama, [email protected]
Do you want to enter a string?
please enter a string over alphabet:
The string read is: asdasd
Do you want to enter a string?
The string read is: asdasd
Program terminates....
The start state of PDA is: 37
----@pop-os:/tmp$        


Try the command :

echo -e "y\nasdasd\nn" | ./a.out 


It will echo 'y' followed by newline(\n) ,followed by some string ,followed by newline(\n) & will end with 'n'.
You can also create text file with the inputs that need to be fed to your program as needed

eg input.txt ,command will be

cat input.txt | ./a.out

Also I used return statemnt  instead of exit(0), not sure what the prob statement was.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...