当前位置: 代码迷 >> 综合 >> 文件学习
  详细解决方案

文件学习

热度:44   发布时间:2024-01-14 22:31:34.0
#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; //不成功}elsecout<<"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;
}


 

  相关解决方案