I'm fairly new to C++, and I can't seem to find any sources on opening applications or files straight from the program.

I looked up the system() function on this site, but all that it did for me was verify the file's path and post it's size.

I need the function w/any necessary headers that can actually open a file given the file's path.


Here's what I had...

#include <iostream>
#include <fstream>

using namespace std;

int main()
    system("DIR C:\\WINDOWS\\system32\\sol.exe");
    fopen ("DIR C:\\WINDOWS\\system32\\sol.exe", "\n");

Recommended Answers

All 2 Replies

in c++ use the fstream or ifstream class that is declared in <fstream> and <iostream> header files. fopen() works too, but its declared in <stdio.h> and is C, not C++. Look in cplusplus code snippets and you will find quite a few examples.

>> fopen ("DIR C:\\WINDOWS\\system32\\sol.exe", "\n");
That line is completely wrong. The first argument is the name of the file you want to open, and the second argument is how to open it -- there is no such open flag as "\n" -- that is used on printf() statements to move the cursor to the next line of the screen or create end-of-record in text files. If you want to read the file, here is the correct syntax: fopen ("C:\\WINDOWS\\system32\\sol.exe", "rb"); Since sol.exe is a binary file you will want to read it as binary data, not text data.

Now that line will not actually execute sol.exe but just allow your program to read it into memory using fread() or similar functions.

Thanks very much, and for tolerating my newness especially.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.