CLion standard input while debugging - c++

What I'm trying to do is basically:
./myProgram < myData.txt
While I'm debugging with CLion IDE. I just can't find the option to do so.
A similar question - but product-specific to MSVS

I had the same problem and it seems that CLion is not handling standard inputs yet.
I got around this problem by changing the input stream before running my program.
As an example if you want to input a file stream inside your stdin you can write in your main:
std::ifstream in("ABSOLUTE_PATH_TO_YOUR_FILE");
std::cin.rdbuf(in.rdbuf());
Then you can find a way to toggle this stream change when you want. Note that for files you will need to provide absolute path since the application is run from a different directory than the current one.
I hope this can help until CLion provides a real solution.

Assuming your input file is myData.txt, you can reopen/reuse the stdin stream using freopen
freopen("myData.txt","r",stdin);
if you want to do the same with your output:
freopen("myOutput.txt","w",stdout);
this will work for std::cin, printf, etc...
You can find more information about this here: http://www.cplusplus.com/reference/cstdio/freopen/
By the way, there is already a feature request for this. If you are interested, you can vote here so it gets prioritized:
https://youtrack.jetbrains.com/issue/CPP-3153

As of CLion 2020.1 this feature is built in:
Input redirection
If you need to redirect input from a file to the
stdin of your application, you can now do that. Use a new field in the
configuration called Redirect input from. Enter:
A relative path (CLion will prepend with the Working directory path).
An absolute path (will be remapped for remote configurations).
Or macros (like FilePrompt).

Still Clion don't have the feature like pycharm where we can give input in terminal while debugging the code.
But it has an option to give input through a .txt file while debugging.
Image of debug setting window
Click the setting icon in the debug console (on upper left corner) to open the setting of debugging. Then check the "Redirect input from" box and select the input file path and click "OK".
Here you go!
Now you can give input from the text file while debugging the code.

For me, CLion creates the executable in a file called 'cmake-build-debug'. Check out my file structure in the pic.
Then, I just opened up my terminal and went to the directory containing the executable and used this command to pipe in the text file:
./FirstProject < ../hw1.txt

Related

File as Command Line arguments

I am new to C++ and I am learning with Visual Studio.
I want to make a small program that reads a text from the command line and opens the text. I know that my program has to start like this:
int main (int argc, char *argv[] ){
ifstream File( argv[1] )
But I am super confused on how to run it from the Visual Studio Command Prompt. I know there are a lot of questions like this but I still haven't found what I am looking for. I read that you have to go to properties, Debug and change the command line arguments, but what exactly do I need to put in there? And what should I type in the VS Command Prompt.
Thank you!
From menu find: Project->Properties. Or from the Solution Explorer tree right click on the project and select Properties.
Now, in the opened dialog left pane select: Configuration Properties->Debugging
Then in the right pane grid find the line titled: Command Arguments
Fill it with the input file name (I think you better put it there as a full path, if there is a space in the path use with double quotas. like this:
Without space:
filepathwithoutspace.txt
or with spaces:
"file path with spaces.txt"
Good luck.
For starters, your code should not start like that: before
passing argv[1] to std::ifstream::ifstream, you should
verify that there is an argument, and output an error message
otherwise. As it is, you could end up passing a null pointer to
the constructor of ifstream, which will result (normally) in
a program crash.
As to how to run it: where did you put the executable? If
you're in the Visual Studio Command Prompt window, and have
invoked cl, then by default, the executable should be in the
local directory. Just enter .\name, where name is the name
of your program. If you've actually compiled it from within the
IDE, then in the command window, you should use cd to navigate
to where the executable was generated (which you can find out
from your properties), and invoke it as above; or you can simply
specify the path completely:
c:\Users\me\whereeverIPutTheThing\name.
If you want to debug (using the debugger), you need to specify:
1) the name of the executable (but the default should be good),
2) the parameters to pass it (what you want to see in
argv[1]—don't forget the quotes if it has a space in
it), and 3) the directory where the executable should run. The
second and third are somewhat interdependant: you can, for
example, specify just the filename in 2, and the path where the
file is located in 3, or you can specify the complete path to
the filename in 2, and forget about 3. Or use a combination of
the two: in practice, I tend to do everything from the root
directory of the project, so I'd specify a path relative to this
directory, and then the path from my project file to this root
in 3. (The way we have things set up, this is ..\..\.., but
I think you'll find it somewhat shorter.)

Open .bmp in Windows Photo Viewer from C++ app

In my program I have a CImage that gets saved to a .bmp file on the hard drive. How can I have my program automatically open that picture in Windows Photo Viewer?
If you want the default program for that file type/extension to open the file, then you will have to open a shell and tell it to open the program for that file type.
See: How can you open a file with the program associated with its file extension? for more details.
You should just run the program, and probably give path to the file you want to open through command line parameters. It really depnds on the program you want to run.
On Linux, to start a program, you should use fork / exec pair. On Windows, you use CreateProcess
This question is old but I was also looking for the same and got the answer.
Hope it will be helpful for others also.
::ShellExecute(NULL,L"open",_T("rundll32.exe"),_T("shimgvw.dll,ImageView_Fullscreen picture.bmp"),NULL,SW_SHOWNORMAL);
This will open the picture in Windows Default Photo Viewer.
at the place of picture.bmp you need to pass full path of the picture/image like D:\\picture.bmp.

How do I open a file in c++ (other than notepad)

I was wondering how to open a file other than notepad... Our prof gave us an example:
s = "notepad.exe test.txt";
system(s.c_str());
That will open a file type of "notepad.exe" and the file name of "test.txt"
Main Question:
Now, I was wondering if there was a way to open other type of files, such as Microsoft Excel, Microsoft Word, Visual Studio, or 7zip.
My attempt opened something in a new cmd.exe (because of the START keyword):
fileNeededtoBeOpened = "START \"New Microsoft Office Excel Worksheet.xlsx\"";
system(fileNeededtoBeOpened.c_str());
(This code is slightly different from my original, where I'm trying to open a file from a vector...) but all I really need to know is instead of "notepad.exe" or "START" is there a different command to open different file types that aren't .txt
Also, a side note, I was reading on the internet that it wasn't safe to use system() to open files, is this correct?
I found the answer by myself... for those who are curious, here an the answers:
To open a text file: system(notepad)
To open an excel file: system(start excel)
To open a word doc file: system(start winword)
To open a 7z file: system(start 7zFM)
To open a visual studio file: system(start devenv)
I think you're confused.
System executes a command as you would on the command line (type cmd into the run prompt under start menu to get that).
So, when you type notepad.exe test.txt it's saying:
Open the program notepad.exe which is on the system path (so the
command line can find it to execute that program), and pass the
parameter test.txt to it.
Notepad itself decides what to do with test.txt, in this case it opens it.
So, you can tell it to run any command (program/executable) and pass any parameters to it in reality. If excel's on your system path, you can probably just type excel.exe to open it from your system command. Otherwise, find the location excel is installed in, and refer to it with the whole path to excel.exe and it will work fine.
For example, on my computer, executing "C:\Program Files\Microsoft Office\Office12\EXCEL.EXE" would open excel from the command line. I can pass further parameters to it by having more information (like filenames) after the Excel.exe" portion, just as you did in your notepad example. Using your system command should have equivilent behavior when that line is executed.
If you are only targeting Windows systems you can use the ShellExecuteEx function (part of the Win32 API). You can just pass a filename to it and it will launch what ever program is registered to handle that file type (just as if you opened the file from windows explorer). Documentation is available on MSDN:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762154(v=vs.85).aspx
There is some examples on Launching Applications (ShellExecute, ShellExecuteEx, SHELLEXECUTEINFO) MSDN article and lots more elsewhere around the internet.
AS the other guys mentioned , the System function only executes a cmd command, .. notepad.exe is in the system's path by default so it works directly
but for example for me if I want to open a zip file on my desktop , I'd type something like
"C:\Program Files\7-Zip\7zFM.exe" Desktop\zipfile.zip
that's when I'm currently at the my user's directory [by default] , or
"C:\Program Files\7-Zip\7zFM.exe" C:\Users\JiMMaR\Desktop\zipfile.zip
[where JiMMaR is my user name on windows 7]
note that this certain command works only on windows , if you are using another OS this won't work as it is
try doing a
fileNeededtoBeOpened = "\"C:\Program Files\7-Zip\7zFM.exe\" C:\Users\YOUR_USER_NAME\Desktop\zipfile.zip";
and see if that executes or not
edit:
if you cannot escape the space , then try this one
fileNeededtoBeOpened = "C:\Program~1\7-Zip\7zFM.exe C:\Users\YOUR_USER_NAME\Desktop\zipfile.zip";
Ok, firstly - system - is a function that starts a separate process to your program. Much the same as in a command window when you type the command. The command lines you provide will be dependent on the applications you want to launch.
Now, I was wondering if there was a way to open other type of files,
such as Microsoft Excel, Microsoft Word, Visual Studio, or 7zip.
Yes I would be pretty shocked if there wasn't a command line parameter you could specify to load a document in these apps at start up. (Ok not shocked, but it is pretty standard)
Does this have anything to do with c++ - not really - you need to look at references for the applications you mention and see what the command lines parameters are for them. Then craft a string and system(...) to your hearts content.

Open file to display content in C++

I have 2 questions to ask regarding opening files (any kind of files) using C++. I am currently working on a GUI program and I want to add a changelog in txt form. Also I want a menu in my program to open that changelog.txt with the default text editor every user has installed or to simply put it to open that text file. Please keep in mind that I want to open the file for display NOT in the program for input/output.I know I can do that using
system("notepad.exe filepath.txt");
or to open them with the preset program:
system("filepath.txt");
The problem is that both of those open a command line behind the notepad. I know there is another command to open files using Win32 API called CreateProccess() but my compiler doesn't recognise that command (OpenWatcom W32).
So here are my questions:
1) Is there any other command to open files or is there a way to stop the command line from opening when using system command?
2) How do you define in Windows that the text file is in the current program folder? I mean instead of giving the entire filepath which will change from user to user is there any way to "tell" the program that the file is always on the current folder that the program is in?
I am sorry for any errors, if you want any clarification please let me know.
CreateProcess would be the wrong function to use here. That would require you to decide which process to run. The user may prefer to use a text editor other than Notepad, I know I do! The right way to do this on Windows is to ask the shell to open the file with whatever program the user has associated with the file. The ShellExecute function does this.
Call it like this:
ShellExecute(
MainWindowHandle,
"open",
FullyQualifiedTextFileName,
NULL,
NULL,
SW_SHOWNORMAL
);
You will need to include the Shellapi.h header file and link to the Shell32.lib library. If your compiler does not include these files, and I would be surprised if that was the case, then you can get them from the Platform SDK. That said, if you are serious about programming on Windows you should get hold of a tool that gives you access to the Windows API.
I do recommend that you use a fully qualified path for a task like this. Since your text file is located in the same directory as the executable you should simply join that directory to your text file's name. Get hold of the full path to the executable by calling GetModuleFileName passing NULL for the hModule parameter.

C++ program infile won't open in xcode?

alt text http://img638.imageshack.us/img638/5731/screenshot20100613at121.png
Why does the c++ program produce the error shown? I'm especially confused since outfile opens without error yet infile displays the error? Both are defined in xcode exactly the same!! I've altering the "path type" setting without success. The open on infile always fails! Any suggestions would be very much appreciated!!
For those who responded thanks but as you can see but infile and outfile exist and are in the same location:
alt text http://img709.imageshack.us/img709/9316/screenshot20100613at123.png
In Xcode, expand the "Targets" node and find your target. Given what I've seen, it'll be the only one there.
Next, right-click on your project and add a new "Copy Files" build phase. When the dialog box comes up, make sure you set the Destination to "Products Directory".
The new build phase will appear beneath your target with whatever name you gave it. Drag your in_file and out_file there.
Compile and run.
In your current case out_file would be created even if it doesn't exist (because you're using std::ofstream).
in_file, on the other hand, has to exist, and (I guess there is no such file in the directory with created binary), hence an error is produced.
Did you try launching your compiled application with the file in the same folder where the binary file is?
Probably there is no file named "in_file" in the program's working directory, so it can't be opened for reading.
For outfile this doesn't matter since it is opened for writing and if it doesn't exist yet it will just be created.
The directory listing you posted doesn't show where the compiled executable is, but probably it will be somewhere in the build directory. Probably this is then also its working directory and the place where the input file would need to be. (Look for the out_file the program creates when it is run, it will be created in the working directory, the same directory where it searches for in_file. And it is not the directory you posted the listing of, the out_file there is too old.)
Turns out you have to specifically create the file here:
Project Path/build/Debug
apparently you can't just define it directly in the xcode project