文件学习

#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;
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值