How to write the contents of the console to a file in C++ or C, multiple process/exe may write to console - c++

I have multiple exes(one exe call other exe, another exe may call another....) which are writing something to console. I would like to copy the displayed contents of console to a text file. In C++ or C. not manually by right click and mark paste. If same happens through code it will be a great help.
C or C++

Write the name of the exe file in the console/command prompt along with greater than symbol and the file name in which you want the output.
Ex. Considering "abc.exe" the executable file.
write the below statements in your cmd or console.
abc.exe > output.txt
This will create a file "output.txt" with the output, in the same folder where the exe file exists.

Related

Open Text File in the root directory

I have made a simple scoring system which upon correct answer, stores the numbers of the player in the file.
I have used the file name like this :
ofstream outfile ("C:\Aadam\Desktop\Project\Scores.txt",ios::app);
But the problem with this approach is that what if I move the program over to a USB and try to run it in another computer. Now it will look in the directory I specified above but there is no Scores.txt file in there.
What I want to do is to give it a path which is in the project folder. So when I move the program, it shouldn't make a difference because I will move the whole project folder.
Of course I can do this :
ofstream outfile ("Scores.txt",ios::app)
which will always look in the project directory and it will work fine as long as I run the program from the IDE but what if I run the program from the .exe file which is two directories down like
"C:\Aadam\Desktop\Project\bin\Debug\Project.exe"
Now in this case, it can't open the file.
So if you know a good way to open files and kindly, Show me the Way.....
You can parse argv[0] (it will contain path used to invoke your executable - absolute or relative) and replace executable name in it with "Scores.txt"
The easiest way is to pass the file path to program as an argument.
When you run a program from IDE, the project directory is considered as current working directory. If the program is run from the command line, the current working directory is from where the command is being run.
If you run the exe file,ofstream outfile ("Scores.txt",ios::app) will create a file named "Scores.txt" in the same directory as your program.

C/C++ project under Visual Studio : Not found resources

When I run the Debug in Visual Studio for a project, fopen function works fine. It tries to open a file contained in the project and that I added in the filter "Resources".
But when I run .EXE file of my project, I get the null pointer exception: 0x000005c.
When I added the file to be in the same directory as my .EXE file, the exception disappeared.
This is the instruction I use :
fopen(&filename, "rb");
I know it is adviced to use fopen_s instead, but the file is not found anyway...
Apparently, the file is searched always in the current directory...
So, how to include the file in .EXE and make the path of the file relative to the .EXE, at a way it will be contained in the .EXE and not added to the directory where there is .EXE?
You can't include the file in the .exe. You just need to make sure that the file is in the same directory as the .exe.
If you really, really want to only use one file, you could either:
Zip the .exe and the text file together and make sure you include in a readme that the text file needs to be in the same location as the .exe
Use an array/struct/some other way of storing the contents of the file in the program itself, and reference that instead of using a file (I assume you don't care about users being able to edit this data outside of an instance of the program since you wanted it bundled with an executable, so the file is unnecessary in that case)
The reason the program only works when you put the file in the directory of the .exe is because the path to the file is defined in the program as something like .\file.txt or file.txt. When the file isn't in the same directory as the .exe, the program will try to find it, and will be unable to, which is why you get the error. It works when you debug because you have a copy of the text file in the same location as the debug .exe.
EDIT: I would also ignore the warnings about fopen_s and other variant's that append a _s to the end of a command - these are windows specific, non-standard, and in most cases, pointless. If you know this program will only be used in windows environments and you're not doing something for school where you are required to write standard code, I suggest you use the _s variants for the sake of security, but it will reduce portability of your code.

Visual Studio 2010 run .exe from command line vs run (f5) debug

I am new to c++ and am making a very simple program. All my program does is call a function from the main function, which reads in a text file and returns. To check that I am reading in the file correctly, I am trying to print out the strings I have read in. The print out (cout) works properly when I run from Visual Studio (f5). However, when I run the executable from command line, none of the print outs from my function show up. Only print outs directly in the main function appear. I cannot find a similar question elsewhere. Any help would be appreciated.
When you run a program from within VC++ the current directory is set to the project directory by default, but the application is by default in a different folder.
E.g. the application may be:
D:\Work\MyApp\Debug\MyApp.exe
But the project directory may be:
D:\Work\MyApp\MyApp\
When you start the program from outside of VC++ you need to take steps to make sure the current directory is correct, or that the executable and any data files it refers to are in the same folder.
The default working directory for an IDE-launched project in Visual Studio is the project folder. This is the folder where you project file resides (the .vcproj or .vcprojx file is the project file).
If the data file you are reading is in the same folder, code like this:
std::ifstream inf("datafile.txt");
will succeed because the current working folder and the folder where the data file resides are the same.
However, if you switch to where the executable was written (typically this is the project-dir/Debug or project-dir/Release folders) and run the same executable from a command-shell, the data file will not be found.
To test this is the case. Do the following:
Open a command prompt.
Switch to the project folder where your data file resides.
Run the executable with a specified path: ./Debug/YourProgram.exe, for example.
Note: you can avoid this by having the program take the data file name as an argv[] parameter. Then your program will simply use whatever file you tell it to at launch-time.

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.

How do i write a program that opens an exe file(spectrawin) and saves the data into a specific name and folder?

I'm using c++. How do i write a program that opens an exe file(spectrawin) and saves the data into a specific name and folder?
Cheers
This will make a directory called Output, and pipe the output from your executable into a file called output.txt, inside that Output directory.
mkdir ("Output");
chdir ("Output");
system ("spectrawin > output.txt");
Please note that I do not know what spectrawin does. The above code works with an exe that outputs to the console.
Incidentally, if you need to know where your working directory currently is, use something like:
char workingDir [_MAX_PATH];
_getcwd( workingDir, _MAX_PATH );