[英]Issue with file redirection and coding in c++ using ubuntu linux
I am trying to complete an assignment where I create a numbers.txt file that has one line with six numbers without any addition or subtraction. 我正在尝试完成一个分配,在其中创建一个numbers.txt文件,该文件具有一行包含六个数字的行,而没有任何加法或减法。 I have to write a program(sum.cpp) that adds the numbers using cin as an input. 我必须编写一个使用cin作为输入来添加数字的程序(sum.cpp)。 I have instructions to go to linux and type in $./sum < numbers.txt and it should print sum. 我有使用Linux的说明,并输入$。/ sum <numbers.txt,它应该显示sum。
I tried doing that and multiple variations of the code and got a blank screen. 我尝试执行该操作以及代码的多个变体,但出现了空白屏幕。 When I did it in another compiler I got 32764 which is way off from the sum. 当我在另一个编译器中执行此操作时,我得到了32764,与总和相去甚远。
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
int input= 0;
while(cin>>input)
{
sum+=input;
}
cout << sum << endl;
return 0;
}
Expected Results : 预期成绩 :
If you have a file numbers.txt that contains: 如果您有一个包含以下内容的文件numbers.txt:
10 15 16 -7 102 345
then if you redirect it into the program, it should report: 然后,如果将其重定向到程序中,它将报告:
$ ./sum < numbers.txt
481
将sum.cpp
和numbers.txt
放在同一目录中后,请尝试运行以下命令
g++ sum.cpp -o sum.out
chmod +x ./sum.out
./sum.out < numbers.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.