I am using Xcode to code C++. Whenever I create a new project, the default code template is
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
I want to change it into something else. How can I do this?
Thanks in advance.
So emm.. thanks to the encouragement of #Alan Birtles. I now know how to do this.
First, we need to find where the default templates are. Go to this directory
/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/Project\ Templates/Mac/Application/Command\ Line\ Tool.xctemplate
I use Xcode to mostly code C++ codes, so I am only changing the command line tool templates. But I think this works for all kinds of tools.
Now that you are in this directory, you should see a file named:
TemplateInfo.plist
This is the file you should change.
Open this file with a test editor. In my case, I used atom to make changes to this file.
This file is written in a special syntax(that I didn't understand)
Try to understand the basic syntax and change this as the way you like.
Here's how I changed the default C++ part:
<key>C++</key>
<dict>
<key>Nodes</key>
<array>
<string>main.cpp:comments</string>
<string>main.cpp:include</string>
<string>main.cpp:main:content</string>
</array>
<key>Definitions</key>
<dict>
<key>main.cpp:include</key>
<string>#include <iostream> //changes start from here
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iostream>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
#include <queue>
using namespace std;//changes end here
</string>
<key>main.cpp:main:content</key>
<string>// insert code here...
std::cout << "Hello, World!\n";
return 0;
</string>
</dict>
</dict>
Save the changed file and try to create a new command line project.
It works great.
ps.Do copy the file before you make any changes in case you mess it up, if you changed it in the wrong way and didn't make a copy of that file, there's a chance that you won't be able to create a project.
Related
I am making a toast notification apear, but when the script runs it just closes the powershell like if it ran it but no noticfication apears. How could I make it work?
#include<iostream>
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string>
#include <cstdlib>
using namespace std;
void main()
{
string strPath = "d:\\callPowerShell.ps1";
system("start powershell.exe d:\\callPowerShell.ps1");
}
It would recall the file and run it on PowerShell
Some time ago I made an app in C++Builder and I did something like that for made a backup in MariaDB. The code I used was something like this:
UnicodeString usMysqldump, usCmd;
usMysqldump = "In my case path to mysqldump.exe";
usCmd = "Script to be executed";
ShellExecuteW( Application->Handle, "open", usMysqldump, usCmd, NULL, SW_HIDE );
PD: SW_HIDE is for keep the console hide, may be you want something diferent.
Try this code and I hope it be helpfull for you.
I am trying to write my program from VS (MVCS) for Qt(MinGb)
I am using ofstream and has a following code:
#include <fstream>
#include <string>
#include <iostream>
#include <exception>
using namespace std;
void WriteToFile(ofstream* fileToWrite, std::string StringNeedsToWrite)
{
if (fileToWrite)
{
if (IsStartOfNewString(StringNeedsToWrite))
{
*fileToWrite << '\n';
}
*fileToWrite << StringNeedsToWrite;
}
else
{
throw exception();
}
}
I want to write string into file, and set ofstream into my method.
But i have a strange error and don't know how to fix it (on image)
They differ in their make files and project files. A common problem is that moving a project from one environment to another entails big mistakes.
For example, MinGW under Linux does not have standard streams, but there is windows.h.
Such problems should be taken into account and studied at the stage of developing a program architecture and choosing technologies for development.
Frequent practice has shown that Qt is best used with your IDE and, if possible, use its classes for development.
I am using Visual Studio 2019, and my code uses console outputs which change colors frequently. I am including Windows.h in my code, which is the header file that contains SetConsoleTextAttributes, whereas STD_OUTPUT_HANDLE should be initialized by using namespace std. My code in its entirety can be found here, but the following is the section with the error:
#include <iostream>
#include <cmath>
#include "HeadFile.h"
#include <windows.h>
#include <string.h>
using namespace std;
int Play(char(&spaces)[7][6], int(&color)[7][6], int player, int playerOneWins, int playerTwoWins, int ties)
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
.....
The code runs fine, but inside of studio itself, I see the following error:
The error is coming from the first instance of STD_OUTPUT_HANDLE only (another case at the bottom of the picture has no errors). If I comment out the first one, the next instance errors:
How can I fix this issue? I've read in a few non-related posts that using namespace std can sometimes lead to problems. Is this the case?
Use Header file "Windows.h" instead of "windows.h".
I had the same problem, adding #include <stdlib.h> fixed it for me.
I had the same problem, you need to use #include <Stdio.h>
I am writing a small program for my father.
He needs it to cycle through a list of URLs contained in a text file every 15 seconds. I have it opening the URLs but I cant figure out how to either redirect the current tab or close and open a new one.
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <string>
#include <fstream>
#include <iostream>
#include <windows.h>
std::ifstream infile("links.txt");
int main(){
std::string link;
while(std::getline(infile, link)){
system(std::string("start " + link).c_str());
Sleep(15000);
}
}
I am fairly inexperienced with C++ and out of practice. Any and all help is greatly appreciated.
Your first issue is you are calling start on a link which does not exist as a program, this won't work if that is the link to a website. That being said, using system() is very dangerous and slow, this explains it a little better and provides an alternative for windows: https://stackoverflow.com/a/15440094/2671276.
I am working on a Windows VC++2008 program that does fileIO, and have hit an issue that is really weird. in my #include directives I have
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
and then I have a method that actually does the fileIO, but when I try to open the file like this:
std::ofstream Output;
Output.open("Output/log.txt", ios::out);
my intelisense allows it, and even has correct auto completes, but my compiler throws an error of:
1>c:...\engine\gsp420maincore\gsp420maincore\messagequeue.cpp(141) : error C2653: 'ios' : is not a class or namespace name
1>c:...\engine\gsp420maincore\gsp420maincore\messagequeue.cpp(141) : error C2065: 'out' : undeclared identifier
when I read about the ofstream.open() it stated that whether the file to be opened is for input, output, or both should be specified, but ios should be automatically included by any other iostream #include directive, and this problem is not corrected when I insert the:
#include <ios> // directive
the compiler has no complaints when I remove the second argument, but I know that I should try, and specify just in case I want to go in and read from a file as well as write to it. did I do something wrong?
It looks like you forgot to prefix it with std:: and you haven't used using namespace std; (judging from the fact that you explicitly state the namespace for std::ofstream).
Try changing it to std::ios::out.
You should not need to #include <ios> manually.
As already noted, you need std::ios::out -- only you really don't need it at all. When you open an ofstream, it opens for output by default (likewise, an ifstream gets opened for input by default). I'd also advise initializing the object at creation rather than creating a sort-of uninitialized stream, then opening it separately. Taking that into account, you get rather simpler bit of code:
std::ofstream Output("output/log.txt");