使用 stdin 重定向输入

发布于 2024-12-02 02:35:31 字数 1185 浏览 2 评论 0原文

我正在编写一个简短的程序来对整数数组进行排序。我在打开输入文件“prog1.d”时遇到问题。作业要求在程序目录中创建一个符号链接,我在创建对象后创建了一个符号链接。可执行文件,我们按如下方式调用该程序...

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

我知道我的冒泡排序工作正常&高效,因为我使用了自己的测试“txt”文件。

作业说:

您的程序从 stdin 获取随机整数并将它们放入数组中,按升序对数组中的整数进行排序,然后在 stdout 上显示数组的内容。

如何使用“cin”读取文件直到 EOF &将整数添加到我的数组 a[] 中?

到目前为止,这是我的代码:

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;
}

我知道打开流是执行此操作的错误方法,我只是将它用于测试“txt”文件,我用它来确保我的排序有效。老师说我们应该将输入重定向到“cin”,就像有人在键盘上输入整数一样。

任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花落人断肠 2024-12-09 02:35:31

当您在命令行上使用重定向时,argv 不包含重定向。相反,指定的文件只是成为您的 stdin/cin。因此,您不需要(也不应该尝试)显式打开它 - 只需从标准输入读取,就像输入未重定向时从终端读取一样。

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.

悲欢浪云 2024-12-09 02:35:31

由于您在标准输入上传输文件,因此 argv[1] 上没有文件名,只需在用户在控制台键入时读取标准输入,例如使用 cin :

cin.getline (...);

Since you are piping the file on the stdin, you don't have the file name on argv[1], just read the stdin as the user was typing at the console, for example using cin:

cin.getline (...);
相权↑美人 2024-12-09 02:35:31

其他答案是完全正确的,但这里是需要澄清的重写代码:

int main( int argc, char * argv[] )
{
    int a[SIZE];
    int count = 0;

    // read int's & 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;
}

The other answers are completely correct, but here's the rewritten code to claify:

int main( int argc, char * argv[] )
{
    int a[SIZE];
    int count = 0;

    // read int's & 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;
}
尾戒 2024-12-09 02:35:31

正如大家所说,直接使用 std::cin —— 您不需要打开输入文件,您的 shell 已经为您完成了该操作。

但是,请不要使用 cin.eof() 来测试是否已到达输入末尾。如果您的输入有缺陷,您的程序将挂起。即使您的输入没有缺陷,您的程序也可能(但不一定)额外运行一次循环。

尝试这个循环:

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

或者,通过使用会自动增长的 std::vector 来增加鲁棒性:

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

或者,使用通用算法:

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

As everyone has stated, use std::cin directly -- you don't need to open the input file, your shell has already done that for you.

But, please, please, please, don't use cin.eof() to test to see if you have reached the end of your input. If your input is flawed, your program will hang. Even if your input isn't flawed, your program may (but won't necessarily) run the loop one extra time.

Try this loop instead:

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

Or, add robustness by using std::vector which will automatically grow:

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

Or, use generic 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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文