chapter 8 文件

2021/11/22 6:10:07

本文主要是介绍chapter 8 文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录
  • 大学C语言程序设计
    • chapter 8 文件
      • 1. freopen文件重定向
      • 2. fopen文件输入输出
      • 3. fopen标准输入输出
      • 4. fstream文件输入输出流
      • 5. scanf/printf and fscanf/fprintf and sscanf/sprintf

大学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 8 文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程