Problems with CFile reading and writing binaries

Updated on technology 2024-04-16
6 answers
  1. Anonymous users2024-02-07

    Don't use cstring, the character length of cstring is unknown, so it is difficult to read and write. You can see what the value of sizeof(game) is.

    Let's just use an array of char type.

  2. Anonymous users2024-02-06

    Debug debugging to see where the error is.

    cfile file;,cfile::typebinary|cfile::moderead|cfile::

    sharedenynone|cfile::modenotruncate|cfile::modecreate);

    game g;

    sizeof(game));

    cstring temp;

    temp=;

    messagebox(temp);

    sizeof(game));

    Out of the book wrong values.

    It is recommended that you take a look at the persistence of objects.

  3. Anonymous users2024-02-05

    The reading of binaries in C is done with fread and fwrite.

    fwrite() is not the same as fprintf().

    fwrite saves the written data as the disk content of the file. fprintf saves the ASCII code for each character of the written data as the disk content of the file. fprintf did a conversion job.

    When a file is opened, Notepad automatically converts the disk content of the file into the corresponding characters as ASCII code, and then displays it, i.e., the text content is displayed instead of the disk content.

    For example, if you write "65" to a file with fwrite, the disk content of the file is the saved 65 (represented as binary on disk). When opening a file with Notepad, Notepad reads 65 and treats 65 as an ASCII code, and displays the corresponding character "A". Therefore, the text content seen on the screen is "a".

    When you use fprintf to write 65 to a file, the ASCII code corresponding to the characters 6 and 5 is stored in the disk content of the file, which are 54 and 53, respectively. So the disk contents of the file are 54 and 53. When you open a file with Notepad, when the Notepad reads 54, it shows the corresponding "6".

    Read 53 again, and the corresponding "5" is displayed.

  4. Anonymous users2024-02-04

    Since the data structure is.

    The length of the data (must be int) + the content of the data (which can be char) are stored in the form of binary data streams into their respective files.

    Then consider reading the length with fread and then reading out the data segment with fread, for example.

  5. Anonymous users2024-02-03

    This is normal, it is the operating system's cache at work.

    Cache: Created to address the difference between CPU speed and memory speed (CPU computing speed is much faster than memory).

    When the program needs to read a file, it is actually to read the data into memory by the CPU to calculate, the CPU first goes to the cache to find, and if it can't be found, it will go to the memory to read and copy it to the cache for the next access, at this time, the speed is naturally very slow, when you read the file for the second time, the cache already exists, and the CPU accesses the data again will become very fast.

    This is related to the way the system reads the data, and not because a function is inefficient, obviously:

    If you search for a file name under a certain disk, the first time will be slower, and the second time will be much faster, because the data that the CPU needs to process the second time is already stored in the cache, and the processing efficiency will be very high.

  6. Anonymous users2024-02-02

    When you know that the arguments of a function will not be modified, you can use the tst to modify the arguments, which has the advantage of preventing unintentional modifications of the arguments that you don't want to modify, and the modification of the const object can find errors during compilation.

    For example, the buffer in this write function, read data from this memory and write it to the file, the data in this memory will definitely not change, so add a sink to ensure that it will not be modified, to prevent getting drunk and unintentionally modifying it when you are nervous, that is to say, the st is to prevent yourself from making mistakes. The const in the write function is added by the author of the write function to prevent himself from making mistakes, which is a habit, and the caller knows that this parameter is read-only when he sees const, and if this parameter changes, it will definitely not be caused by this function. Almost all C C++ libraries follow this habit.

    The read parameter cannot be added to const, because this is written from the file read data into the buffer, so the buffer cannot be read-only.

Related questions
13 answers2024-04-16

Landlord, you just forgot to close it, and your program doesn't need to be overhauled. >>>More

8 answers2024-04-16

1.Binary 1000 is replaced by 16.

With the 8421 conversion method, that is, from left to right, 8*1+4*0+2*0+1*0=0x8, this is the universal conversion method for all hexadecimal to binary, bit-to-bit alignment conversion, multiply by 8421 respectively, and then add up. If the binary number. >>>More

12 answers2024-04-16

What is the base system?

If it's not a decimal one: >>>More

10 answers2024-04-16

If it's a method, I can give you a natural language description of how the decimal integer part n can be converted to binary as follows: >>>More

9 answers2024-04-16

#include

using namespace std; >>>More