zoukankan      html  css  js  c++  java
  • C++/Php/Python/Shell 程序按行读取文件或者控制台

    写程序经常需要用到从文件或者标准输入中按行读取信息,这里汇总一下。方便使用

    1. C++

     读取文件

     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 int main(){
     5     const char* in_file = "input_file_name";
     6     const char* out_file = "output_file_name";
     7 
     8     FILE *p_in = fopen(in_file, "r");
     9     if(!p_in){
    10         printf("open file %s failed!!!", in_file);
    11         return -1;
    12     }
    13         
    14     FILE *p_out = fopen(out_file, "w");
    15     if(!p_in){
    16         printf("open file %s failed!!!", out_file);
    17         if(!p_in){
    18             fclose(p_in);
    19         }
    20         return -1;
    21     }
    22 
    23     char buf[2048];
    24     //按行读取文件内容
    25     while(fgets(buf, sizeof(buf), p_in) != NULL) {
    26         //写入到文件
    27         fwrite(buf, sizeof(char), strlen(buf), p_out);
    28     }
    29 
    30     fclose(p_in);
    31     fclose(p_out);
    32     return 0;
    33 }
    View Code

    读取标准输入

     1 #include<stdio.h>
     2 
     3 int main(){
     4     char buf[2048];
     5 
     6     gets(buf);
     7     printf("%s
    ", buf);
     8 
     9     return 0;
    10 }
    11 
    12 /// scanf 遇到空格等字符会结束
    13 /// gets 遇到换行符结束
    View Code

    2. Php

    读取文件

     1 <?php
     2 $filename = "input_file_name";
     3 
     4 $fp = fopen($filename, "r");
     5 if(!$fp){
     6     echo "open file $filename failed
    ";
     7     exit(1);
     8 }
     9 else{
    10     while(!feof($fp)){
    11         //fgets(file,length) 不指定长度默认为1024字节
    12         $buf = fgets($fp);
    13 
    14         $buf = trim($buf);
    15         if(empty($buf)){
    16             continue;
    17         }
    18         else{
    19             echo $buf."
    ";
    20         }
    21     }
    22     fclose($fp);
    23 }
    24 ?>
    View Code

    读取标准输入 

     1 <?php
     2 $fp = fopen("/dev/stdin", "r");
     3 
     4 while($input = fgets($fp, 10000)){
     5         $input = trim($input);
     6        echo $input."
    ";
     7 }
     8 
     9 fclose($fp);
    10 ?>
    View Code

     3. Python

    读取文件

    1 file = open("read.py", "r")
    2 while 1:
    3     line = file.readline()
    4     if not line:
    5         break
    6     #line = line
    7     line = line.strip() ##移除字符串头尾指定的字符(默认为空格)
    8     print line
    View Code

    读取标准输入

     1 #coding=utf-8
     2 
     3 # 如果要在python2的py文件里面写中文,则必须要添加一行声明文件编码的注释,否则python2会默认使用ASCII编码。
     4 # 编码申明,写在第一行就好 
     5 import sys
     6 
     7 input = sys.stdin
     8 
     9 for i in input:
    10     #i表示当前的输入行
    11 
    12     i = i.strip()
    13     print i
    14 
    15 input.close()
    View Code

     4. Shell

    读取文件

    1 #!/bin/bash
    2 
    3 #读取文件, 则直接使用文件名; 读取控制台, 则使用/dev/stdin
    4 
    5 while read line
    6 do
    7     echo ${line}
    8 done < filename
    View Code

    读取标准输入

    1 #!/bin/bash
    2 
    3 while read line
    4 do
    5     echo ${line}
    6 done < /dev/stdin
    View Code
  • 相关阅读:
    matlablib安装Helvetica字体
    word插入矢量图的方法
    字体对应关系
    MySQL8.0.15基于mycat读写分离(windows环境)
    2.MySQL8.0.15主从同步(windows环境)
    1.MySQL8.0.15安装步骤(windows环境)
    mysql自带的压力测试工具
    FreeFileSync + Windows任务计划程序 实现自动备份功能
    FreeFileSync 设置服务器文件自动备份,同时可做本地代码程序同步软件
    dokcer镜像添加ssh服务
  • 原文地址:https://www.cnblogs.com/xudong-bupt/p/5423865.html
Copyright © 2011-2022 走看看