Reputation: 14849
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 !
Upvotes: 3
Views: 4680
Reputation: 1586
Actualy just recenlty had the same issue with netbeans 8 under Ubuntu linux in C++ project.
Now you can pass file as input parameter, you just need to do it the right way:
Under File->Project Properties->Run->Run Command
Add this:
"${OUTPUT_PATH}" "./main"< "/home/user/projects/C/tests.in"
The second part between commas is your full path to the file. And its even being used when debugging - very useful.
Upvotes: 1
Reputation: 31
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.
Upvotes: 3
Reputation: 1782
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.
Upvotes: 3
Reputation: 3024
As far as I know, Netbeans is only an IDE, and it can be configured to use some different compilers. Your code should be coded according to what that compiler expects, but if you use the standard library you shouldn't have any problems.
Upvotes: 0
Reputation: 135265
Do you have access to the normal standard library classes in the NetBeans IDE? If so, normally you access STDIN using the cin
instance:
std::string line;
while (std::getline(cin, line))
{
// do something with line...
}
Upvotes: 0