7

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

d0pe
  • 573
  • 4
  • 9
  • 23
  • 1
    This redirects standard input and output only. Using `printf()` and `scanf()`, `puts()` and `getline()`, etc. should be fine. –  Nov 08 '12 at 19:00
  • It still doesn't work :( I've changed the getchar() to gets as I wanted the whole line, now I get char by char within the string but, when I run it, it still waits for manual input. I really think it's a codeblocks problem. – d0pe Nov 08 '12 at 19:35
  • No, it's not a codeblocks problem by any means, you're missing something... Imagine, what could an IDE do? It's not even a compiler... –  Nov 08 '12 at 19:50
  • Well, I went to linux, the input command works fine so, it must be an IDE issue. And also, it reads getchar() just fine, there is no need to scanf or gets to read from – d0pe Nov 08 '12 at 19:59
  • @d0pe Can you give an example program that exhibits the problem that you demonstrate and the steps needed to reproduce it? – fuz Nov 09 '12 at 07:11

6 Answers6

7

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.

5

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.

Fabio Antunes
  • 22,251
  • 15
  • 81
  • 96
user2678713
  • 51
  • 1
  • 2
1

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

codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37
Jehan Yoon
  • 11
  • 1
  • The source code link does not work. well can you tell the original command that the CB uses to launch a program in cb_console_runner.exe – Hassan Uddin Feb 01 '22 at 10:37
1

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

codefreak
  • 6,950
  • 3
  • 42
  • 51
  • Tried with Code::Blocks 16.01 but this didn't work as it leads to call `Executing: "C:\Program Files (x86)\CodeBlocks/cb_console_runner.exe" "C:\Work\cpp\cout\cout.exe" – Vadzim Aug 04 '16 at 21:05
1

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

AnotherDeveloper
  • 2,161
  • 2
  • 23
  • 27
1

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.

Star Wolf
  • 61
  • 3
  • I didn't understand the use of DEBUG? What exactly it does?? – Sumit Apr 17 '20 at 03:05
  • @Sumit Use of DEBUG is same as if you had first line of the program "#define DEBUG", but only when Debug build is selected locally. In example, if you followed instructions above, and you submitted such code to a grader, any code between "D(" and ")" would be excluded during compile time, thus, not choking on a missing input file on a remote system/grader. With the code/config above, you would get the same results as if you used for "Set program's arguments" for Debug build on Linux " < ./input.txt > ./ouput.txt". – Star Wolf Jun 18 '21 at 13:16