views:

768

answers:

6

Is there an ascii value I can put into a char in C++, that represents nothing? I tried 0 but it ends up screwing up my file so I cant read it.., thanks.

+1  A: 

The null character '\0' still takes up a byte.

Does your software recognize the null character as an end-of-file character?

If your software is reading in this file, you can define a place holder character (one that isn't the same as data) but you'll also need to handle that character. As in, say '*' is your place-holder. You will read in the character but not add it to the structure that stores your data. It will still take up space in your file, but it won't take up space in your data structure.

Am I answering your question or missing it?

John at CashCommons
+1  A: 

Do you mean a value you can write which won't actually change the file? The answer is no.

Maybe post a little more about what you're trying to accomplish.

Mark Ransom
A: 

it would depend on what kind of file it is and who is parsing it.

Kevin
+3  A: 

Sure. Use any character value that won't appear in your regular data. This is commonly referred to as a delimited text file. Popular choices for delimiters include spaces, tabs, commas, semi-colons, vertical-bar characters, and tilde.

Mark Bessey
+4  A: 

ASCII 0 is null. Other than that, there are no "nothing" characters in traditional ASCII. If appropriate, you could use a control character like SOH (start of heading), STX (start of text), or ETX (end of text). Their ASCII values are 1, 2, and 3 respectively.

For the full list of ASCII codes that I used for this explaination, see this site

Greg
+2  A: 

In a C++ source file, '\0' represents a 0 byte. However, C++ strings are usually null-terminated, which means that '\0' represents the end of the string - which may be what is messing up your file.

If you really want to store a 0 byte in a data file, you need to use some other encoding. A simplistic one would use some other character - 0xFF, for example - that doesn't appear in your data, or some length/data format or something similar.

Whatever encoding you choose to use, the application writing the file and the one reading it need to agree on what the encoding is. And that is a whole new nightmare.

Bill Michell