kireirain
kireirain

Reputation: 35

Why do READ methods in Python and FREAD methods in C display different data?

I first open the file in python's “rb” mode ,and then i use “read” method to show the binary data for files. The file is 39 KB large.

Python code is:

aaa = open('C:\simfile\\testphoto.jpg','rb')
a = aaa.read()
print(len(a))  #39038
print(a)

The result of the Python code is enter image description here

As you can see, the binary form of the file is “\xff \xd8 \xff \xe0 ......” Next, I use the C language's FREAD and read methods to read the file and print its binary form.

#include <stdio.h>
#include <string.h>
#include<iostream>
#include <io.h>
#include<fcntl.h>
using namespace std;

int main()
{

   FILE *fp;

    //cout<<sizeof(char)<<endl;
   /* 打开文件用于读写 */
   fp = fopen("D:\CbProject\testphoto2.jpg", "rb");
   /* 写入数据到文件 */
   //fwrite(c, strlen(c) + 1, 1, fp);
   /* 查找文件的开头 */
   fseek(fp, 0, SEEK_SET);

    int mysize = 39038+1;

    rewind(fp);
      char buffer[mysize];
   /* 读取并显示数据 */
   fread(buffer, mysize, 1, fp);
   //int fd = open("D:\CbProject\testphoto2.jpg", O_RDONLY);

  // read(fd,buffer,39039);

   for(int i = 0;i<mysize;i++)
   {
        printf("%2x  ", buffer[i] );
        //cout<<i << "  ";
   }


   fclose(fp);

   return(0);
}

The result of C language is: enter image description here

enter image description here

why? what's more,C:\simfile\testphoto.jpg and C:\simfile\testphoto.jpg ,They are the same file

Upvotes: 0

Views: 164

Answers (1)

Stephan Schlecht
Stephan Schlecht

Reputation: 27126

The problem is in this line:

fp = fopen("D:\CbProject\testphoto2.jpg", "rb");

In C one starts an escape sequence within a string with the escape character \. The escape sequence consists of at least two characters, the most well-known example is presumably \n for a newline character.

To get an actual backslash in a string literal, use the escape sequence \\.

Therefore, the line should look more like this:

fp = fopen("D:\\CbProject\\testphoto2.jpg", "rb");

Some additional remarks:

  • the compiler usually warns about unknown escape sequences.
  • as mentioned in the comments, check file operations for error codes
  • fseek and rewind are not necessary, because the file is opened at the start anyway
  • since the question is tagged with C - unlike in C++ there are no namespace declarations in C
  • since the file length seems to be 39038 (see the output of your Python program), you should read 39038 and not 39038 + 1 bytes

Applying the above, your sample code could look slightly different as follows:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MYSIZE 39038

int main(void)
{

    FILE *fp;

    if((fp = fopen("c:\\simfile\\testphoto2.jpg", "rb")) == NULL) {
        fprintf(stderr, "can't open file\n");
        exit(1);
    }

    char buffer[MYSIZE];
    if(fread(buffer, sizeof(buffer), 1, fp) != 1) {
        fprintf(stderr, "read failed");
        exit(1);
    }

    for (int i = 0; i < sizeof(buffer); i++) {
        printf("%2x  ", buffer[i]);
    }
    fclose(fp);
    return(0);
}

Upvotes: 1

Related Questions