Read file encountered 0x1a unexpected termination solution

Source: Internet
Author: User

In the integrated development environment of Windows (the problem exists in Qt, VC, vs), write about the file read C/E + + program, the occurrence of the unexpected termination when reading to 0X1A, after the debug check found that 0x1a after reading was processed into 0XFF (that is, EOF (-1)), However, there is no problem with this parsing error in Linux (Redhat6.4 and Ubuntu14.04 tests). The reasons for this problem can be referred to: https://blog.csdn.net/zhoubl668/article/details/7054282. Then there are two ways to solve this problem:

1. Read in binary mode:

FILE * fp = fopen ("File", "RB");//read as binary stream

Example of a ". DCM" File resolution (parsing NEW.DCM information into hexadecimal output into New.hex):

* * April 20, 2018 16:37:10 * Binary Stream read */#include "stdafx.h" #define String_buffer char Strbuf[string_buffer] = {

0};

    int main (void) {unsigned int i = 0;  File * FPR = fopen ("F:\\NEW.DCM", "RB");//Read the file * Fpw = fopen ("F:\\new.hex", "w") in binary stream;//output file if (FPR = NULL | |
        FPW = = NULL) {printf ("Open file failure at line:%d\n", __line__);
    Exit (Exit_failure);
    } fprintf (FPW, "%08x:", num); while (!feof (FPR)) {//directly feof () can be used to determine int ch = fgetc (FPR);//read one character strbuf[num% string_buffer] = (unsigned c
        HAR) ch;//and recorded in a 16-character-line buffer fprintf (FPW, "%02x", (unsigned char) ch);//write the character (1Byte, such as ' a ') to the output file (2Byte, ' a ' corresponds to 0x41)
            num++;//read the number of characters plus 1 if (num% 16 = 0) {//output file format, read 16 bytes for a row fprintf (FPW, ";");
                    for (i = 0; i<string_buffer; i++) {//parse buffer characters if (Strbuf[i] > && strbuf[i] < 127)
                fprintf (FPW, "%c", Strbuf[i]);
             Else       fprintf (FPW, ".");

    } fprintf (FPW, "\n%08x:", num);/newline memset (strbuf, 0, String_buffer);//Empty Buffer}}
    Fclose (FPR);

    Fclose (FPW);
    printf ("complete!\n");
return 0; }

2, to determine the file read encountered EOF (0XFF) due to 0x1a or to the end of the file caused by:

FILE * fp = fopen ("File", "R");//Read

fseek (FP, 0, seek_end) in text file;
Const size_t Len_file = Ftell (FP);
Fseek (FP, 0, Seek_set);

Gets the length of the file and then determines whether it ends based on the number of characters read

The same is read on the file mentioned above:

#include "stdafx.h" #define String_buffer char Strbuf[string_buffer] = {0};

    int main (void) {unsigned int num = 0, i = 0;
    File * FPR = fopen ("F:\\NEW.DCM", "R");//Read file * Fpw = fopen ("F:\\new.hex", "W") in a non-binary stream;
        if (FPR = NULL | | fpw = NULL) {printf ("Open file failure at line:%d\n", __line__);
    Exit (Exit_failure);
    //Get file length fseek (FPR, 0, Seek_end);
    const unsigned int len_file = Ftell (FPR);

    Fseek (FPR, 0, Seek_set);

    printf ("%d\n", len_file);
    fprintf (FPW, "%08x:", num); The feof () cannot be judged because the 0x1a has been parsed into 0XFF while (num < len_file) {//To determine whether to reach the end of the file based on the number of characters read and the length of the file int ch = fgetc (FPR
        );
            if ((unsigned char) ch = = 0XFF) {//if read to 0XFF, determine if the 0X1A is caused by strbuf[num% string_buffer] = 0X1A;
            fprintf (FPW, "1 a");
            Fseek (FPR, num+1, Seek_set);
            Cannot use Fseek (FPR, 1, seek_cur) or fseek (FPR, Ftell (FPR) +1, Seek_set);
Because if you encounter a terminator EOF or 0X1A, the value of Ftell () that is seek_cur becomes a multiple of 4096        } else{strbuf[num% string_buffer] = (unsigned char) ch;
        fprintf (FPW, "%02x", (unsigned char) ch);
        } num++;
            if (num = = 0) {fprintf (FPW, ";");
                    for (i = 0; i<string_buffer; i++) {if (Strbuf[i] > && strbuf[i] < 127)
                fprintf (FPW, "%c", Strbuf[i]);
            else fprintf (FPW, ".");
            } fprintf (FPW, "\n%08x:", num);
        memset (strbuf, 0, String_buffer);
    } fclose (FPR);

    Fclose (FPW);
    printf ("complete!\n");
return 0; }

For information about why the FGETC () return value is int and another way for EOF to read the unexpected end of a file, you can refer to the return value of the FGETC function as the int type. The end of these two accidents is not a situation, one is the logic is not rigorous results, one is currently looking at the Microsoft System Library problem.

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.