Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

How to make text file open up in notepad C++

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
[Image: MOSHED-2019-11-30-10-59-36.jpg]
use system to run cmd start file.txt
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
You can use WinApi function ShellExecute https://msdn.microsoft.com/en-us/library...s.85).aspx to open file with default program.

ShellExecute(NULL, L"open", L"Path to your txt file", NULL, NULL, SW_HIDE);
As someone said you can use ShellExecute, system with cmd or WinExec.
Or even CreateProcess cmd.exe with /c parameter and your command
If you're using standard C libraries you can just use the system() function and just specify the path of the file.
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"
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
For something so simple, just use system(), but don't get comfortable with it.
(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.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
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();
(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
(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?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by pebblakOct 15, 2019 05:24 AM 6 307 Oct 15, 2019 05:24 AM
Last Post: Joseph Ducreux
[Shell] by Emre_Aug 31, 2019 07:22 AM 0 80 Aug 31, 2019 07:22 AM
Last Post: Emre_
[C] by VoxiFeb 27, 2019 03:55 PM 3 259 Feb 27, 2019 03:55 PM
Last Post: Voxi