C++学习:第二章C语言基础 - (十八)文件、格式控制

1. 主要的几个函数

scanf         从键盘读取

printf        输出到屏幕

sscanf       从字符串读取

sprint        输出到字符串

fscanf       从文件中读取

fscanf       输出到文件

  • printf、sprintf与fprintf 的用法区分, scanf 的同理
  1. printf 是和标准输出文件(stdout)关联的,fprintf 则没有这个限制.
  2. fprintf是用于文件操作的,原型是int fprintf( FILE *stream, const char *format [, argument ]...);
  3. sprintf是格式化输出到一个字符串,fprintf是格式化输出到一个stream,通常是到文件。

2. 关于IO缓冲区

IO 输出有缓冲区,会先到缓冲区在到屏幕上

遇见输入请求时输出的东西会马上到屏幕上:键盘输入,scanf等

遇见换行符时。。。

程序结束时。。。。。

缓冲区满的时候。。。

  • fflush(stdout)//刷新标准缓冲区输出后会马上输出

3. sscanf 和 sprint

界面到内存与内存到界面的过程中都有一个标准格式化的一个步骤在里面

因为在界面显示的是字符串,而在内存中保存的是数据,因此需要转换

平时转换的两个函数是scanf 和 printf

引深到对字符串的处理 sscanf 和 sprint

#include <stdio.h>

int main(){
    char buf[100];
    char s[100];
    int n;
    
    sprintf(buf, "hello %d", 100);
    puts(buf);
    sscanf(buf, "%s%d", &s, &n);
    printf("s = %s;  n = %d\n", s, n);

    getchar();
    return 0;
}

4. fscanf 和fprintf

fprintf fscanf 从文件中读写,C中对应的预定义好的文件 stdin stdout stderr (都是FILE*类型)

标准输入输出可以重定向,但是stderr不可以,但是stderr没有输出缓冲,stdout有输出缓冲

此代码只在LInux上跑通
——————————————————————————————————————————————————
#include <stdio.h>

int main(int argc, char** argv) {
    int n;
    double d;
    char str[100];

    fprintf(stderr, "请输入名字信息 \n");
    fscanf(stdin, "%s%d%lf", str, &n, &d);
    fprintf(stdout, "%s, %d, %g \n", str, n, d);

    return 0;
}

5. fopen 和 fclose 

打开自己的文件

fopen 返回一个FILE* ,一旦打开文件成功,系统就会分配一些资源来缓存部分文件,也就是说一旦文件打开就会占用系统资源,所以不用了就需要释放资源( fclose(FILE*) )

打开参数 (r w a r+ w+ a+)读 写 追加 读写 读写(文件不存在会创建该文件) 追加读写

fclose :文件一定要指定关闭,因为可能有部分数据在缓存中,如果不关闭会丢失这部分数据

此代码只在LInux上跑通
————————————————————————————————————
#include <stdio.h> 

FILE *stream;

void main(void){
    int i = 10;
    double fp = 1.5;
    char oo = ' ';
    char s[100] = "this is a string\n";

    FILE* stream = fopen("fprintf", "w+");
    fprintf(stream, "%c%s",oo,  s);
    fprintf(stream, "%d\n", i);
    fprintf(stream, "%f\n", fp);
    fclose(stream);
}

用VS2015编译时使用的fopen_s、fprintf_s

#include <stdio.h> 

FILE *stream;

void main(void){
    int i = 10;
    double fp = 1.5;
    char s[] = "this is a string\n";
    char a = '\0';

    fopen_s(&stream,"a.txt", "w+");
    fprintf_s(stream, "%c", a);
    fprintf_s(stream, "dddddd\n");
    fprintf_s(stream, "%s", s);

    fprintf(stream, "%d\n", i);
    fprintf(stream, "%f\n", fp);
    fclose(stream);

}

6. 格式控制符

%宽度d

%0宽度d  高位不够位数以0补齐

%宽度.小数位数f

%-宽度d 靠左

%+宽度d 输出正好

%宽度.字符数s 限定字符数

#include <stdio.h>

void main(void){

    int n = 123;
    printf("%d\n", n);
    printf("%5d\n", n);
    printf("%-d\n", n);
    printf("%-5d\n", n);
    printf("%0d\n", n);
    printf("%05d\n", n);
    printf("%+5d\n", n);

    double d = 1.23;
    printf("%f\n", d);
    printf("%5f\n", d);
    printf("%5.3f\n", d);
    printf("%06.3f\n", d);//注意,这里小数点占宽度
    printf("%+5.3f\n", d);
    printf("%.3f\n", d);
    int i = 3;
    printf("%0*d", i, i);//注意,这里第一个i的占位符是*,第二个i的占位符才是%
    
    getchar();
}

7. 一个文件加密解密的代码

feof使用

#include <stdio.h> 

void main(void){

    FILE* file, *file2, *file3;

    fopen_s(&file, "file.mv", "w+"); 
    fprintf(file, "asdfghjkl\ndalkdjlkasd\nksldkfajlksdjla\nlaskdjlajslkd\nsldkjaslkdf");
    fclose(file);

    //加密
    fopen_s(&file, "file.mv", "r");
    fopen_s(&file2, "file2.mv", "w+");
    char ch;
    while (!feof(file)){
        ch = fgetc(file);   //每次读取一个字符
        if ((int)ch != -1 && (int)ch != 0) {//ch!=EOF
            ch = ~ch; //取反;
            fputc(ch, file2); //写入到临时文件 
        }
    }
    fclose(file);
    fclose(file2);

    //解密
    fopen_s(&file2, "file2.mv", "r");
    fopen_s(&file3, "file3.mv", "w+");
    while (!feof(file2)){
        ch = fgetc(file2);   //每次读取一个字符 
        if ((int)ch != -1 && (int)ch != 0) {
            ch = ~ch; //取反;
            fputc(ch, file3); //写入到临时文件
        }
    }
    fclose(file2);
    fclose(file3);

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值