razrlele
V2EX  ›  问与答

Linux 下用 VIM 编译 Cpp 代码时如何读取数据文件?

  •  
  •   razrlele · Sep 1, 2014 · 2892 views
    This topic created in 4362 days ago, the information mentioned may be changed or developed.
    之前一直用的 ./a.out < data.txt
    最近把VIM改了一下直接F5编译运行, 从网上参考的方法如下:

    :autocmd BufRead,BufNewFile *.dot map <F5> :w<CR>:!dot -Tjpg -o %<.jpg % && eog %<.jpg <CR><CR> && exec "redr!"

    "Compile C&C++ with F5
    map <F5> : call CompileRunGcc()<CR>
    func! CompileRunGcc()
    exec "w"
    if &filetype == 'c'
    exec "!gcc % -o ~/build/%<"
    exec "!time ~/build/%< <"
    elseif &filetype == 'cpp'
    exec "!g++ % -o ~/build/%<"
    exec "!time ~/build/%<"
    endif
    endfunc

    但是这样就不不知道在F5按下去的时候自动运行并且自动加载数据文件了, 直接在后面加< data.txt好像不行

    然后我又想办法在代码里面添加文件读取操作, 但是又发现用freopen的话
    我是 freopen("data.txt", "r", stdin);
    每次直接数据读取完却不执行程序, 用fstream 的话
    fstream fin;
    fin.open("data.txt");
    又不能成功读取数据...
    请问该如何正确实现VIM 快捷键编译运行的同时还能读取数据?
    Supplement 1  ·  Sep 2, 2014
    问题解决了
    原来freopen里面不能使用 "~"
    必须用绝对路径.
    2 replies    2014-09-01 22:31:42 +08:00
    ChiangDi
        1
    ChiangDi  
       Sep 1, 2014 via Android
    你按下F5的时候就已经编译了,再加<data.txt 当然不行。
    razrlele
        2
    razrlele  
    OP
       Sep 1, 2014
    @ChiangDi exec "!time ~/build/%< <" 这一句应该是执行吧 为什么不能加 < data.txt?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5823 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 02:12 · PVG 10:12 · LAX 19:12 · JFK 22:12
    ♥ Do have faith in what you're doing.