boost locale c++ - understanding the basics - c++

First of all How do i make the following example work (from boost website):
#include <boost/locale.hpp>
#include <iostream>
using namespace std;
using namespace boost::locale;
int main()
{
generator gen;
// Specify location of dictionaries
gen.add_messages_path(".");
gen.add_messages_domain("hello");
// Generate locales and imbue them to iostream
locale::global(gen(""));
cout.imbue(locale());
// Display a message using current system locale
cout << translate("Hello World") << endl;
}
(tried creating an hello.mo file but still didn't work).
Basically what i am trying to do is to be able to cout a string like: "operation",
and then according to file1 / file2 it will print the string value under id:operation for that specific file.
how can i do that?
Thanks.

boost translate: po file not work might help.
The most confusing step is:
2. Put the .mo file into the correct file structure, for example if your trying to translate to spanish this would be ./es_ES/LC_MESSAGES/hello.mo
It's a wraper of GNU 'gettext' utilities. The manual is useful too.

Related

Printing boost path on windows with escaped backslashes

I need to print out a path (stored as boost filesystem path) to file, to be parsed back to path later.
The parser expects paths in windows platform to be escaped, so a path like
c:\path\to\file
will appear in the file as
c:\\path\\to\\file
Is there a method in boost path to do this? or do i need to process the output of string() method to add the escapes?
Did you hear about std::quoted?
It can be handy for things like this. Alternatively, use the power of your shell (e.g. Escape FileNames Using The Same Way Bash Do It)
Live On Coliru
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::quoted(R"(c:\path\to\file)") << std::endl;
std::cout << std::quoted("c:\\path\\to\\file") << std::endl;
}
Prints
"c:\\path\\to\\file"
"c:\\path\\to\\file"
Note: also shows raw string literal

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.

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"

Appending to existing file via boost

I need to aggregate many log files into a single log.
I tried to do this with boost::filesystem::copy_file but it doesn't support appending.
Any ideas? (I'm preferring doing this via boost libraries)
Tnx
You don't need Boost for this simple task - the standard iostream will do the job:
#include <fstream>
//...
using std::ifstream;
using std::ofstream;
ifstream input1("input1.log"), input2("file2.log");
// append to an existing file
ofstream output("output.log", ofstream::out | ofstream::app);
output << input1.rdbuf() << input2.rdbuf();
//...
(Note however that the above approach may have suboptimal performance; take a look at this answer to see how to improve the performance.)

How to create a text file in a folder on the desktop

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..