#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
FILE *fp,*hp;
fp = fopen("c1.txt","r+");//打开文件
if(fp==NULL)
{
cout<<"0"<<endl; //不成功
}
else
cout<<"1"<<endl; //成功
char ch;
hp = fopen("c2.txt","w+");
for(int i=0;i<4;i++)
{
ch = fgetc(fp); //读字符
cout<<ch;
fputc('1',fp);//写字符
}
cout<<endl;
fclose(fp);
fp=fopen("c1.txt","r+");
char st[20];
fputs("12345",fp);//写入字符串
fp=fopen("c1.txt","r+");
fgets(st,5,fp);//读出字符串
cout<<st<<endl;
if(fclose(fp)==0)//关闭文件
{
cout<<"1"<<endl; //成功
}
return 0;
}
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define N 100
typedef struct student{
int no;
char name[10];
double score;
} STUDENT;
int main()
{
STUDENT stud[N];
FILE *fpin,*fpout;
int n,i;
fpin = fopen("data1.in","r");
fscanf(fpin,"%d",&n);
for(i=0;i<n;i++)
{
fscanf(fpin,"%d%s%lf",&stud[i].no,stud[i].name,&stud[i].score); //读
}
//sort(stud,n);
fpout = fopen("data.out","w");
for(i=0;i<n;i++)
{
fprintf(fpout,"%d %s %.2lf\n",stud[i].no,stud[i].name,stud[i].score);//写
}
fclose(fpin);
fclose(fpout);
return 0;
}
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
FILE *fp,*hp;
int n;
fp = fopen("c1.txt","r+");//打开文件
fseek(fp,3*sizeof(char),0); //移动光标
char ch;
ch = fgetc(fp);
cout<<ch<<endl;
n=ftell(fp); //返回光标位置
cout<<n<<endl;
rewind(fp); //将光标移到头
ch = fgetc(fp);
cout<<ch<<endl;
n=ftell(fp);
cout<<n<<endl;
fclose(fp);
return 0;
}