文件操作

C语言文件操作

Talk is easy, Show you code!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
void writeConfig();
void writeQues(char[], int, int);
void readConfig(char[]);
int main()
{
	FILE *Config;
	int how;
	char set[64];
	char path[] = { "/mnt/sdcard/config.txt" };
	Config = fopen(path, "r");
	if (Config != NULL)
	{
		printf("读取配置文件成功!\n配置文件内容如下:\n");
		printf("使用该配置请输入1,重新设置请输入2\n");
		scanf("%d", &how);
		fclose(Config);
	}
	else
	{
		printf
			("读取文件失败!\n您可能是第一次运行本软件\n请输入某些程序需要的参数\n");
		fclose(Config);
		writeConfig();
	}
	if (how == 1)
	{
		printf("使用已经存在的配置文件生成题目中\n");
		// writeQues(path);
	}
	else if (how == 2)
	{
		writeConfig();
	}
	printf("程序结束");
	return 0;
}

void writeConfig()
{




}

void writeQues(char sign[], int a, int b)
{

}

readConfig(char path[], char *res)
{
	FILE *Cf;
	char sign[32] = { 0 };
	char rand[16] = { 0 };
	int i;
	Cf = fopen(path, "w+");
	if (Cf == NULL)
		printf("文件打开失败\n");
	else
	{
		for (i = 1; i < 5; i++)
		{
			if (i == 2)
				fgets(sign, 16, Cf);
			if (i == 4)
				fgets(rand, 16, Cf);
		}
	}
	strcpy(sign, rand);
	strcpy(res, sign);

}
updatedupdated2020-05-032020-05-03