STDIN at Netbeans

Does anyone know .. how to read files in STDIN in Netbeans.
I have a problem with my program in Netbeans. The program works as follows:

./myprog < in.txt 

This is a C ++ project. Any ideas?

Edit: I have Proglems with netbeans setup. where can I say: READ / USE THIS FILE? It works great on the console!

+2
source share
5 answers

I do not believe that there is a way to ask NetBeans to connect the input to your program (this functionality is processed by your shell). If you want to test or debug your program in the IDE, the best way is to let it accept the file name as a parameter or to backtrack from standard input if no file name is specified. You can then configure the project launch configuration and provide a test file name as an argument.

Note that if you try to use "<file" in your startup configuration, it will simply be passed directly to your program because the shell does not intercept it.

[Edit] I found a way, although it is a little weak.

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

"$ pgm" "$ @"

Quotation marks exclude any use of the <operator in your project properties so that you can remove quotes (and accept the consequences) or include <between them:

"$ pgm" <"$ @"

and just specify the file name as an argument.

If you do not know where to find the arguments, in the project properties, outside the file menu.

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

[Edit] This is also not useful when trying to debug your program ... perhaps it is best to change the code to deal with reading from a file or stdin.

+3
source

I agree with JimG regarding the best way to handle this (via the file name as a parameter, backtracking to stdin if not). You can also pass the flag to your code through the make flag and conditionally use freopen in your code, for example:

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

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

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

Your Makefile should obviously go through 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 this is a 3-line fix in the code itself. I believe cmdline parsing is in one place, but for each of them.

+3
source

Do you have access to the regular classes of the standard library in NetBeans IDE? If so, you usually access STDIN with a cin instance:

 std::string line; while (std::getline(cin, line)) { // do something with line... } 
0
source

As far as I know, Netbeans is just an IDE and can be configured to use some different compilers. Your code should be encoded according to what this compiler expects, but if you use the standard library, you should not have any problems.

0
source

In fact, only recenlty had the same problem with netbeans 8 under Ubuntu linux in a C ++ project.

Now you can pass the file as an input parameter, you just need to make the correct path:

In File->Project Properties->Run->Run Command 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 it is even used for debugging - very useful.

0
source

All Articles