ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

chapter 8 文件

2021-11-22 01:02:58  阅读:164  来源: 互联网

标签:chapter 文件 输入输出 fout ans fopen fin


目录

大学C语言程序设计

chapter 8 文件

对于输入输出,一般我们分两种:

  1. 标准输入输出,也就是通过键盘和显示器控制台来输入输出。
  2. 文件输入输出,无需手动输入,程序会自动读写相关文件。

总的来讲文件操作形式有三种:
一、freopen文件重定向
二、fopen文件输入输出
三、fstream文件输入输出流
(部分竞赛不允许使用文件重定向,但是信息学竞赛专门考察文件重定向,可能是因为它更简单)。

1. freopen文件重定向

一、freopen文件重定向
 需要使用freopen函数,包含于标准库 <cstdio>中,使用格式如下:
    freopen ( filename, mode, stream);
译:freopen ( 文件名, 文件打开模式, 文件流);
例:
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
    //....
    fclose(stdin);    //关闭文件输入流
    fclose(stdout); //关闭文件输出流

重定向样例

#include<stdio.h>

int main(){
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);

    int a,b,ans;
    scanf("%d%d", &a, &b);
    ans = a+b;
    printf("%d", ans);

    fclose(stdin);
    fclose(stdout);
    return 0;
}

2. fopen文件输入输出

二、fopen
 需要使用fopen函数,包含于标准库 <cstdio>中,使用格式如下:
    FILE *file = fopen( filename, mode);
译:FILE *file = fopen( 文件名, 文件打开模式);
例:
    FILE *fin,*fout;
    fin = fopen("data.in", "rb");
    fout = fopen("data.out", "rw");
    //注意:使用fopen的读写方式为文件读写 fscanf/fprintf
    fclose(fin);   //关闭文件
    fclose(fout);  //关闭文件

 fopen改写为标准输入输出只需:fin=stdin; fout=stdout; 不需要fopen和fclose。

fopen样例

#include<stdio.h>

int main(){
    FILE *fin, *fout;
    fin = fopen("data.in", "rb");
    fout = fopen("data.out", "wb");

    int a,b,ans;
    fscanf(fin, "%d%d", &a, &b);
    ans = a+b;
    fprintf(fout, "%d\n", ans);

    fclose(fin);
    fclose(fout);
    return 0;
}

3. fopen标准输入输出

#include<stdio.h>

int main(){
    FILE *fin, *fout;
    fin = stdin;
    fout = stdout;

    int a,b,ans;
    fscanf(fin, "%d%d", &a, &b);
    ans = a+b;
    fprintf(fout, "%d\n", ans);

    //fclose(fin);//标准输入输出不需要fopen/fclose
    //fclose(fout);
    return 0;
}

4. fstream文件输入输出流

三、文件输入输出流
 需要使用流操作以及设定输入输出对象到那个文件,二类的定义在标准库 <fstream>中,所以需要导入该头文件。

例:用fin作为输入对象,fout作为输出对象,可以使用如下定义:

    ifstream fin("data.in");    //定义输入文件名
    ofstream fout("data.out");  //定义输出文件名

    fin.close();  //关闭文件
    fout.close(); //关闭文件

文件输入输出流样例

#include<fstream>
using namespace std;
int main(){
    ifstream fin("data.in");
    ofstream fout("data.out");

    int a,n,ans;
    fin>>a>>b;
    ans=a+b;
    fout<<ans<<endl;

    fin.close();
    fout.close();
    return 0;
}

5. scanf/printf and fscanf/fprintf and sscanf/sprintf

scanf/printf 表示从控制台输入输出
fscanf/fprintf 表示从文件输入输出
sscanf/sprintf 表示从字符串中输入输出(或者说赋值)给另外的字符串
当两者混用时,相互不影响,但是其作用都能表现出来。

#include<stdio.h>

int main(){
    FILE *fin, *fout;
    fin = fopen("data.in", "rb");
    fout = fopen("data.out", "wb");

    int a,b,ans;
    fscanf(fin, "%d%d", &a, &b);
//    scanf("%d%d", &a, &b);
    ans = a+b;
    fprintf(fout, "%d\n", ans);
    printf("%d\n", ans);

    fclose(fin);
    fclose(fout);
    return 0;
}
#include<stdio.h>
#include<string.h>

void fun1(){
    char buf[100], buf2[100], buf3[100];
    scanf("%s", buf);
    sscanf(buf, "%s", buf2);//从 buf中以格式 %s 读入赋给 buf2

    sprintf(buf3, "%s", buf2);//从 buf2中以格式 %s 读入赋给 buf3
    printf(" buf=%s\n buf2=%s\n buf3=%s\n",buf, buf2, buf3);
}

void fun2(){
    char ip[100]="192.168.1.1:8080", buf[100];
    int a,b,c,d,e;
    sscanf(ip, "%d.%d.%d.%d:%d", &a, &b, &c,&d, &e);
    sprintf(buf, "%d.%d.%d.%d:%d", a, b, c,d, e);
    printf("a=%d\n",a);
    printf("b=%d\n",b);
    printf("c=%d\n",c);
    printf("d=%d\n",d);
    printf("e=%d\n",e);
    printf("strcmp(ip,buf)=%d\n", strcmp(ip,buf));
}

void fun3(){
    char buf[]="`1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./";
    printf("%d\n", strlen(buf));
    for(int i=0; i<strlen(buf); i++){
        printf("%c", buf[i]);
    }printf("\n");

    int a;
    char buf2[100], buf3[100];
    sscanf(buf, "%*c%d%s", &a, buf2);// *表示跳过
    sprintf(buf3, "%d", a); //将 a的数据存入字符数组 buf3
    printf(" a=%d\n buf2=%s\n buf3=%s\n", a, buf2, buf3);
}

int main(){
    fun1();
    fun2();
    fun3();
    return 0;
}

标签:chapter,文件,输入输出,fout,ans,fopen,fin
来源: https://www.cnblogs.com/hellohebin/p/15456897.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有