File definition, file operation function, operation steps and precautions, etc


1. What is a file and its unique identification: three elements (path, file name, suffix)
File: a collection of data stored on external media
2. File operation function
2.1 fopen return value: two parameters of FILE pointer FILE * (the first parameter [FILE name] and the second parameter [how to open the FILE])
2.2 there is only one Fclose  parameter (released file stream pointer) 1. Prevent memory leakage  2. Errors may occur when opening again without releasing
2.3 fread ^ return value: returns the number of items actually read ^ four parameters (1. Where the data read from the file is stored in the memory, 2. The size of the read item, 3. The maximum number of items read, and 4. From which file)
If the returned value of fread is smaller than the size of the input buffer, it must be read; otherwise, it cannot be guaranteed to be read
2.4 fwrite {return value: returns the number of items actually written} four parameters (1. Data to be written to the file, 2. Size of items to be written, 3. Number of items to be written, 4. Where is the file to be written)
2.5 fseek# adjust the position of the cursor in the file # SEEK_SET  SEEK_CUR  SEEK_END
2.6 ftell , tell me the position of the cursor in the file , the return value is an integer value , representing how many bytes the cursor is from the beginning of the file
3. Data stream (input stream and output stream) standard input stream device: keyboard; standard output stream device: screen
3.1 text data stream (text file. TXT)
3.2 binary data stream (binary file song video picture executable file)
3.3 input stream (write from file to memory) output stream (write from memory to file)

4. Document operation steps: 1 Open file 2 Execute file operation function 3 Close file
5. File pointer
6. Absolute and relative paths of files

7. Read and write binary files and text files respectively
8. Precautions: if the fopen function mode is "w", if there is data in it, it will be cleared and opened again, resulting in data loss

int main()
{
	const char* path = "1.txt";//Relative path
	const char* path2 = "C:\\1.txt";//Absolute path
	FILE* fw = fopen(path,"w");
	//int a = 123456;
	const char* str = "123456";
	//fwrite(&a, sizeof(int), 1,fw);
	fwrite(str, sizeof(char), strlen(str), fw);
	fclose(fw);
	return 0;
}

 

//Write data from memory to a text file (output stream)
int main()
{
	const char* path = "xyz.txt";//Relative path
	FILE* fw= fopen(path, "w");//Open by writing
	const char* str = "hello world";
	fwrite(str, sizeof(char), strlen(str), fw);
	fclose(fw);
	return 0;
}
//Read data from a text file and write it to memory (input stream)
int main()
{
	const char* path = "xyz.txt";//Relative path
	FILE* fr = fopen(path, "r");//Read only permission open
	//You need to apply for a piece of memory to store the data read from the file
	char buff[100];
	int len=fread(buff, sizeof(char),100 , fr);
	//A cyclic method that reads characters one by one
	char cr;
	while (fread(buff, sizeof(char), 1, fr) > 0)//Read successfully: 1; Read failed: 0
	{
		printf("%c",cr);
	}
	printf("\n");
	fclose(fr);
	return 0;
}
//Write data from memory to binary file (output stream)
int main()
{
	FILE* fwb = fopen("abc.txt", "wb");//Open by writing
	int arr[] = { 12,23,34,45,56,67,78,89,90,100 };
	fwrite(arr,sizeof(arr), sizeof(arr)/sizeof(arr[0]),fwb);
	fclose(fwb);
	return 0;
}
//Read data from binary file and write it to memory (input stream)
int main()
{
	FILE* frb = fopen("abc.txt", "rb");//Read only permission open
	//You need to apply for a piece of memory to store the data read from the file
	int tmp;//Similar to the buff above
	while (fread(&tmp, sizeof(int), 1, frb) > 0)//Read successfully: 1; Read failed: 0
	{
		printf("%d", tmp);
	}
	printf("\n");
	fclose(frb);
	return 0;
}
//fseek
int main()
{
	const char* str = "www.txt";
		FILE*fw=fopen(str, "w");
		const char* str1 = "hello world";
		const char* str2 = "wwww,yyyyy";
		fwrite(str1, sizeof(char), strlen(str1), fw);
		fseek(fw,0,SEEK_SET);//Adjust the position of the parameter
		fseek(fw, -5, SEEK_END);//Adjust the position of parameters, take 5 steps to the left, and look_ End means to start from the end position
		fseek(fw, 8, SEEK_SET);//Adjust the position of the parameter, take 8 steps to the right from the starting position, and then click SEEK_SET indicates the start position
		fwrite(str2, sizeof(char), strlen(str2), fw);
		long flg = ftell(fw);//ftell: gets the current location of the file pointer
		printf("%d\n", flg);
		fseek(fw, -10, SEEK_CUR); //SEEK_CUR indicates the current position
		flg = ftell(fw);
		printf("%d\n", flg);
		fclose(fw);
	return 0;
}

Keywords: C

Added by hawk72500 on Thu, 30 Dec 2021 18:28:29 +0200