c++文件操作
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string>
int main01(void)
{
fputc('a', stdout);
system("pause");
char ch;
ch = fgetc(stdin);
return 0;
}
int main02()
{
FILE *fp = NULL;
fp = fopen("./03.txt","w+");
if (fp == NULL)
{
perror("fopen");
return -1;
}
if (fp!=NULL)
{
fclose(fp);
fp = NULL;
}
return 0;
}
void fput_c(char *path)
{
FILE *fp = NULL;
fp = fopen(path, "w+");
if (fp == NULL)
{
perror(" file fopen");
system("pause");
}
char buf[] = "kdjkajda";
if (fp != NULL)
{
for (int i = 0; i < strlen(buf); i++)
{
fputc( buf[i],fp);
}
fclose(fp);
}
}
void fget_c(char *path)
{
FILE *fp = NULL;
fp = fopen(path, "r+");
if (fp == NULL)
{
perror(" file fopen");
system("pause");
}
char ch;
while ((ch=fgetc(fp))!=EOF)
{
printf("%c", ch);
}
if (fp!=NULL)
{
fclose(fp);
fp = NULL;
}
}
void fput_s(char *path)
{
FILE *fp = NULL;
fp = fopen(path, "w+");
if (fp == NULL)
{
perror(" file fopen");
system("pause");
}
char *buf[] = { "\nkdjkajda\n","dagdgha\n","dahdajhd\n" };
if (fp != NULL)
{
for (int i = 0; i < 3; i++)
{
fputs(buf[i], fp);
}
fclose(fp);
}
}
void fget_s(char *path)
{
FILE *fp = NULL;
fp = fopen(path, "r+");
if (fp == NULL)
{
perror(" file fopen");
system("pause");
}
char ch[30];
while (!feof(fp))
{
char *p=fgets(ch,sizeof(ch),fp);
printf("%s", p);
}
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
}
int main03()
{
char *test_c = "./02.txt";
fput_c(test_c);
fget_c(test_c);
char *test_s = "./03.txt";
fput_s(test_s);
fget_s(test_s);
system("pause");
return 0;
}
typedef struct Stu
{
char name[30];
int age;
};
void fput_k(char *path)
{
FILE *fp = NULL;
fp = fopen(path, "w+");
if (fp == NULL)
{
perror(" file fopen");
system("pause");
}
Stu s[3];
char buf[50];
for (int i = 0; i < 3; i++)
{
sprintf(buf, "stu%d%d%d", i, i, i);
strcpy(s[i].name, buf);
s[i].age = i + 1;
}
fwrite(s,sizeof(Stu),3,fp);
fclose(fp);
}
void fget_k(char *path)
{
FILE *fp = NULL;
fp = fopen(path, "r+");
if (fp == NULL)
{
perror(" file fopen");
system("pause");
}
Stu s[3];
fread(s, sizeof(Stu), 3, fp);
for (int i = 0; i < 3;i++)
{
printf("%s %d\n", s[i].name, s[i].age);
}
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
}
int main04(void)
{
char *test_k = "./04.txt";
fput_k(test_k);
fget_k(test_k);
system("pause");
return 0;
}
void fput_g(char *path)
{
FILE *fp = NULL;
fp = fopen(path, "w+");
if (fp == NULL)
{
perror(" file fopen");
system("pause");
}
fprintf(fp, "hellow %d", 250);
fclose(fp);
}
void fget_g(char *path)
{
FILE *fp = NULL;
fp = fopen(path, "r+");
if (fp == NULL)
{
perror(" file fopen");
system("pause");
}
int a = 0;
fscanf(fp, "hellow %d", &a);
printf("%d",a);
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
}
int main05(void)
{
char *test_g = "./05.txt";
fput_g(test_g);
fget_g(test_g);
system("pause");
return 0;
}
void fget_bk(char *path)
{
FILE *fp = NULL;
fp = fopen(path, "r+");
if (fp == NULL)
{
perror(" file fopen");
system("pause");
}
Stu bmp;
fseek(fp, sizeof(Stu)* 2, SEEK_SET);
fread(&bmp, sizeof(Stu), 1, fp);
for (int i = 0; i < 1; i++)
{
printf("%s %d\n", bmp.name, bmp.age);
}
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
}
int main(void)
{
char *test_k = "./05.txt";
fput_k(test_k);
fget_bk(test_k);
system("pause");
return 0;
}