#include<stdio.h>
void io_c() {
FILE* fp1 = fopen("E:/1.txt", "r");
if (fp1 == 0) {
printf("文件无法打开\n");
return;
}
FILE* fp2 = fopen("E:/2.txt", "w");
char c;
while (!feof(fp1)) {
c = fgetc(fp1);
putchar(c);
fputc(c, fp2);
}
fclose(fp1);
fclose(fp2);
}
void io_str() {
char str[10];
FILE* fp1 = fopen("E:/1.txt", "r");
while (fgets(str, 10, fp1) != 0) {
printf("%s", str);
}
fclose(fp1);
}
void io_f() {
char str[10];
FILE* fp1 = fopen("E:/3.txt", "w");
fprintf(fp1, "%s %d", "我", 100);
fclose(fp1);
char str2[10];
int a;
FILE* fp2 = fopen("E:/3.txt", "r");
while (!feof(fp2)) {
fscanf(fp2, "%s %d", str2, &a);
printf("%s %d", str2, a);
}
fclose(fp2);
}
struct student {
char name[10];
int score;
}stu = {"我",100},stu2;
void io_rw() {
FILE* fp1 = fopen("E:/4.txt", "w");
fwrite(&stu, sizeof(struct student), 1, fp1);
fclose(fp1);
FILE* fp2 = fopen("E:/4.txt", "r");
while (!feof(fp2) && fread(&stu2, sizeof(struct student), 1, fp2)) {
printf("%s %d\n", stu2.name, stu2.score);
}
fclose(fp2);
}
void io_seek() {
FILE* fp1 = fopen("E:/1.txt", "r");
if (fp1 == 0) {
printf("文件无法打开\n");
return;
}
char c;
while (!feof(fp1)) {
c = fgetc(fp1);
putchar(c);
}
printf("\n指针位置: %d\n", ftell(fp1));
fseek(fp1, 5, SEEK_SET);
printf("\n指针移动后位置: %d\n", ftell(fp1));
while (!feof(fp1)) {
c = fgetc(fp1);
putchar(c);
}
fclose(fp1);
}
void io() {
io_seek();
}