Read and write TXT file notes 2

The last note wrote about the fopen function and the fclose function. In this note, we wrote the fgetc_fputc function, the fgets_fputs function, and the fread_fwrite function.

1. Read and write one character at a time

Function declaration:

int fgetc(FILE *stream);

Function description:

fgetc reads a byte from the file identified by stream and returns the byte value.

return value:

In the way of t: read the end of the file and return EOF (EOF is a symbolic constant defined in the stdio.h file, the value is -1)

In the way of b: read the end of the file, use feof (file pointer) to judge the end

feof is a C language standard library function. Its prototype is in stdio.h. Its function is to detect the end of file on the stream. If the file ends, it returns a non-zero value, otherwise it returns 0.

Function declaration:

int fputc(int c,FILE *stream)

Description of the function:

fputc writes the value of c to the file represented by stream.

return value:

If the output is successful, return the byte value of the output

Returns an EOF if the output fails

※※Note: When opening a file, the default read-write position is at the beginning of the file. If the read-write position is opened in the form of a at the end of the file, the read-write position will go to the file when reading or writing bytes into the file. The end offset, how many bytes to read, how many bytes the read and write position is offset to the end of the file.

example:

#include<stdio.h>

int main()
{
	FILE *fp1,*fp2;
	char ch;
	fp1=fopen("test.txt","r");
	if(fp1==NULL)
	{
		perror("fopen");
		return 0;
	}
	fp2=fopen("test2.txt","w");
	if(fp2==NULL)
	{
		perror("fopen");
		return 0;
	}
	while((ch=fgetc(fp1))!=EOF)
	{
		fputc(ch,stdout);	
		fputc(ch,fp2);
	}
	fclose(fp1);
	fclose(fp2);
	return 0;
}

Contents in test.txt file:

 The test2.txt file was originally an empty file. After executing the above program, the content of the test2.txt file:                                        

 Note: If there is no test2.txt file, an empty test2.txt file will be created first.

Second, read and write one string at a time

char *fgets(char *s,int size,FILE *stream);

Read characters from the file identified by stream, and stop reading when a newline is encountered or the end of the file is encountered, or size-1 bytes are read to stop reading, and the content is read. A '\0' will be added after it as the end of the string.

return value:

Successfully returns the first address of the array, that is, s.

Returns NULL on failure.

int fputs(const char *s,FILE *stream);

Function function:

Writes the string pointed to by s to the file represented by stream.

return value:

Returns the number of bytes written on success.

Returns -1 on failure.

example:

#include<stdio.h>

int main()
{
	FILE* fp_read,*fp_write;
	char str[100];
	fp_read=fopen("test.txt","r");
	if(fp_read==NULL)
	{
		perror("fopen");
		return 0;
	}
	fp_write=fopen("dest.txt","w");
	if(fp_write==NULL)
	{
		perror("fopen");
		return 0;
	}
	fgets(str,6,fp_read);
	printf("str=%s\n",str);
	fputs(str,fp_write);
	return 0;
}

Contents in test.txt file:

 The contents of the dest.txt file after executing the above program:

 If the 6 in fgets(str,6,fp_read) is changed to 16, the content in the dest.txt file: ​​​​​​​ ​​​​​​​ ​​​​​​​ ​​​​​​​ ​​​​​​​​​​​​​​​​​​​​ ​​​​​​

 The reason I only read ten characters is because there is a newline after helloworld.

Third, read the file fread

Function declaration:

size_t_fread(void *ptr,size_t size,size_t nmemb,FILE *stream);

Description of the function:

The fread function reads data from the file identified by stream, each block is size bytes, a total of nmemb blocks, and is stored in the memory pointed to by ptrh.

Return value: The actual number of blocks read.

example:

unsigned int num;

num=fread(str,100,3,fp);

The content read from the file represented by fp is stored in the memory pointed to by str, and the number of bytes read is 100 bytes per block, 3 blocks. Return value num:

If 300 bytes are read, the return value is 3

If the number of bytes read is greater than or equal to 200 and less than 300, the return value is 2

If the number of bytes read is greater than or equal to 100 and less than 200, the return value is 1

Returns 0 for less than 100 bytes.

Fourth, write the file fwrite

Function declaration:

size_t fwrite(void *ptr,size_t size,size_t nmemb,FILE *stream);

Description of the function:

The fwrite function writes the data in the memory pointed to by ptr to the file identified by stream, one block is size bytes, and a total of nmemb blocks.

Return value: The actual number of blocks written.

example:

#include<stdio.h>

struct stu
{
	char name[10];
	int num;
	int age;
 }stu1[2],stu2[2];
int main()
{
	FILE *fp;
	int i;
	fp=fopen("student.txt","wb+");//把文件当作二进制文件打开
	if(fp==NULL)
	{
		perror("fopen");
		return 0;
	}
	for(i=0;i<2;i++)
	{
		scanf("%s %d %d",stu1[i].name,&(stu1[i].num),&(stu1[i].age));
	}
	fwrite(stu1,sizeof(struct stu),2,fp); //将学生信息写入文件中
	rewind(fp); //文件指针经过写操作已经到了最后,需要复位
	fread(stu2,sizeof(struct stu),2,fp); //将文件中的数据读入内存中
	printf("%s %d %d\n",stu2[0].name,stu2[0].num,stu2[0].age);
	printf("%s %d %d\n",stu2[1].name,stu2[1].num,stu2[1].age);
	return 0;
}

※※Notice:

The fwrite function is to output the data in memory to the file as it is.

The fread function reads the data in the file into memory as it is. (read byte by byte)

Five, random read and write

Functions to complete file positioning: rewind, fseek functions

1.rewind reset read and write position

rewind function:  void rewind(file pointer);

Function: move the internal position pointer of the file to the beginning of the file

Call form: rewind(file pointer);

example:

fwrite(pa,sizeof(struct stu),2,fp);

rewind(fp);

fread(pb,sizeof(struct stu),fp);

2. ftell measures the number of bytes from the beginning of the file to the read and write position of the file

Define function: long ftell(file pointer);

Function: Get the current read and write position of the file stream

Return value: Returns the current read/write position (the number of bytes from the beginning of the file), or -1 in case of error.

example:

long int length;

length =ftell(fp);

3.fseek positioning position pointer (read and write position)

fseek function (generally used for binary files, that is, the way to open files requires b)

Function declaration:

int fseek(FILE *stream,long offset,int whence);

int fseek(file type pointer, displacement, starting point);

The offset is a positive number to offset to the end of the file, and a negative number to offset to the beginning of the file.

Function: Move the read and write position of the file stream.

whence starting position

start of file SEEK_SET 0
file current location SEEK_CUR 1
end of file SEEK_END 2

Guess you like

Origin blog.csdn.net/qq_61139806/article/details/124056466