stdio.h

#include 
   
   
    
    
#include 
    
    
     
     

//fseek rewind fgetpos fsetpos
void movep();

/*
读文件
*/
void readfile();

/*
从控制台读取
*/
void readconsole();

/*
从字符串读取、写入字符串
将|符号换成&
*/
void readstring();

int main()
{
    movep();
    //readfile();
    //readconsole();
    //readstring();
    return 0;
}

void readfile()
{
    FILE *fp,*fp1;
    int ch;
    char buf[50];
    //文件重命名
    rename("aaddb.c","newname.c");
    //删除文件
    remove("client.o");
    //创建临时文件,程序退出时自动销毁
    fp = tmpfile();
    putc('n',fp);//向其中写入一个字符
    //读取文件getc
    fp1 = fopen("client.c","r");
    while((ch=getc(fp1)) != -1)
    {
        putc(ch,stdout);
    }
    fclose(fp1);
    //读取文件fgets
    fp1 = fopen("client.c","r");
    while(fgets(buf,50,fp1) != NULL)
    {
        fputs(buf,stdout);
    }
    fclose(fp1);
    //读写文件fread
    fp1 = fopen("client.c","r");
    fread(buf,sizeof(char),50,fp1);
    fwrite(buf,sizeof(char),50,stdout);
    fclose(fp1);
}

void readconsole()
{
    char str[12],ch,str2[12];
    //最基本的格式化读取
    printf("input 'hello world'\n");
    scanf("%[^\n]",str);//可以读取空格
    printf("%s\n",str);
    //读取字符
    printf("input 'h'\n");
    ch = getchar();//会读取回车键
    putchar(ch);//所以会换行不会产生其他输出
    putchar('\n');
    //读取字符串
    printf("input 'hello world'\n");
    gets(str2);
    puts(str2);
    puts("\n");
}

void readstring()
{
    char *str1,str2[22],arg1[4],arg2[3],arg3[11];
    str1 = "you | me | him or her";
    sscanf(str1,"%s | %s | %[^\n]",arg1,arg2,arg3);//从字符串中格式化读取,%[^\n]允许读入空格
    printf("%s %s %s\n",arg1,arg2,arg3);//中间结果
    sprintf(str2,"%s & %s & %s\n",arg1,arg2,arg3);//箱向字符串中格式化输出
    printf("%s\n",str2);//最终结果
}

/*
ftell获得当前文件指针位置
fseek设置指针偏移,SEEK_SET以开头为基准,SEEK_CUR以当前位置为准,SEEK_END以结尾为准
fsetpos与fgetpos为一对控制偏移函数
rewind将指针返回开头
*/
void movep()
{
    FILE * stream;
    fpos_t pos;
    stream = fopen("client.c","r");
    fseek (stream,5,SEEK_SET);
    printf("offset=%d\n", ftell (stream));
    rewind (stream);//将指针指向文件开头
     /* 取得指针位置并存入&pos所指向的对象 */
    fgetpos (stream,&pos);
    printf("offset=%d\n",pos);
    pos = 10;
    fsetpos (stream,&pos);
    printf("offset = %d\n", ftell(stream));
    fclose(stream);
}

    
    
   
   
#include #include struct DATA { int ID; char name[4]; char sex[3]; int score; }; void paixu(int*,DATA*,int); int sishewuru(double); void func1(int*,int*,DATA*,int*,int,int,int,int);//统计男女比例 int func2(int*,int,DATA*);//查找考生序号 void print(); void main() { int length=0,i,yiben,erben,sanben,dazhuan,male[4],female[4]; int yi,er,san,si; char input; FILE* file=fopen("f1.txt","r"),*file1; if(file==NULL) { printf("No such file!\n"); return; } while(EOF!=fscanf(file,"%*[^\n]\n")) length++;//自动计算考生数罝ATA* data=(DATA*)malloc(length*sizeof(DATA)); int* pai=(int*)malloc(length*sizeof(int)); rewind(file); for(i=0;i='0'&&input<='4')) { printf("非法输入,请重新输入\n请输入:"); fflush(stdin); } else break; } getchar(); switch(input) { case '0': printf("\n一类本科招生线:%d\n二类本科招生线:%d\三类本科招生线:%d\\n高职高专招生线:%d\n",yi,er,san,si); printf("是否打印为文件?(y/n):"); if(getchar()=='y') { file1=fopen("各批次录取分数线.txt","w"); fprintf(file1,"一类本科招生线:%d\n二类本科招生线:%d\\n三类本科招生线:%d\n高职高专招生线:%d\n",yi,er,san,si); fclose(file1); } fflush(stdin); break; case '1': func1(male,female,data,pai,yiben,erben,sanben,dazhuan); printf("一类本科招生线男女比例:%d:%d\n",male[0],female[0]); printf("二类本科招生线男女比例:%d:%d\n",male[1],female[1]); printf("三类本科招生线男女比例:%d:%d\n",male[2],female[2]); printf("高职高专招生线招生线男女比例:%d:%d\n",male[3],female[3]); printf("是否打印为文件?(y/n):"); if(getchar()=='y') { file1=fopen("各批次录取男女比例.txt","w"); fprintf(file1,"一类本科招生线男女比例:%d:%d\n",male[0],female[0]);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值