How to create a text file in a folder on the desktop - c++

I have a problem in my project. There is a project folder on my desktop. I want to create a text file and write something include this text file. That is my code:
ofstream example("/Users/sample/Desktop/save.txt");
But I want to it could been run the other mac. I don't know what I should write addres for save.txt.
Can anyone help me?

Create a file and write some text to it is simple, here is a sample code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
std::ofstream o("/Users/sample/Desktop/save.txt");
o << "Hello, World\n" << std::endl;
return 0;
}
I hope that answers your question but I am not sure if i understand your question correctly, If not please add the details correctly of what you are trying to acheive.
[Update]:
Okay I guess the comment clears the problem.
Your real question is, You want to save the file in the desktop of the user who is playing the game. So getting the path of the current user's desktop is the problem.
I am not sure if there is an portable way to get desktop path but it can be done in following ways:
In Windows:
Using the SHGetSpecialFolderPath() function.
Sample code:
char saveLocation[MAX_PATH] = {0};
SHGetSpecialFolderPath(NULL, saveLocation, CSIDL_DESKTOPDIRECTORY, FALSE);
//Now saveLocation contains the path to the desktop
//Append your file name to it
strcat(saveLocation,"\\save.txt");
ofstream o(saveLocation);
In Linux:
By using environment variables $HOME
sample code:
string path(getenv("HOME"));
path += "/Desktop/save.txt";
ofstream o(path);

Rules defining where-you-should-save-file vary from platform to platform. One option would be to have it part of your compile script (that is you #define SAVEGAME_PATH as part of your compilation configuration), and thus your code itself remain more platform-agnostic.
The alternative is to find a save-data-management library that is already designed to be ported across different platforms. Whether it'd be a C or C++ or whatever-binary-interoperable library then no longer matters.
Just don't expect that to be part of C++ (the language).

if you want your program to run across platform,you'd better use the
relative path.
eg. "./output.txt",or better “GetSystemDirectory()”to obtain the system
directory to create a file,and then you could write or read the file
with the same path..

Related

how i could solve this problem with ifstream

when I run this code its only work with visual studio, in online compiler always tell me "cant opening file"
#include <iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
ifstream input("C:/Users/ACER/Desktop/words.txt");
if (!input.is_open())
{
cout << "cant opening file";
return 0;
}
An online compiler probably cannot open file on your computer using C++ code since that's not how the web works. It would have to route through some Javascript system, which is possible to create, however it seems the creators did not implement that when setting up their compiler.
You might try using file://C:/Users/ACER/Desktop/words.txt and double checking your path. Web browsers use that file:// thing when accessing local file i.e. if you had a web page on your computer it could be like file://C:/Users/ACER/Documents/index.html or something.
If that doesn't work, it just doesn't have the capability to load or save local files like that.
When you were attempting to use the code on an online compiler, most of the time you are actually compiling the code on their server, instead of on your computer.
What that means is that when you are trying to open "C:/Users/ACER/Desktop/words.txt", you are actually attempting to open a text file that is located on their server. And in most cases, you wouldn't be permitted to lookup any directory other than the one you are in. Even if you were permitted to do that for some reason, the chances they also have that text file on their server in the same location is gonna be really low.
Some comments above mentioned that many online compilers often don't have the facility to manipulate files. One that I often use do have the ability to do so in case you were looking for one: https://replit.com/#Ranoiaetep/IndianredSquigglyPrinters
.Note that the working directory is defaulted as your root directory.

How do I create a directory inside the directory where my .cpp file is placed?

I am creating a directory in the desktop folder using this code.
#include <iostream>
#include <stdio.h>
#include <direct.h>
#include<string>
using namespace std;
int main()
{
string k,s="c:/Users/Dell/Desktop/";
cin>>k;
string p =s+k;
if (mkdir(p.c_str()) != 0)
{
cerr<<"Failed"<<endl;
}
return 0;
}
But this code will be specific to my pc only as it has the whole parent directory path.
I want to create a new folder inside my working directory where the main.cpp is placed, because we don't know where the user will place the cpp file.
I know there are a lot of solutions similar to this problem on the forum but I couldn't find or understand any that could help me here. Thanks.
Do you really mean the .cpp file?
You can get the filename as a string using the __FILE__ macro and then use std::filesystem::path to manipulate it, but are you sure this is really what you want to do? Are you expecting end users to compile this program in the location they want to run it?
You can also (in theory) get the path through which the program was executed using the argv argument to main, which seems more likely to be what you mean, but it's unreliable in a number of surprising ways ...

Not able to read data from the file in xcode

I am a beginner c++ programmer and I was accustomed to using Visual Studio before, but i am now using mac and i use xcode now. In xcode, i am not able to read data from the file (so i am also not able to get the output.) Which setting should i change in xcode to read data from the file(the file is in the same folder as the project) properly ?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//in thefile.txt i only have a string "hello"
int main() {
string text; //since i only have a string in the file. So this is a variable representation for it
ifstream myfile;
myfile.open("thefile.txt");
myfile >> text; //extract the word from the file
cout << "The following is the data in the file: " << text <<endl; //trying to print The following is the data in the file: hello
return 0;
}
//Output is "The following is the data in the file: "
Probably the most straightforward way is to use a fully qualified file name to take the guesswork out of it. If you're not sure of the full name, you can open up a window in the OS X Terminal program, type "file ", and then drag your file from a Finder window onto that command line. Terminal will fill in the full path. "file" is an innocuous command that will tell you information about your file type. But the important thing is, you will have a full path that you can then copy from Terminal and paste into your code.
If your file path has blanks in it, you may have to manually remove the backslash characters that will appear.
Xcode doesn't have a setting per se (that I know of) to determine what directory is active when you launch your program. But a full path takes the guesswork out of it.

Best method copying folders in C++ on Windows 7

I am working on a simple program to copy all files in folders from one drive to another using C++. Using the Windows API function CopyFile(). I have used the following code:
#include <iostream>
#include <windows.h>
int main()
{
std::cout << "File Copier Version 1";
CopyFile("U:\\whateverfile.file","U:\\whateverfile2.file",0);
return 0;
}
What is the best way to handle copying an entire directory and all the files in it? Any other advice on this? Problems I may encounter?
If you wish to do it on Windows with progress display and other features, without putting too much effort into it, look up IFileOperation::CopyItem. However, it requires Vista or later.

C++ Getting basic properties of a program

I am writing an algorithm that wants to check if google-chrome or has the same version as the input given by the user. for that I need a way to check what version google-chrome has. I am using a linux machine to program but I want to make it work at home where I use win 8.1
Is there a way to check in C/C++ what the version of a program is?
I thing it is best to get the awnser in a string because then i can just compare with
if(strcmp(version, input)=1)
Thanks for reading.
PS. I started in C++ but I can change, even to java if neccesary
here is the basic version of what i have now:
#include <iostream>
#include <string>
using namespace std;
#define x 256;
int main(){
std::string version;
std::string input;
//get version
if(strcmp(version, input)=1){
//versions are equal
}
//chrome needs to be updaded
return 0;
}
You can launch a terminal process from C with the popen() command. You'll need to include the stdio.h header. Here's a code snippet that might help you:
FILE *pd = popen("google-chrome --version", "r");
char output[50];
fgets(output,50,pd);
pclose(pd);
In the output array you'll get something like "Google Chrome 25.0.1364.97"