Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STDIN in Netbeans

Tags:

c++

netbeans

Does someone know.. how to read files to STDIN in Netbeans.
I have a Problem my Program in Netbeans. The Program works like this :

./myprog < in.txt 

It's a C++ Project. Any Ideas ?

Edit : I have Proglems with setting up netbeans . where can i say : READ/USE THIS FILE ? On the Console it just works fine !

like image 841
n00ki3 Avatar asked Sep 08 '26 22:09

n00ki3


2 Answers

I don't believe there's a way to ask NetBeans to pipe input to your program (that functionality is handled by your shell). If you want to test or debug your program in the IDE, the best way is to allow it to take a filename as a parameter, or fall back on standard input if no filename is given. Then you can adjust your project run configuration and supply the test filename as an argument.

Note that if you try to use "< file" in the run configuration, that will simply be passed directly to your program, because there is no shell intercepting it.

[Edit] I found a way, though it's a bit weak.

NetBeans (at least on my Mac) runs C++ programs via a script called dorun.sh, which is under the .netbeans folder in my home directory. It includes a line near the end like this:

"$pgm" "$@"

The quotes escape any use of the < operator in your project properties, so you could remove the quotes (and accept the consequences), or include a < between them:

"$pgm" < "$@"

and simply include the filename as an argument.

If you don't know where to find the arguments, it's in the project properties, off the file menu.

If you're using NetBeans on Windows, I'm not sure what kind of script (if any) it uses to launch your program. Also note that this doesn't work if you use the output window instead of an external terminal (this setting is in the same window).

[Edit edit] This is also no good when trying to debug your program... probably best just to amend your code to deal with reading from either a file or stdin.

like image 118
JimG Avatar answered Sep 12 '26 11:09

JimG


I agree with JimG regarding the best way to handle this (via filename as parameter, falling back on stdin if none). You can also pass a flag to your code via a make flag and conditionally use freopen in your code, like this:

#ifdef NETBEANS
  freopen("input.txt", "r", stdin);
#endif

Then in Project Properties -> Build -> Make -> Build command, you could do something like:

${MAKE} -f Makefile "DEBUG_FLAGS=-DNETBEANS"

Your Makefile would obviously have to be passing along DEBUG_FLAGS, something like this:

.cpp.o: $<
    $(CC) $(DEBUG_FLAGS) $(CFLAGS) $(INCLUDES) -o $@ $<

I use this method since I always use the same recycled make file inside and outside netbeans, and it's a 3 line fix in the code itself. I suppose parsing the cmdline is all in one spot, but to each his own.

like image 20
mdos Avatar answered Sep 12 '26 12:09

mdos