Redirecting input using stdin

I am writing a short program to sort an array of integers. I am having trouble opening my input file, which is "prog1.d". The destination asked to create a symbolic link in the program directory, and after creating the object and executable file, we call the program as follows:

prog1.exe < prog1.d &> prog1.out 

I know that my bubble sorting works correctly and efficiently because I used my own txt test file.

The assignment says:

Your program gets random integers from stdin and puts them into an array, sorts the integers in the array in ascending order, and then maps the contents of the array to stdout.

How to read a file using 'cin' before EOF and add integers to my a [] array?

Here is my code:

 int main( int argc, char * argv[] ) { int a[SIZE]; for ( int i=1; i<argc; i++) { ifstream inFile; // declare stream inFile.open( argv[i] ); // open file // if file fails to open... if( inFile.fail() ) { cout << "The file has failed to open"; exit(-1); } // read int & place into array a[] for(int i=0; !inFile.eof(); i++) { inFile >> a[i]; } inFile.close(); // close file } bubbleSort(a); // call sort routine printArr(a); // call print routine return 0; } 

I know that opening a stream is the wrong way to do this, I just used it for the txt test file that I used to make sure my sorting worked. Teacher said that we should redirect the input to "cin" as if someone were typing integers on the keyboard.

Any help would be greatly appreciated.

+4
source share
4 answers

When you use redirection on the command line, argv does not contain redirection. Instead, the specified file will simply become your stdin / cin . Therefore, you do not need (and do not try) to open it explicitly - just read from standard input, as you will read from the terminal if the input is not redirected.

+5
source

Since you are writing the file to stdin, you do not have the file name in argv [1], just read stdin when the user entered a command on the console, for example, using cin :

 cin.getline (...); 
+3
source

The other answers are completely correct, but the code for claify is rewritten here:

 int main( int argc, char * argv[] ) { int a[SIZE]; int count = 0; // read int & place into array a[] //ALWAYS check the boundries of arrays for(int i=0; i<SIZE; i++) { std::cin >> a[i]; if (std::cin) count = count + 1; else break; } bubbleSort(a, count); // call sort routine printArr(a, count); // call print routine return 0; } 
+2
source

As everyone stated, use std::cin directly - you do not need to open the input file, your shell has already done this for you.

But please, please, please, do not use cin.eof() to check if you have reached the end of your input. If your input is corrupted, your program freezes. Even if your input is not corrupted, your program may (but not necessarily) start a loop in extra time.

Try using this loop:

 int a[SIZE]; int i = 0; while( std::cin >> a[i]) { ++i; } 

Or add confidence using std::vector , which will automatically grow:

 std::vector<int> a; int i; while(std::cin >> i) { a.push_back(i); } 

Or use general algorithms:

 #include <iterator> #include <algorithm> ... std::vector<int> a; std::copy(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(), std::back_inserter(a)); 
+1
source

Source: https://habr.com/ru/post/1369508/


All Articles