• 文件操作


    1.过滤器(filter)

      逐行读取数据,对数据进行处理,再把数据写到某个地方。

      head:显示前几行

      tail:显示后几行

      sed:流编辑器,用来搜索和替换文本

     2.(在命令行中使用)

       < 重定向标准输入:从指定文件中读取数据

          >  重定向标准输出:输出到指定文件

      2> 重定向标准错误

    3. 在屏幕上显示程序结束后返回的数字

      Mac、Linux、Unix:   $ echo $

      Windows: C:>echo %ERRORLEVEL%

    4. printf("abc");  等价于 fprintf(stdout,"abc");

      stdout 标准输出数据流

      stdin   标准输入数据流

      stderr  标准错误

    5. | 表示管道,能连接一个进程的标准输入与另一个进程的标准输出

      例如:Bermuda | geo2json (Bermuda的输出变成geo2json的输入)

    6.main函数的又一形式

    main函数能以字符串的形式读取命令行参数,用argv[]储存数组,用argc的值记录数组中元素个数。

    注意:用户运行程序时,命令行中第一个参数是程序名,也就是说,第一个命令行参数其实是argv[1]

    int main(int argc,char *argv[])

    {

      ...做事情...

    }

     7.程序查错

    1 FILE *in = fopen("我不存在.txt","r");
    2 if (!(in = fopen("我不存在.txt","r")))
    3 {
    4     fprintf(stderr,"无法打开文件。
    ");
    5     rerurn 16 }

    练习一

     1 #include<stdio.h>
     2 int main()
     3 {
     4     float latitude;
     5     float longitude;
     6     char info[80];
     7     int started = 0;
     8     puts("data=[");
     9     while (scanf("%f,%f,%79[^
    ",&latitude,&longitude,info) == 3)//%79[^
     用于收集一行余下来的所有字符
    10         //scanf()返回成功读取的数据条数
    11     {
    12         if(started)
    13             printf(",
    ");
    14         else
    15             started = 1;
    16         printf("{latitude:%f,longitude:%f,info:'%s'}",latitude,longitude,info);
    17     }
    18     puts("
    ]");
    19     return 0;
    20 }

    练习二

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 int main()
     5 {
     6     char line[80];
     7     FILE *in = fopen("spooky.csv","r");//"w" 写(write) ; "r"  读(read) ; "a" 追加 (append)
     8     FILE *file1 = fopen("ufos.csv","w");
     9     FILE *file2 = fopen("disappearance.csv","w");
    10     FILE *file3 = fopen("other.csv","w");
    11 
    12     while(fscanf(in,"%79[^
    ]",line) == 1)
    13     {
    14         if(strstr(line,"UFO"))
    15             fprintf(file1,"%s
    ",line);
    16         else if(strstr(line,"dispearance"))
    17             fprintf(file2,"%s
    ",line);
    18         else
    19             fprintf(file3,"%s
    ",line);
    20     }
    21     fclose(file1);//要关闭打开的所有文件
    22     fclose(file2);
    23     fclose(file3);
    24     return 0;
    25 }

    练习三 main函数的又一用法

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 int main(int argc,char *argv[])
     5 {
     6     char line[80];
     7     
     8     if(argc != 6)
     9     {
    10         fprintf(stderr,"You need to give 5 arguments
    ");
    11         return 1;
    12     }
    13     FILE *in = fopen("spooky.csv","r");
    14     FILE *file1 = fopen(argv[2],"w");
    15     FILE *file2 = fopen(argv[4],"w");
    16     FILE *file3 = fopen(argv[5],"w");
    17 
    18  while(fscanf(in,"%79[^
    ]",line) == 1)
    19  {
    20      if(strstr(line,argv[1]))
    21          fprintf(file1,"%s
    ",line);
    22      else if(strstr(line,argv[3]))
    23          fprintf(file2,"%s
    ",line);
    24      else
    25          fprintf(file3,"%s
    ",line);
    26   }
    27 
    28     fclose(file1);
    29     fclose(file2);
    30     fclose(file3);
    31     return 0;
    32 }
  • 相关阅读:
    Unable to satisfy the following requirements解决方式
    零基础学python》(第二版)
    mysql 更新数据表的记录
    mysql创建数据库和删除数据库
    正则表达式
    python lambda函数详细解析(面试经常遇到)
    Linux 命令 统计进程数目
    Python 时间戳与时间字符串互相转
    python 安装配置(windows)
    linux 系统 tar 的用法详解
  • 原文地址:https://www.cnblogs.com/syyy/p/5686370.html
Copyright © 2020-2023  润新知