Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6390391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:32:35+00:00 2026-05-25T03:32:35+00:00

I am writing a short program to sort an array of integers. I am

  • 0

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 assignment has asked to create a symbolic link in the programs directory, and I after creating the object & executable, we invoke the program as follows…

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

I know my bubble sort works correctly & efficiently because I have used my own test ‘txt’ file.

The assignment says:

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

How do I read the file using ‘cin’ until EOF & add the integers to my array a[] ?

Here is my code so far:

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's & 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 was using it for a test ‘txt’ file I was using to make sure my sorting worked. The teacher said we should redirect the input to ‘cin’ like someone was entering integers on a keyboard.

Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:32:36+00:00Added an answer on May 25, 2026 at 3:32 am

    When you’re using redirection on the command line, argv does not contain the redirection. Instead, the specified file simply becomes your stdin/cin. So you don’t need to (and shouldn’t try to) open it explicitly — just read from the standard input, as you would read from the terminal when input isn’t redirected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program which generates its own Wireshark pcap-file (sort of a network
I am currently writing a program which takes user input and creates rows of
I am writing a program to sort a list fo input strings (song names).
I am writing a grading program for an assignment in which students are implementing
So, I am writing some sort of a statistics program (actually I am redesigning
I'm writing code in python for some sort of daemon that has to execute
I am interested in writing a Java program which does the following. Attach to
I'm currently writing a program that needs to compare each file in an ArrayList
I'm writing a program that among other things needs to download a file given
I am writing a small test program that gives the following xml file as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.