c++visual-studio-2022text-editorfile-formatnull-character

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


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)


Solution

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