Are Binary Files and Text Files the Same?

  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    Binary files
AI Thread Summary
Binary files and text files are fundamentally different in how they are interpreted by programs, despite both being sequences of bytes. Binary files contain data in a format specific to their type, while text files represent characters using encodings like ASCII or Unicode. The distinction between the two is crucial for understanding how data is stored and accessed, as programs treat them differently, particularly in file operations. Reading a file in binary mode retrieves the raw bytes, while reading in text mode interprets those bytes as characters, which can lead to the same visible output if the content is purely text. Understanding these differences is essential for effective file handling in programming.
  • #51
Thanks guys, I got it, after I hit F10 and got into the BIOS, I just search around and found the Action Key Mode and disable it, and then restarted the computer. It works. I can use the F keys like normal. I got into VS and manage to create breakpoints at will.

Also, I learn how to step line by line. But I can only start the stepping after I run to the first break point, then I can step one step at a time using the mouse arrow.

But I cannot manage to get the window that show the value of the variables now. I know I've seen it before, but now I cannot make it appear. How do you bring up that window?

Thanks
 
Technology news on Phys.org
  • #52
yungman said:
But I cannot manage to get the window that show the value of the variables now. I know I've seen it before, but now I cannot make it appear. How do you bring up that window?
Debug menu --> Windows --> Locals
 
  • Like
Likes sysprog and yungman
  • #53
I stepped through the program and I can see step by step the value/string of variables. That works. My question is how do I determine whether inF.open(), outF.open() working or not? Also whether the stuffs are written into file or read back looking at the window below?
Compile error Listing 7.2.jpg


The program is working, just want to know how to look at it if it fails.

Also, what else I should learn on debugger other than reading these. I want to get back to the C++!

Thanks
 
Last edited:
  • #54
yungman said:
I stepped through the program
In my view that's good. The 'diagnostic techniques' set of topics constitute an eventually-advanced topic set that pretty much begins with step-by-step analysis. I recommend that you please continue with your use of the debugger tool-set.
 
Last edited:
  • Like
Likes yungman
  • #55
yungman said:
The program is working, just want to know how to look at it if it fails.
Most of the I/O routines return a value or have a way to check whether they succeeded. In the code that I showed in post #46, I added a line to see if the file opened successfully.
C++:
dataFile1.open("demofile1.txt", ios::binary|ios::out);
if (dataFile1.fail()) std::cout << "Failed to open demofile1.txt" << "\n";
If the call to dataFile1.fail() is true, the file couldn't be opened for some reason, and my code prints a message to that effect.

I added a line after the call to write().
C++:
dataFile1.write((char *)(&x), sizeof(x));
if (!dataFile1) std::cout << "Failed to write to demofile2.txt" << "\n";
The write() function returns a reference to the stream it's writing to. If the call to write() fails, the function returns null, and my code prints a message.
sysprog said:
I recommend that you please continue with your use of the debugger tool-set.
+1
 
  • Like
Likes yungman and sysprog
  • #56
I am back to char* ptr; ptr = reinterpret_cast<char*>(&x) where int x; I have been reading back old posts. I know now this is just changing an integer pointer(&x) to char pointer. So now it's char ptr = &x? Meaning it's still the address of x.
 
  • #57
yungman said:
I am back to char* ptr; ptr = reinterpret_cast<char*>(&x) where int x; I have been reading back old posts. I know now this is just changing an integer pointer(&x) to char pointer.
Yes. The bit pattern that represents an address doesn't change in the slightest. All that changes is that the address is now considered to be the address of a char instead of an int.
yungman said:
So now it's char ptr = &x? Meaning it's still the address of x.
No, not quite. The left side of what you wrote is wrong.
This says that ptr is a character, not the address of a character.
Here is a revision of the code you've been asking about that might clarify things for you. Each line is commented to explain what is happening.
C++:
int x = 0x1234;   // Declaration + initialization of x
pInt = &x;        // pInt holds the address or x
char * pChar;     // Declaration --pChar can hold the address of a character.
                  // Since pChar isn't initialized, it contains only a garbage value.
pChar = reinterpret_cast<char *>(pInt);     // The address in pInt is copied to pChar

After the last line executes, pInt and pChar contain exactly the same bit pattern; the address of x. The only difference is how this address is interpreted -- as the address of an int or the address of a byte.
Because of this difference, if we dereference these two pointers, we'll get different results.
cout << *pInt displays 4660, the decimal representation of 0x1234.
cout << *pChar displays 4, because the first byte of the four-byte representation of 0x1234 is 0x34. As a char, this is the ASCII code for the character '4.' This value is due to the way bytes are stored in Big-endian form on Windows machines.
 
Last edited:
  • Like
Likes sysprog
  • #58
We're up to 57 posts now, and I believe the two major questions of this thread have been exhaustively answered, so I will close the thread. @yungman, if you have any lingering questions, you can send me PM. For new questions, please start a new thread.
 
  • Like
Likes Tom.G
Back
Top