#include <stdio.h>
#include <string.h>
void writeFileByChar(const char* fileName)
{
FILE* fp = fopen(fileName, "a");
if (fp == NULL)
{
fp = fopen("1.sb", "w+");
}
char str[] = "ILoveyou,我喜欢你";
int i = 0;
while (str[i] != '\0')
{
fputc(str[i], fp);
i++;
}
fclose(fp);
}
void readFileByChar(const char* fileName)
{
FILE* read = fopen(fileName, "r");
char str[1024]="";
int i = 0;
while (!feof(read))
{
str[i++] = fgetc(read);
}
printf("文件中的内容是:\n%s\n", str);
fclose(read);
}
void writeFileByStr(const char* fileName, const char* strInfo)
{
FILE* write = fopen(fileName, "w");
fputs(strInfo, write);
fclose(write);
}
void readFileByStr(const char* fileName, char* array,int arrayNum)
{
FILE* read = fopen(fileName, "r");
fgets(array, arrayNum, read);
puts(array);
fclose(read);
}
struct MM
{
char name[20];
int age;
double num;
};
void writeFile(const char* fileName, struct MM* array, int arrayNum)
{
FILE* write = fopen(fileName, "w");
for (int i = 0; i < arrayNum; i++)
{
fprintf(write, "%s\t%d\t%.1lf\n", array[i].name, array[i].age, array[i].num);
}
fclose(write);
}
void readFile(const char* fileName, struct MM* array)
{
FILE* read = fopen(fileName, "r");
int i = 0;
while (fscanf(read, "%s\t%d\t%lf\n", array[i].name, &array[i].age, &array[i].num) != EOF)
{
printf("%s\t%d\t%.1lf\n", array[i].name, array[i].age, array[i].num);
i++;
}
fclose(read);
}
int main()
{
readFileByChar("1.sb");
writeFileByStr("2.txt", "ILoveyou");
char str[1024] = "";
readFileByStr("2.txt", str, 1024);
puts(str);
struct MM array[3] = { "张三",18,1001,"李四",29,1002,"王五",32,1004 };
writeFile("mmInfo.txt",array, 3);
struct MM readArray[3];
readFile("mmInfo.txt", readArray);
return 0;
}