4 Replies - 559 Views - Last Post: 22 January 2013 - 07:11 AM Rate Topic: -----

#1 Guzfraba  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 01-November 12

[Program]Save 10 seconds of your life.

Posted 19 January 2013 - 06:37 AM

Hello! I've made a program that creates a "main.cpp" in a new project.It is useful for time saving I think :)/> The code that is written in "main.cpp" is taken from a "sc.txt" file,located in the folder with the program.You can change the content from the sc file anytime.So here is the code,you have to modify just one line I think:
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>

using namespace std;
int main(){
system("title patch_v2.exe");
string  pname,tname;
string stmp,st;
fstream cpp,source;
cout<<"Enter project name: ";
getline(cin,pname);
while(pname.empty()){
	cout<<"Enter a valid project name : ";
	getline(cin,pname);
}

tname="C:\\Documents and Settings\\user name\\My Documents\\Visual Studio 2010\\Projects\\"+pname+"\\"+pname+"\\main.cpp"; 
// here ^^/>/>


cpp.open(tname,ios::out);
source.open("sc.txt",ios::in);
if(!cpp) {
	cerr<<endl<<"Unable to open project."<<endl;
	Sleep(1000);
	return 0;
}
if(!source) {
	cerr<<endl<<"Unable to open source file."<<endl;
	Sleep(1000);
	return 0;
}
while(getline(source,stmp)){
	st+=stmp+"\n";
}

cpp<<st;
cpp.close();
source.close();
cout<<endl<<"Ready to write in."<<endl;
Sleep(700);
return 0;
}
 

0.Modify the beginning of the line 19 with the address of your VC++ projects.
1.Now build the program and copy it on the desktop(for example).
2.Create a file "sc.txt" on the desktop and write in it something,for example:
#include <iostream>
using namespace std;
int main() {

system("pause");
return 0;
}

3.Open compiler,select new project.
4.Open the program,write in the projects name.
5.In the compiler select Source file->Add->Existing item and open main.cpp.
Ready-you saved 10 seconds of your life ;)/>.
Of course you can edit the sc.txt file to something longer.
Hope this helped anybody :D/>

Is This A Good Question/Topic? 0
  • +

Replies To: [Program]Save 10 seconds of your life.

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,592
  • Joined: 08-August 08

Re: [Program]Save 10 seconds of your life.

Posted 19 January 2013 - 07:19 AM

Using system() is discouraged because it means your code will not be portable. As an example, your code will only work on Windows systems.

By the way, many IDEs already generate a main.cpp when you start a new project.
Was This Post Helpful? 1
  • +
  • -

#3 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: [Program]Save 10 seconds of your life.

Posted 19 January 2013 - 10:10 AM

Why are you hardcoding your directory path?

tname="C:\\Documents and Settings\\user name\\My Documents\\Visual Studio 2010\\Projects\\"+pname+"\\"+pname+"\\main.cpp"


Using the Windows API you can get the current directory using the standard windows function GetCurrentDirectory or you may use _getcwd.

This post has been edited by Jeet.in: 19 January 2013 - 10:11 AM

Was This Post Helpful? 0
  • +
  • -

#4 gibson_junk  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 24-July 09

Re: [Program]Save 10 seconds of your life.

Posted 22 January 2013 - 06:59 AM

View PostCTphpnwb, on 19 January 2013 - 07:19 AM, said:

Using system() is discouraged because it means your code will not be portable. As an example, your code will only work on Windows systems.

By the way, many IDEs already generate a main.cpp when you start a new project.


Whats a better method other then system?
Was This Post Helpful? 0
  • +
  • -

#5 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: [Program]Save 10 seconds of your life.

Posted 22 January 2013 - 07:11 AM

View Postgibson_junk, on 22 January 2013 - 07:29 PM, said:

View PostCTphpnwb, on 19 January 2013 - 07:19 AM, said:

Using system() is discouraged because it means your code will not be portable. As an example, your code will only work on Windows systems.

By the way, many IDEs already generate a main.cpp when you start a new project.


Whats a better method other then system?


You can use the following to pause the console window:

cin.ignore();
cin.get();


Use it before the return statement. A more thorough discussion is here: Holding The Console Window Open

As for renaming the Window title use
SetConsoleTitle()
found in windows.h. Sample usage would be
SetConsoleTitle("title patch_v2.exe");

This post has been edited by Jeet.in: 22 January 2013 - 07:17 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1