Code Blocks redirecting input output

2020-02-09 14:31发布

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 a file passed from argv[1] but, the program doesnt automatically read the input from the given file nor does it right the output to the file output.

I'm aware it is on set program's arguments, my arguments line is: list.txt < input > output

After some research I saw a guy doing it like this: < ./input > ./output, seems like running a program to give the input and output, anyways, I've also tried that to no avail. Do I need to use file handlers to interact with it? It doesn't make sence, simple getchar() should read from the passing input file.

What am I missing here?

Thanks in advance

6条回答
Explosion°爆炸
2楼-- · 2020-02-09 14:54

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.

查看更多
Anthone
3楼-- · 2020-02-09 14:56

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.

查看更多
来,给爷笑一个
4楼-- · 2020-02-09 15:07

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.

enter image description here

查看更多
冷血范
5楼-- · 2020-02-09 15:09

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

查看更多
倾城 Initia
6楼-- · 2020-02-09 15:10

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.

查看更多
We Are One
7楼-- · 2020-02-09 15:10

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

查看更多
登录 后发表回答