莫非凉。 2023-01-02 12:18 采纳率: 40%
浏览 26
已结题

Mac环境 C++文件重定向读取文件内容与写入文件内容一样

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
我想要达到的结果:将输出结果写在outfile中
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 1月10日
    • 创建了问题 1月2日

    悬赏问题

    • ¥15 Todesk 远程写代码 anaconda jupyter python3
    • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置