binary files and text files

Source: Internet
Author: User
Tags integer numbers

1, the default in C language is to open the file as text.

2. binary files and text files
(1) A file is another form of storage of data in the computer's memory that is represented in binary format on the external media.
(2) files are usually divided into binary files and text files.
(3) A binary file is a file that contains data or program instructions written in ASCII and extended ASCII characters. Generally executable programs, graphics, images, sounds and so on.
(4) A text file (also known as an ASCII file): Each byte of it holds a file that can be represented as a single character of ASCII code. It is a "line" as the basic structure of the Organization and storage of a file, you can use any word processing program to read a simple text file.

3. Text mode and binary mode

(1) When writing data to a file in text mode (by default), once a newline character (ASCII 10) is encountered, it is converted to carriage return-newline (ASCII 13, 10). Write more than one character! When a file is read, a carriage return-a combination of line breaks (that is, consecutive ASCII 13, 10) is converted to a newline character (ASCII 10).
(2) When we write data to a file in binary fashion, the data is stored in memory as it is in the file.
(3) Maintain consistency when writing and reading files. If the text is written, it should be read in text mode, and if the data is written in binary mode, it should be used in binary mode when reading.
(4) Whether it is a text file or a binary file, there is no error if it is written and read uniformly in binary mode.

(5) Whether it is a text file or a binary file, it can be opened in binary or text mode, and then written or read. However, for binary files, some problems may occur if you read them in text mode.

4, the problem arises: to give you an integer, for example: 98341, the integer is saved to a file, requires that the Notepad program to open the file, also display 98341.
If you write 98341 directly to a text file and then open the file, you will not see this integer. Because for a text file, every byte of it is stored in an ASCII code that can be represented as one character. If you want to see 98341 this way in Notepad, you actually have to see "these characters corresponding ASCII code converted characters" in Notepad, that is, "98341" in Notepad is five characters, not integers: 98341!
A file is actually another form of storage on an external storage medium that is stored in data memory. When the file is opened in Notepad, that is, when the file is opened as text, every byte of data stored in the file is converted to the corresponding character as an ASCII code, but each byte of the data in the above file is converted to a character and is unreadable, so what you see is garbled.

Direct int I=98341;fwrite (&i,4,1,file); This will only output garbled characters.

Workaround:

(1):

File *file;file=fopen ("1.txt", "w"), Char *p= "98341"; fwrite (P,1,5,file); fclose (file);
or (2)
FILE *file;file=fopen ("1.txt", "w"), Char ch[5];ch[0]= ' 9 '; ch[1]= ' 8 '; ch[2]= ' 3 '; ch[3]= ' 4 '; ch[4]= ' 1 '; Fwrite (ch,1,5, file); fclose (file);
or (3): We Store "98341" to store the ASCII code of the five characters (the character "0" ASCII code is 48).
FILE *pfile=fopen ("3.txt", "w"); int I=98341;char ch[5];ch[0]=9+48;ch[1]=8+48;ch[2]=3+48;ch[3]=4+48;ch[4]=1+48; Fwrite (Ch,1,5,pfile); fclose (PFile);
or (4) use the itoa () function to convert the certificate to a string and then write the string to the file. However, this method value involves the preservation of numbers, if the numbers and characters are mixed together, and require to be opened with Notepad to see the numbers, do not use the ITOA function, you will convert these integer numbers to ASCII code, and then write the file.
FILE *pfile=fopen ("3.txt", "w"), int I=98341;char ch[5];itoa (i,ch,10); fwrite (ch,1,5,pfile); fclose (PFile);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

binary files and text files

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.