Some contents of file operation

Keywords: C C#

Close file

fclose(fp);

fp=NULL;

Open file

FILE* fp=fopen("xxx.xxx","xx");

The first parameter is the type of file set

The second parameter has the following

operationeffectfile does not exist
"w" (write only)To output data, open a text fileCreate a new file
"r" (read only)To output data, open an existing fileerror
"a" (added)Add data to the end of a text fileerror
"rb" (read only)To enter data, open a binary fileerror
"wb" (write only)To output data, open a binary fileCreate a new file
"ab" (additional)Add data to the end of a binary fileerror
"r +" (read / write)To read and write, open a text fileerror
"w +" (read / write)Create a new file for reading and writingCreate a new file
"a +" (read / write)Open a file and read and write at the end of the fileCreate a new file
"rb +" (read / write)To read and write, open a binary fileerror
"wb +" (sketch)To read and write, create a new text fileCreate a new file
"ab +" (read / write)Open a binary file and read and write at the end of the fileCreate a new file

The functions related to sequential file reading and writing are

functionFunction nameApply to
Character input functionfgetcAll input streams
Character output functionfputcAll output streams
Text line input functionfgetsAll input streams
Text line output functionfputsAll output streams
Format input functionfscanfAll input streams
Format output functionfprintfAll output streams
Binary inputfreadfile
Binary outputfwritefile

Keyboard -- standard input device ----- stdin

Screen -- standard output device ----- stdout

Use of fputc function

int main()
{
	FILE* fpwrite = fopen("text.txt", "w");
	if (fpwrite == NULL)
	{
		perror("Open failed");
		return;
	}
	fputc('H', fpwrite);
	fputc('e', fpwrite);
	fputc('l', fpwrite);
	fputc('l', fpwrite);
	fputc('o', fpwrite);


	fclose(fpwrite);
	fpwrite = NULL;

	return 0;
}

After running, you can see in the text.txt file

  Use of fgetc function

text.txt contains Hello

int main()
{
	FILE* fpread = fopen("text.txt", "r");
	if (fpread == NULL)
	{
		perror("Open failed");
		return;
	}
	printf("%c\n", fgetc(fpread));
	printf("%c\n", fgetc(fpread));
	printf("%c\n", fgetc(fpread));
	printf("%c\n", fgetc(fpread));
	printf("%c\n", fgetc(fpread));


	fclose(fpread);
	fpread = NULL;

	return 0;
}

After running, you can see it on the command console

Use of fputs function

int main()
{
	FILE* fpwrites = fopen("text1.txt", "w");
	if (fpwrites == NULL)
	{
		perror("Open failed");
		return;
	}
	fputs("Hello", fpwrites);
	fputs("World", fpwrites);

	fclose(fpwrites);
	fpwrites = NULL;
	return 0;
}

  Display in text1.txt file

Use of fgets function

int main()
{
	FILE* fpreads = fopen("text1.txt", "r");
	char tmp[100] = { 0 };
	if (fpreads == NULL)
	{
		perror("Open failed");
		return;
	}
	//Output 50 characters from fpreads to tmp
	fgets(tmp, 50, fpreads);
	//Output tmp
	printf("%s\n", tmp);
	fclose(fpreads);
	fpreads = NULL;
	return 0;
}

  Display in command desk

The fprintf function uses

int main()
{
	FILE* fpwrite = fopen("text2.txt", "w");
	if (fpwrite == NULL)
	{
		perror("Open failed");
		return;
	}

	fprintf(fpwrite, "%s %s %s %d", "Hello", "Word", "in", 2021);

	fclose(fpwrite);
	fpwrite = NULL;
	return 0;
}

  Display in text2.txt

  Use of fscanf function

int main()
{
	FILE* fpread = fopen("text2.txt", "r");
	char arr1[20] = { 0 };
	char arr2[20] = { 0 };
	char arr3[20] = { 0 };
	int year = 0;
	if (fpread == NULL)
	{
		perror("Open failed");
		return;
	}
	//Store the data in fpread in Arr1, arr2, arr3 and year
	fscanf(fpread, "%s %s %s %d", arr1, arr2, arr3, &year);
	printf("%s %s %s %d", arr1, arr2, arr3, year);
	fclose(fpread);
	fpread = NULL;
	return 0;
}

Display on command console

fwrit function uses

int main()
{
	int a = 10000;
	FILE* fpwrite = fopen("text3.txt", "wb");
	if (fpwrite == NULL)
	{
		perror("Open failed");
		return;
	}
	//Store a element in four bytes in fpwrite
	fwrite(&a, 4, 1, fpwrite);//Write to file in binary mode
	fclose(fpwrite);
	fpwrite = NULL;
	return 0;
}

  Open text3.txt in a binary editor

Using the fread function

int main()
{
	FILE* fpread = fopen("text3.txt", "rb");
	int a = 0;

	if (fpread == NULL)
	{
		perror("Open failed");
		return;
	}
	/* Read and display data */
	fread(&a, sizeof(a), 1, fpread);
	printf("%d\n", a);
	fclose(fpread);
	fpread = NULL;
	return(0);
}

  Display on command console

 

Posted by aliento on Tue, 16 Nov 2021 01:35:56 -0800