Mac m1环境使用VScode,用文件重定向写输入输出
遇到的现象:读取文件内容与写入文件内容一样
booktest.cpp文件内容
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item total; // variable to hold data for the next transaction
// read the first transaction and ensure that there are data to process
if (std::cin >> total) {
Sales_item trans; // variable to hold the running sum
// read and process the remaining transactions
while (std::cin >> trans) {
// if we're still processing the same book
if (total.isbn() == trans.isbn())
total += trans; // update the running total
else {
// print results for the previous book
std::cout << total << std::endl;
total = trans; // total now refers to the next book
}
}
std::cout << total << std::endl; // print the last transaction
} else {
// no input! warn the user
std::cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
return 0;
}
book_sales文件内容
0-201-70353-X 4 24.99
0-201-82470-1 4 45.39
0-201-88954-4 2 15.00
0-201-88954-4 5 12.00
0-201-88954-4 7 12.00
0-201-88954-4 2 12.00
0-399-82477-1 2 45.39
0-399-82477-1 3 45.39
0-201-78345-X 3 20.00
0-201-78345-X 2 25.00
运行结果:
(base) yuwenshuo@Cosmos-Computer Cubic1 % $booktest <book_sales >outfile
(base) yuwenshuo@Cosmos-Computer Cubic1 % cat book_sales
0-201-70353-X 4 24.99
0-201-82470-1 4 45.39
0-201-88954-4 2 15.00
0-201-88954-4 5 12.00
0-201-88954-4 7 12.00
0-201-88954-4 2 12.00
0-399-82477-1 2 45.39
0-399-82477-1 3 45.39
0-201-78345-X 3 20.00
0-201-78345-X 2 25.00
(base) yuwenshuo@Cosmos-Computer Cubic1 % cat outfile
0-201-70353-X 4 24.99
0-201-82470-1 4 45.39
0-201-88954-4 2 15.00
0-201-88954-4 5 12.00
0-201-88954-4 7 12.00
0-201-88954-4 2 12.00
0-399-82477-1 2 45.39
0-399-82477-1 3 45.39
0-201-78345-X 3 20.00
0-201-78345-X 2 25.00
如果直接在终端手动输入信息,可以正常运行输出结果
(base) yuwenshuo@Cosmos-Computer Cubic1 % cd "/Users/yuwenshuo/Programming/cpp_project_VSCode/Cubic1/" && g++ bo
oktest.cpp -o booktest && "/Users/yuwenshuo/Programming/cpp_project_VSCode/Cubic1/"booktest
0-201-88954-4 5 12.00
0-201-88954-4 7 12.00
0-201-88954-4 2 12.00
0-201-88954-4 14 168 12