[Educoder homework] C&C++ file training

[Educoder homework] C&C++ file training

It took me a long time, mainly because I didn’t fully understand it at the time.

The difference between the entire file training and the previous one is that when dealing with problems, our data is stored in the file or we want to put the output in the file. So you only need to solve the problems of file input and file output. The four levels are equivalent to different methods, but they are similar.

T1 uses the FILE structure to operate text files

This question seems to be blocked in strange ways
We hope to write one G e t c h a r Getchar Getcha The r function represents reading a valid character from the file, so that subsequent processing is very simple. It should be noted that the end of the file is E O F EOF EOF

#include <stdio.h>
// 函数extractDigit的功能:从文件a.txt中提取数值写入文件b.txt中
void extractDigit();
// 请在此添加代码,实现extractDigit函数
/********** Begin *********/
char Getchar(FILE *f) {
    
    
	char c = fgetc(f);
	if (c == EOF) return EOF;
	while (c < '0' || c > '9') {
    
    
		c = fgetc(f);
		if (c == EOF) return EOF;
	}
	return c;
}

void extractDigit() {
    
    
	FILE *fi = fopen("a.txt", "r");
	FILE *fo = fopen("b.txt", "w");
	if (fi == NULL || fo == NULL) return;
	char c = Getchar(fi);
	int mdl = 0, cnt = 0;
	while (c != EOF) {
    
    
		(mdl *= 10) += (c - '0');
		cnt ++ ;
		if (cnt == 3) {
    
    
			// printf("%d\n", mdl);
			fprintf(fo, "%d ", mdl);
			mdl = 0, cnt = 0;
		}
		c = Getchar(fi);
	}
	if (cnt) fprintf(fo, "%d ", mdl);
	fclose(fi);
	fclose(fo);
}
/********** End **********/

T2 uses the FILE structure to operate binary files

This is the new reading method. Open the file in binary, so that you can use f r e a d fread according to the number of bits.freadComplete.

#include <stdio.h>

// 结构clothing
struct clothing {
    
    
    char label[12];  // 编号
    float price;  // 价格
};

/* 
   函数readClothing:从文件fp中读取服装信息到cloth中
   参数fp:指向打开的二进制文件,cloth:存放读出的服装信息
   返回值:服装信息数量
   说明:文件中首先以二进制方式存入了一个整数(服装信息数n),然后以二进制方式存入n种服装信息(clothing类型)
*/
int readClothing(FILE *fp, clothing cloth[])
{
    
    
    // 请在此添加代码,补全函数readClothing
    /********** Begin *********/
	int n;
	fread(&n, sizeof(n), 1, fp);
	fread(cloth, sizeof(clothing), n, fp);
    return n;
    
    /********** End **********/
}

T3 uses file streams to manipulate text files

This level introduces us to the flow method, which is very simple and convenient.
Note w h i l e while while's judgment condition is fulfilled.

#include <iostream>
#include <fstream>
using namespace std;

/*
  函数count:统计文件fin中每种服装的销售总额,并写入文件fout中
  参数fin:文件每种服装的销售情况,fout:每种服装销售总额的写入文件
  返回值:无
  说明:文件fin中,每种服装信息占一行,分别为服装编号,销售件数,每件的销售价格(整型)。
  文件fout:每种服装统计信息占一行,分别为服装编号,销售总额(整型),中间用一个空格隔开。
*/
void count(ifstream & fin, ofstream & fout)
{
    
    
    // 请在此添加代码,补全函数count
    /********** Begin *********/
	char s[12];
	fin >> s;
	while (!fin.eof()) {
    
    
		int m, sum = 0;
		fin >> m ;
		for (int j = 0; j < m; j ++ ) {
    
    
			int x; fin >> x ; sum += x;
		}
		fout << s << ' ' << sum << endl ;
		fin >> s ;
	}
    /********** End **********/
}

T4 uses file streams to manipulate binary files

The fourth level is to the third level what the second level is to the first level. It is equivalent to giving a binary method based on an input method. Bitwise input can be achieved, which will be useful in certain specific questions.

#include <fstream>
#include <string.h>
#include <iostream>
using namespace std;

// 结构clothing
struct clothing {
    
    
    char label[12];  // 编号
    int numberRemaining;  // 剩余件数
};

/*
  函数getNumber:在文件ifile中查找标签为lable的服装数量
  参数ifile:存放服装信息的文件,label:要查找的服装标签
  返回值:标签为label的服装数量
  说明:文件中ifile中存放着服装信息,服装信息为以二进制写入的一个个clothing结构变量
*/
int getNumber(ifstream &ifile, char *label)
{
    
    
    // 请在此添加代码,补全函数getNumber
    /********** Begin *********/
    clothing a;
		ifile.read((char*)&a, sizeof(clothing));
		while (!ifile.eof()) {
    
    
			if (strcmp(label, a.label) == 0) return a.numberRemaining;
			ifile.read((char*)&a, sizeof(clothing));
		}
		return 0;
    
    
    /********** End **********/
}

Guess you like

Origin blog.csdn.net/JZYshuraK/article/details/128444687