C language file reading and writing-by byte, by line + when reading and writing files, the last line is read twice.

Text flow

  Text stream, that is, we often say that we read files in text mode. Some characteristics of text stream may be different in different systems. One of them is the maximum length of the text line. The standard stipulates that at least 254 characters are allowed. Another feature that may be different is how the text line ends. For example, in the Windows system, text files are agreed to end with a carriage return and a line feed. But under Linux, only a newline end is used.

   Standard C positions the text as zero or more characters, followed by a newline character (\n) indicating the end. For those systems where the external representation of the text line is different from this definition, the library function is responsible for the external and internal forms Translation between forms, for example, in the Windows system, when outputting, the newline character of the text is written as a pair of carriage return/line feed. When typing, carriage returns in the text are discarded. This ability to manipulate text without considering its external form simplifies the creation of portable programs.

Binary stream

  The bytes in the binary stream will be written to the file completely according to the form in which the program is written, and read into the program completely according to the form in which they are read from the file or device, without any changes. This type of stream is suitable for non-text data, but if you do not want the I/O function to modify the end-of-line characters of the text file, you can also use them for the text file.

   The C language does not distinguish between these two types of files. They are treated as character streams and processed by bytes.

   Open files in text mode and binary mode that we often see in our programs are only reflected in the processing of line breaks.

   For example, under Windows, the newline character of a file is \r\n, but under Linux it is \n

   When the file is opened in text mode, the newline character \r\n in the read-write Windows file will be replaced with \n and read into the memory. When the file is written under Windows, \n is replaced with \ r\nWrite the file again. If the file is opened in binary mode, the conversion between \r\n and \n is not performed. Since the newline character under Linux is \n, there is no difference between a text file and a binary file.

File read and write functions

1. Read and write files according to characters: fgetc(), fputc()

2. Read and write files by line: fputs(), fgets()

3. Read and write files in blocks: fread(), fwrite()

4. Read and write files in accordance with the format: fprintf(), fscanf()

5. Read and write files according to random positions: fseek(), ftell(), rewind()

Read and write files by character

#include<stdio.h>
#include<string.h>
#pragma pack(show)



void test01()
{
	int i;
	char ch;
	char buff[] = "hello world";
	FILE* f_read;
	//写文件
	FILE* f_write = fopen("./test1.txt","w");


	if(f_write == NULL)
		return;

	
	for(i = 0;i < strlen(buff);i++)
	{
		fputc(buff[i],f_write);
	}
	fclose(f_write);

	//读文件
	//FILE* f_read = fopen("./test1.txt","r");
	f_read = fopen("./test1.txt","r");
	if(f_read == NULL)
		return;
	while((ch = fgetc(f_read)) != EOF)
	{
		printf("%c",ch);
	}
	fclose(f_read);
}


int main()
{
	test01();

	return 0;
}

operation result:

hello world

At the same time, a test1.txt file will appear in the same level directory as the .c file

Read and write files by line

#include<stdio.h>
#include<string.h>
#pragma pack(show)



void test01()
{
	int i;
	char ch;
	char temp[1024] = {0};
	
	FILE* f_read;
	//写文件
	FILE* f_write = fopen("./test2.txt","w+");
	char* buff[]=
	{
		"锄禾日当午\n",
		"汗滴禾下土\n",
		"谁知盘中餐\n",
		"粒粒皆辛苦\n"
	};

	if(f_write == NULL)
		return;

	
	for(i = 0;i < 4;i++)
	{
		fputs(buff[i],f_write);
	}
	fclose(f_write);

	//读文件
	//FILE* f_read = fopen("./test1.txt","r");
	f_read = fopen("./test2.txt","r");
	if(f_read == NULL)
		return;

	
	while(!feof(f_read))
	{
	
		fgets(temp,1024,f_read);
		printf("%s",temp);
	}
	fclose(f_read);
}


int main()
{
	test01();

	return 0;
}

operation result:

On the day of hoeing,
sweat drips into the soil.
Who knows that the Chinese food on the
plate is
hard
work. Please press any key to continue...

I found that the last line was read twice, what happened?

 

You can see the following definitions in stdio.h :

1

2

3

4

5

6

7

8

9

#define EOF (-1)

#define _IOEOF 0x0010

#define feof(_stream) ((_stream)->_flag & _IOEOF)

int c;

while(!feof(fp))

{

    c = fgetc(fp);

    printf("%X\n", c);

}

The reason is that after reading the last character, fp->flag is still not set to _IOEOF, so feof() still does not detect the end of the file. Until fgetc() is called again to perform the read operation, feof() cannot detect the end of the file.

Quoted from: https://baike.baidu.com/item/feof/10942186?fr=aladdin

So the improvement is as follows:

fgets(temp,1024,f_read);
    while(!feof(f_read))
    {
        printf("%s",temp);
        fgets(temp,1024,f_read);
        
    }

 

Guess you like

Origin blog.csdn.net/weixin_42596333/article/details/104521765