1

I am making my own file format. It looks like so:

<GameObjects>
{
    <GameObject>
    {
        <int id> 0
        <std::string name> GameObject0
        <Transform>
        {
            <vec3 position>       { 0, 0, 0 }
            <vec3 rotation>       { 0, 0, 0 }
            <vec3 scale>          { 1, 1, 1 }
            <quat orientation>    { 0, 0, 0, 1 }
            <mat4 globalMatrix>   { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
            <bool requiresUpdate> false
            <Transform parent>    -1
        }
    }
}

I do not want to use the '\n' character as a delimiter when I am reading an std::string because it would force me to use the '\n' as part of the file format syntax. So, I came with the idea of using the '\0' (null character) as a delimiter instead. I made the change, but now, I cannot open the file in Visual Studio (2022). I get the following error when double clicking it: enter image description here It opens in notepad with the '\0' represented as a whitespace, but I want to open it in Visual Studio (2022) because the editor is more powerful than notepad. Which leads to the question:

Is there a way I can see '\0' (null character) in Visual Studio's text editor?

Update 1: File with '\0' Creator

Here is a minimal code example:

int main() {
    std::cout << "Program operating" << std::endl;

    std::string s;
    char c = '\0';
    char delimiter = ':';

    std::fstream fstream = std::fstream();
    fstream.open("newfile.txt");
    FileData fileData = FileData(fstream);

    std::ofstream ofstream = std::ofstream();
    ofstream.open("newfile.txt");

    ofstream << "{";
    ofstream << '\0';
    ofstream << "}";

    ofstream.close();

    fstream >> c;
    fstream >> c;
    fstream >> c;

    fstream.close();

    std::cout << "Program terminated" << std::endl;
}

If you try to open the file, Visual Studio cannot open it (at least mine)

  • The error message says that VS isn't going to open it because of the reasons in the error message. If it doesn't open it at all, how is it supposed to show you the null characters? – Ken White Feb 25 '23 at 01:37
  • Is there a way to make VS support it through extensions maybe or any other alternative way? It does not make sense VS being more powerful than notepad, that notepad can open it and VS cannot. It is also very inconvenient. – Christopher Barrios Agosto Feb 25 '23 at 01:40
  • 3
    Has nothing to do with *powerful*. It has to do with a decision made by MS. Have you considered using Notepad++? It's *powerful* - it does time in a gym for hours every day, and allows you to see non-printable characters like spaces and tabs and nulls. – Ken White Feb 25 '23 at 01:44
  • 2
    Why not `\n`? Just quote your string. And why not a standard format such as [TOML](https://toml.io)? – Costantino Grana Feb 25 '23 at 02:23
  • Does this answer work: https://stackoverflow.com/a/1724591/4641116 – Eljay Feb 25 '23 at 13:17
  • No. I tried that, but it is not ideal at all for text editing. The closest I got to make it work was when I included the file to the solution and opened it with the JSON editor. It can identify there is a binary character. It shows as a question mark with a yellow diamond behind it. But, you cannot copy and paste it. It only lets you delete it. – Christopher Barrios Agosto Feb 26 '23 at 01:23
  • @CostantinoGrana I do not want to use quotations because I want to use quotations as part of my string values. It would confuse the interpreter by a lot. ```'\0'``` is not something that can be typed by my program. Therefore, avoiding all sorts of future bugs. – Christopher Barrios Agosto Feb 26 '23 at 02:24
  • Why not reading everything until "]]]", or whatever closing string that will not happen in your strings. Or providing an escape mechanism. – Costantino Grana Mar 01 '23 at 13:55
  • 1
    Gonna have a bad time processing string with '\0' in it whichever language/platform you're working with, the null character is used as the terminator of a string since the C/ASCII era. That is, most low-level API will stop processing the character sequence once the null terminator is found. – Sardelka Mar 02 '23 at 03:05

1 Answers1

0

Upon a close look at the ASCII table and some tests, I found that ASCII characters with decimal values from 1 to 31 are special characters with special meanings that VS can open and edit (copied and pasted). One specific character that can serve the functionality of marking the end of a string is the character with decimal value of 3 which is the end of text (EOF) character. It easily be assigned to a character as such:

char c = (char)3;

VS for whatever reason does not accept ASCII characters 1 to 31 through alt + 0 + # + #. The character can be copied and pasted though. \0 cannot be copied and pasted though most likely because of OS reasons where \0 marks the end of a string in standard C programs.

Here is a reference to the ASCII table: https://www.asciitable.com/

Future readers:

If the null character is absolutely necessary or you face similar issues, look into making your own binary file format or use the binary editor VS provide or whichever editor gets the job done.