zoukankan      html  css  js  c++  java
  • c读写文件

    #include<stdio.h>
    
    void main(void)
    {
        // locate
        char filename[20];
        scanf("%s", filename);
        getchar();    // input a key "Enter('
    ')"
    
        // open
        FILE *fp;
        fp = fopen(filename, "w");
        if(fp != NULL)
            printf("fopen: success
    ");
        else
            printf("fopen: error
    ");
    
        // operate
        char c;
    
        printf("input:
    ");
        while((c = getchar()) != EOF)
            fputc(c , fp);
    
        // close
        if(fclose(fp) == 0)
            printf("fclose: success
    ");
        else
            printf("fclose: error
    ");
    }
    #include<stdio.h>
    
    void main(void)
    {
        // locate
        char filename[20];
        scanf("%s", filename);
        getchar();    // input a key "Enter('
    ')"
    
        // open
        FILE *fp;
        fp = fopen(filename, "w");
    
        // operate
        char c;
    
        while((c = getchar()) != EOF)
            fputc(c , fp);
    
        // close
        if(fclose(fp) == 0)
            printf("fclose: success
    ");
        else
            printf("fclose: error
    ");
    }
    #include<stdio.h>
    
    void main(void)
    {
        // flocate
        char filename[20];
        gets(filename);
    
        // fopen
        FILE *fp;
        fp = fopen(filename, "w");
    
        // foperate
        char c;
    
        while((c = getchar()) != EOF)
            fputc(c , fp);
    
        // fclose
        fclose(fp);
    }

    gets(filename): 不需要再读掉一个空格,该函数本身是以多读一个' '作为结束标识的

    scanf("%s", filename): 需要再读掉一个空格

  • 相关阅读:
    Java 端口转发
    Tomcat笔记
    RocketMQ开启ACL后客户端连接报Algorithm HmacSHA1 not available的解决方式
    RSA签名与验签
    小米9升级MIUI11
    【转】linux awk命令详解
    进程和线程
    Jenkins笔记
    【转】Jenkins启动、停止脚本
    UiAutomator源码分析
  • 原文地址:https://www.cnblogs.com/xkxf/p/6201403.html
Copyright © 2011-2022 走看看