Correct way to write file location - c++

What is the correct way to include files
#include "../myDirecoty/myFile.h"
or
#include "..\myDirecoty\myFile.h"
the difference is the direction of "/" or "\".

This is the correct way:
#include "../myDirecoty/myFile.h"

There is no difference, but the first form is more "clear" because sometime people thinks to \ as an escaping character in string (but include path are not strings)

normal slashes "/", best compatible

From what I've seen looking through the code on my computer, you should use forward slashes ('/').

The backslash \ is used on Windows and DOS, while the slash / is used on all UNIX/POSIX compatible systems (like Linux and Mac OS X). So the later can produce a file-not-found error on non-Windows systems. AFAIK all Windows compiler do support the slash /, so this is one to use.
Edit: See also this SO question.

Related

Difference between \\ and / when working with path directories

Whenever I do any sort of file read or write, I always use the '/'
but I've seen some examples where the value of the given filepath is '\\' instead.
So what's the difference?
Am I doing it wrong or introducing bugs if I use '/'?
There's nothing wrong with using / on systems that support it. In fact, on UNIX systems it's the only thing that works.
Windows supports both / and \ as path separator in most situations.
Note that a platform agnostic option is available in the form of std::filesystem::path.
The common convention used for managing paths in Windows is just reciprocal of Linux. It's formatted something like: C:\abc\abc.txt, although it's your own choice which method you would prefer to access/write the file or folder.
This \\ is an escape sequence to print a common backslash to read or write the file. Note that you won't able to use a single backslash between string value since it reads next character as an escape sequence (e.g. \n, \b, etc.)
That's it.

Qt QUrl: doesn't url-encode colon

I have URL: https://example.com/hello?param=first:last. I expect that it should be percent-encoded as https://example.com/hello?param=first%3Alast. But Qt leaves it as-is. My code:
QUrl url("https://example.com/hello?param=first:last");
printf("Encoded: %s\n", url.toEncoded().constData());
How should I encode colon? Manually format parameter with QString::toPercentEncoding?
You must replace colons because of security issues.
More information: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
You can use percent encoding (":" -> "%3A") for colons, see http://qt-project.org/doc/qt-5.0/qtcore/qurl.html#fromPercentEncoding and http://qt-project.org/doc/qt-5.0/qtcore/qurl.html#toPercentEncoding.
There has been some discussion regarding colon safety in URLs. It sounds like it comes down to the RFC adherence which I'm not versed in.
Is a colon safe for friendly-URL use?
Looks like you may have to replace any ":" characters (after https:) yourself.
I think QUrl::toPercentEncoding() is your best bet. It encodes all standard characters by default, and you can manually specify your own list of additional characters to encode:
Reference URL:
http://doc.qt.digia.com/4.7/qurl.html#toPercentEncoding
Pretty sure you want QUrl::setEncodedURL and QUrl::toEncoded
http://harmattan-dev.nokia.com/docs/platform-api-reference/xml/daily-docs/libqt4/qurl.html
Which version of Qt are you using?
IIRC Qt 3 used QUrl:encode

Using utf-8 characters in log4cxx

I need to be able to use utf-8-encoded strings with log4cxx. I can print the strings just fine with std::cout (the characters are displayed correctly). Using log4cxx, i.e. putting the strings into the LOG4CXX_DEBUG() macro with a ConsoleAppender will output "??" instead of the special character. I found one solution:
LOG4CXX_DECODE_CHAR(logstring, str);
LOG4CXX_DEBUG(logstring);
where str is my input string, but this does not work. Anyone have an idea how this might work? I google'd around a bit, but I couldn't find anything useful.
You can use
setlocale(LC_CTYPE, "UTF-8");
to set only the character encoding, without changing any other information about the locale.
I met the same problem and searched and searched. I found this post, It may work, but I don't like the setlocaleish solution. so i made more research, finally the solution came out.
I reconfigure log4cxx and build it, the problem was solved!
add two more configure options in log4cxx:
./configure --prefx=blabla --with-apr=blabla --with-apr-util=blabla --with-charset=utf-8 --with-logchar=utf-8
hope this will help anyone who need it.
One solution is to use
setlocale(LC_ALL, "en_US.UTF-8");
in my main function. This is OK for me, but if you want more localizable applications, this will probably become hard to track/use.
The first answer didn't work for me, the second one is more than i want. So I combined the two answers:
setlocale(LC_CTYPE, "xx_XX.UTF-8"); // or "xx_XX.utf8", it means the same
where xx_XX is some language tag. I tried to log strings in many languages with different alphabets (on LINUX, including Chinese, language left-to-right and rigth-to-left); so I tried:
setlocale(LC_CTYPE, "it_IT.UTF-8");
and it worked with any tested language. I cannot understand why the simple "UTF-8" without indicating a language xx_XX doesn't work, since i use UTF8 to be language-independent and one shouldn't indicate one. (If somebody know the reason also for that, would be an interesting improvement to the answer). Maybe this also depends by Operatin System.
Finally, on Linux you can get a list of the encodings by typing on shell:
# locale -a | grep utf

Linux won't load my images for my game

I am using C++ and using the G++ compiler to compile my game. It compiles fine, but I'm linking to images, and they aren't showing up in-game. I tried using local links and full links but I couldn't get either one to work. Is there a special way to link to a file in Linux?
Code Example:
StatBack.load_image("\\Dropbox\\Pirate_Entertainment\\images\\hud_thumbnails\\backdrop.png");
StatBack.apply_image_surface(nCurrentX - 180,nCurrentY-230,0);
StatBack.v_DeleteImages();
Unless you are using some kind of portability wrapper which transforms your backslashes to forward slashes, you probably want to use forward slashes:
StatBack.load_image("/Dropbox/Pirate_Entertainment/images/hud_thumbnails/backdrop.png");
or, more likely, using a relative path:
StatBack.load_image("Dropbox/Pirate_Entertainment/images/hud_thumbnails/backdrop.png");
In fact it is safe to use a forward slash in windows as well. The ANSI C standard allows forward slashes to be used in file names as the path separator.
you should use / as a path separator instead of \ (that C++ reduces to ).

__FILE__ Returns a String with "\/" in the Path

I use the __FILE__ macro for error messages. However, sometimes the path comes back as E:\x\y\/z.ext. It does this for specific files.
For example, E:\programming\v2\wwwindowclass.h comes back as E:\programming\v2\/wwwindowclass.h and E:\programming\v2\test.cpp comes back as E:\programming\v2\test.cpp. In fact, the only file in the directory that works seems to be test.cpp.
To work around this, I used jmucchiello's answer to this question to replace any occurrence of "/" with "\". This worked fine, and the displayed path changed to a normal one.
The problem was when I tried it on Windows 7 (after using XP). The string came up as (null) after calling the function.
Along with this, I sometimes get some seemingly random error 2: File not found errors. I'm unsure of whether this is related at all, but if there's an explanation, it would be nice to hear.
I've tried to find why __FILE__ would be returning the wrong string, but to no avail. I'm using GNU g++ 4.6.1. I'm not actually sure yet if the paths that were wrong in XP were wrong in Windows 7 too. Any insight is appreciated.
The function in the linked question appears to return NULL if there are no changes to make. Probably Windows 7 doesn't suffer from the \/ problem (in some cases).
As per MSalters's comment:
Typically, the compiler does so when you pass #include "v2/wwwindowclass.h" to the compiler.
Since every file has its own include statements, you can (but shouldn't) mix the two styles.
This was the case. My compiler automatically adds a forward slash.