This isn't the beginners section. I don't have to assume you don't know how to do something in particular other than what you asked about. |
Granted. But I'm not a beginner anyway. As I said in my original post, I'm serializing the data from a generalized suffix tree. That's hardly beginner stuff. But just because you have experience doesn't mean there aren't things you don't know, and of course things you don't even realize you don't know. And these are C++ forums, not C. I'm no expert in C. I mentioned fscanf merely as an analogy. The truth is, none of our discussion of fscanf actually addressed my original question.
You didn't say "but I'm not trying to write an int to a char". |
I most certainly did. Here's the very beginning of my original question:
I'm serializing a suffix tree. Some of the data is stored as chars to reduce memory requirements. As a result, when I go to write chars to files and read numbers from files into chars, the insertion and extraction operators like to interpret them as characters, not numbers. |
When saving the suffix tree to disk, the data stored in a char takes up 1 byte. So when later loading that data from disk, of course it will fit into a char. In another response I said:
As for lying in my format string, I think you misunderstood. I was not suggesting reading data of different sizes into different types. |
And then, in my long example (some of the comments not shown here):
1 2 3 4 5 6 7 8 9
|
Node nd = Node();
nd.begin = 5; // nd.begin is an unsigned char type (0-255)
fout << nd.begin;
// Because nd.begin is a char type, a character will be written to disk, not the number 5.
fin >> nd.begin;
// Because nd.begin is a char, instead of storing the integral value 8 in nd.begin,
// it will store the value 56, which corresponds to the ascii character 8.
| |
All of these make it crystal clear that I was never trying to write data into a char that could not actually fit into a char.
When you were in school and the teacher said something that didn't make sense to you, did you get up from your seat and explain your version to him/her? No? Then may I ask WTF are you doing? |
What I was doing was explaining my current understanding of the topic. By doing so, the person with better understanding could tell me where I was incorrect.
If you're ignorant, you don't question what you're told. |
A good programmer needs to understand, not simply memorize. Only a fool just accepts everything he's told without trying to understand.
Ask the right questions and you'll get the right answers; ask the wrong questions and you'll get useless answers or confused looks. And we can't read your mind. Although sometimes it may seem otherwise, that's just undefined behavior and you shouldn't rely on it. |
If when referring to
char a [4] = "1234";
a person says "I'm only writing 4 characters," it's perfectly obvious that person isn't aware that the null terminator is added automatically. And when referring to
1 2
|
char a;
sscanf("123", "%d", &a);
| |
someone asks, "What's wrong with that?" It does not take a mind reader, but is rather completely obvious that person isn't aware that %d will always write more data than can fit into a char, regardless of what data is given to it.
Now that I know what I didn't know about %d, looking back at your explanations, I see how they make more sense. But without that critical bit of knowledge, those explanations were quite confusing since I could not see how they applied to what I was actually trying to do. That's why I kept trying to explain in different ways what I was doing.
I wasn't trying to piss you off, Helios. I respect you for spending time to try and help so many people. But you seem a little overly sensitive. When I didn't understand your explanation and tried to explain myself more completely, you acted like I was trying to insult your or teach you or took you for an idiot. Why would you jump to such a conclusion?
In school, the students who just sit quietly and don't bother asking questions when they don't understand something are the ones who do poorly. It can be difficult for teachers to gauge what parts of a lecture the students don't understand. That's why good teachers appreciate the students who ask a lot of questions, because those are often the same questions that the other students have as well.