File operation functions: fread and fopen

Source: Internet
Author: User

Shptree of warmerdam recently used. c is encapsulated as DLL separately, and then called in the component project. When spatial indexes are performed, the correct shapeID is always not obtained *. the format of the qix file is incorrect. Later, we thought that not all data is written in the Tree *. qix, and finally found that fread () cannot be read correctly!

In this way, the time for two days is gone ~

 

 

Use the fread (void * _ DstBuf, size_t _ ElementSize, size_t _ Count, FILE * _ File) function to read data,

The function return value of fread (buf, sizeof (int), 460, qixfile) is 4, that is, only four of the 460 elements are read successfully :(

At this time, I always thought there was a cache limit? However, after tracking the fread () function, we found that only EOF was returned during file reading, that is, it reached the end of the file! But the actual file has not reached the end. There are still many bytes. Why do you think EOF ??

 

When fopen () is sent, the parameter is "r". The default value is "text.

Ah, how much time does it take me to check the cause of the bug.

 

 

 

Record the fopen (), file open mode parameters (the following content comes from the http://blog.csdn.net/c_bg44/archive/2007/01/01/1472040.aspx ):

[C Language] fopen () function parameter description [09:24:26 | Author: bewalking]

Format: file pointer = fopen (file name, file method)

Parameters:
Object Name meaning
"C: // TC // qwe.txt" file C:/TC/qwe.txt
"Qwe.txt" and qwe.txt in the same directory as the program

File Usage
"Rt" only opens a text file and only supports reading data.
"Wt" only writes to open or create a text file, only data can be written
Append "at" to open a text file and write data at the end of the file.
"Rb" is used to open a binary file and only read data.
"Wb" only writes to open or create a binary file, only data can be written
Append "AB" to open a binary file and write data at the end of the file.
"Rt +" reads and writes to open a text file, allowing reading and writing
"Wt +" reads and writes to open or create a text file, allowing reading and writing
"At +" to open a text file, allow reading, or append data to the end of the file
"Rb +" reads and writes a binary file, allowing reading and writing
"Wb +" to open or create a binary file and allow reading and writing
"AB +" reads and writes to open a binary file, allowing reading or appending data at the end of the file

The usage of files is described as follows:

1. The file is comprised of r, w, a, t, B, and +. The meanings of each character are as follows:

R (read): read
W (write): write
A (append): append
T (text): text File, can be omitted without writing
B (banary): Binary File
+: Read and Write

2. When you use "r" to open a file, the file must already exist and can only be read from the file.

3. A file opened with "w" can only be written to this file. If the opened file does not exist, the file is created with the specified file name. If the opened file already exists, delete the file and create a new file.

4. to append new information to an existing file, you can only open the file in "a" mode. However, this file must exist at this time; otherwise, an error will occur.

5. If an error occurs when opening a file, fopen returns a NULL pointer value. In the program, this information can be used to determine whether to complete the file opening and handle it accordingly.

Fread Function

Prototype: t_size fread (void *, t_size, t_size, FILE *);

Example:

Int c;

Char ppp [100];

C = fread (ppp, sizeof (char), 2, myfile );

The number of characters written by char in two single-digit pairs taken from the file indicated by myfile to the region of the ppp, and the number of successfully obtained records is returned.

The second parameter is "single bit", and the third parameter is "n single bit ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.