Code Blocks redirecting input output

后端 未结 6 1840
被撕碎了的回忆
被撕碎了的回忆 2020-12-16 04:58

I\'m new to code blocks, and I can\'t seem to get it to work with command line arguments of < input > output. Does anyone know how to?

I\'m currently able to read

相关标签:
6条回答
  • 2020-12-16 05:20

    I've been working with Code::Blocks for some time now and just recently noted the same at least with Code::Blocks 12.11 in Windows. The redirections > and < do not work in the Project -> Set programs arguments...

    A hackish solution is to do the execution in post-build step.

    Right click project name -> Build options... -> Pre/post build steps -> Post-build steps:

    cmd /C cd /D "bin\$(TARGET_NAME)\" & YourApplicationNameHere.exe >output.txt 2>errors.txt
    

    And check the checkbox Always execute, even if target is up-to-date. Now hit Ctrl+F9 and the program is executed as a last step of the building process.

    0 讨论(0)
  • 2020-12-16 05:33

    I have found a way how to do it in CB 13.12

    Tools -> Configure Tools -> Add:
    
    Name: whatever
    Executable: C:\Windows\System32\cmd.exe
    Parameters: /C ${TARGET_OUTPUT_BASENAME} exampleArg1 <inputFileRedirect.txt
    Working Directory: ${TARGET_OUTPUT_DIR}
    

    It basicaly launches windows console and passes Parameters to it. You can also assign keyboard shortcuts to these tools. The only disadvantage i can see is that the tools are not project specific.

    0 讨论(0)
  • 2020-12-16 05:38

    I think it is the problem of cb_console_runner.exe which launches your program in IDE. ConsoleRunner can not interpret redirection symbol. So, I add some code to the original code of codeblocks 13.12. Please copy linked file to [cb folder]. (Don't forget back-up the original.)

    binary : http://limity.tistory.com/attachment/cfile30.uf@241A8D485621595131B28F.exe

    source code : http://limity.tistory.com/attachment/cfile23.uf@231AF3485621595232A632.cpp

    0 讨论(0)
  • 2020-12-16 05:40

    I tried almost all of the options & failed to make it work :P After becoming fed up with all that, I basically use file processing to get my work done ( phew )

    here is what I did in the code

    At global scope I wrote :

    #define DEBUG
    
    #ifdef DEBUG
    #include<fstream>
    
    ifstream Inputfile;
    ofstream Outputfile;
    
    #define cin     Inputfile
    #define cout    Outputfile
    
    #endif  //#ifdef DEBUG
    

    & in main I wrote the following before doing anything else:

    int main(){
    #ifdef DEBUG
    
        Inputfile.open("Input.txt");
        Outputfile.open("Output.txt");;
    
    #endif // #ifdef DEBUG
    

    Finally just before closing the main process did this :

    #ifdef DEBUG
    
        Inputfile.close();
        Outputfile.close();
    
    #endif // #ifdef DEBUG
    

    After this added two files

    Input.txt

    &

    output.txt

    to the project

    This worked as expected

    0 讨论(0)
  • 2020-12-16 05:41

    I was able to get input redirected to my c program by setting program arguments in project menu.

    Navigate to Top Menu>Project>Set programs' arguments and put </absolute/path/to/yourinputfile notice < in start it tricks codeblocks into redirecting file instead of passing argument.

    0 讨论(0)
  • 2020-12-16 05:41

    I know this is an old topic, but none of the solutions are good enough. For Windows, I would probably go with the following macro definition (as you may need it also for debug printing or similar) at global scope

    #include <cstdio>
    #ifdef DEBUG
        #define D(X) X
    #else
        #define D(X)
    #endif
    

    Then as the first or second line (if you need std::ios::sync_with_stdio(false); ) in main use it as

    int main() {
       D(freopen("input.txt","r",stdin);)
       D(freopen("ouput.txt","w",stdout);)
       ...
    

    And define in Code::Blocks under Projects > Build Options... > (Debug, Compiler Settings, #defines)

    DEBUG

    Expecting that "input.txt" is the text input file in the folder where rest of the .c or .cpp files are, and "output.txt" will be the output file generated in the same folder (or they can be both added to the project as such files for easier editing/viewing).

    This solution will work with both cin/cout and scanf/printf.

    0 讨论(0)
提交回复
热议问题