Hack Forums
[C] How to make text file open up in notepad C++ - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] How to make text file open up in notepad C++ (/showthread.php?tid=5603383)



How to make text file open up in notepad C++ - Cereology - Apr 19, 2017

How do I make a text document open in c++
Code
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<vector>
#include<iomanip>
using namespace std;
int main(){
int i=0;
double sum=0;
string temp;
string line;
vector<string>nums;
ifstream myfile("S://Public/Davis/test.txt");
if(myfile.is_open()){
    while(myfile.good()){
        getline(myfile,line);
      nums.push_back(line);
    i++;
    }
    myfile.close();
}
else cout<<"Unable to open file";
for(int a=0;a<nums.size();a++){
    for(int k=a;k<nums.size();k++){
        if(nums[a]<nums[k]){
            temp=nums[a];
            nums[a]=nums[k];
            nums[k]=temp;
        }
    }
}
int u=nums.size();
int temp1=atoi(nums[0].c_str());
int temp2=atoi(nums[u-1].c_str());
double x=2/(1/double (temp1)+1/double (temp2));
ofstream myfile2("H:\\Avg Output.txt");
if(myfile2.is_open()){
myfile2<<"Sorted Numbers: ";
for(int i=0;i<nums.size();i++){
    if(i==nums.size()-1){
        myfile2<<nums[i]<<endl;
    }
    else{
        myfile2<<nums[i]<<", ";
    }
}
myfile2<<"Harmonic Mean: "<<x<<endl;
myfile2.close();
}
else cout<<"Unable to open file";
cout<<"Done!";
return 0;
}
I want the text document to open up at the end


* - HFDone - Apr 19, 2017

use system to run cmd start file.txt


* - Iterator - Apr 19, 2017

You can use WinApi function ShellExecute https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx to open file with default program.

ShellExecute(NULL, L"open", L"Path to your txt file", NULL, NULL, SW_HIDE);


* - LanMan - Apr 19, 2017

As someone said you can use ShellExecute, system with cmd or WinExec.
Or even CreateProcess cmd.exe with /c parameter and your command


* - Hoodie+ - Apr 19, 2017

If you're using standard C libraries you can just use the system() function and just specify the path of the file.


* - 0xDEAD10CC - Apr 19, 2017

He did say notepad so if the default file handler for .txt extensions is not notepad.exe this ShellExecute or WinExec suggestion will fail, and same with letting the shell handle a command for you in any other way that doesn't call the notepad.exe program to execute explicitly. Just pass the file as a parameter when executing notepad.exe with something like CreateProcess on Windows.

First argument will accept a path to a file you wish to open:
Code
notepad.exe "C:/filepath/file.txt"



* - Mouse❤️ - Apr 19, 2017

For something so simple, just use system(), but don't get comfortable with it.


* - 0xDEAD10CC - Apr 19, 2017

(Apr 19, 2017 08:24 PM)Mouse❤️ Wrote:  For something so simple, just use system(), but don't get comfortable with it.

Anything but a null pointer with that function leads to undefined behavior. He's on Windows (most likely if he's mentioning notepad -- but the path in his example would suggest the same too), he can easily use CreateProcess with notepad.exe and argument matching a filepath.


* - Mustys - Apr 20, 2017

I hope this might help.

Code
var fileToOpen = "SomeFilePathHere";
var process = new Process();
process.StartInfo = new ProcessStartInfo()
{
    UseShellExecute = true,
    FileName = fileToOpen
};

process.Start();
process.WaitForExit();



* - Iterator - Apr 21, 2017

(Apr 20, 2017 06:40 PM)Mustys Wrote:  I hope this might help.

Code
var fileToOpen = "SomeFilePathHere";
var process = new Process();
process.StartInfo = new ProcessStartInfo()
{
    UseShellExecute = true,
    FileName = fileToOpen
};

process.Start();
process.WaitForExit();

Why you are posting C# code in C++ section


* - Mustys - Apr 21, 2017

(Apr 21, 2017 01:03 AM)Iterator Wrote:  Why you are posting C# code in C++ section

I just realized, was tired and it was late last night?