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

1. C++

读取文件

 #include<stdio.h>
#include<string.h> int main(){
const char* in_file = "input_file_name";
const char* out_file = "output_file_name"; FILE *p_in = fopen(in_file, "r");
if(!p_in){
printf("open file %s failed!!!", in_file);
return -;
} FILE *p_out = fopen(out_file, "w");
if(!p_in){
printf("open file %s failed!!!", out_file);
if(!p_in){
fclose(p_in);
}
return -;
} char buf[];
//按行读取文件内容
while(fgets(buf, sizeof(buf), p_in) != NULL) {
//写入到文件
fwrite(buf, sizeof(char), strlen(buf), p_out);
} fclose(p_in);
fclose(p_out);
return ;
}

读取标准输入

 #include<stdio.h>

 int main(){
char buf[]; gets(buf);
printf("%s\n", buf); return ;
} /// scanf 遇到空格等字符会结束
/// gets 遇到换行符结束

2. Php

读取文件

 <?php
$filename = "input_file_name"; $fp = fopen($filename, "r");
if(!$fp){
echo "open file $filename failed\n";
exit(1);
}
else{
while(!feof($fp)){
//fgets(file,length) 不指定长度默认为1024字节
$buf = fgets($fp); $buf = trim($buf);
if(empty($buf)){
continue;
}
else{
echo $buf."\n";
}
}
fclose($fp);
}
?>

读取标准输入

 <?php
$fp = fopen("/dev/stdin", "r"); while($input = fgets($fp, 10000)){
$input = trim($input);
echo $input."\n";
} fclose($fp);
?>

3. Python

读取文件

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

读取标准输入

 #coding=utf-8

 # 如果要在python2的py文件里面写中文,则必须要添加一行声明文件编码的注释,否则python2会默认使用ASCII编码。
# 编码申明,写在第一行就好
import sys input = sys.stdin for i in input:
#i表示当前的输入行 i = i.strip()
print i input.close()

4. Shell

读取文件

 #!/bin/bash

 #读取文件, 则直接使用文件名; 读取控制台, 则使用/dev/stdin

 while read line
do
echo ${line}
done < filename

读取标准输入

 #!/bin/bash

 while read line
do
echo ${line}
done < /dev/stdin

C++/Php/Python/Shell 程序按行读取文件或者控制台的更多相关文章

  1. C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结。

    C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结. 一.总结 C++/Php/Python/Shell 程序按行读取文件或者控制台(php读取标准输入:$fp = fope ...

  2. Python跳过第一行读取文件内容

    Python编程时,经常需要跳过第一行读取文件内容.比较容易想到是为每行设置一个line_num,然后判断line_num是否为1,如果不等于1,则进行读取操作.相应的Python代码如下: inpu ...

  3. 【Shell】按行读取文件内容

    方法1:while循环中执行效率最高,最常用的方法. function while_read_LINE_bottm(){ While read LINE do echo $LINE done < ...

  4. [转]Python跳过第一行读取文件内容

    from itertools import islice file_name='XXXX' input_file = open(file_name) for line in islice(input_ ...

  5. python 逆序按行读取文件

    How to read a file in reverse order? import os def readlines_reverse(filename): with open(filename) ...

  6. python 按每行读取文件怎么去掉换行符

    python按每行读取文件后,会在每行末尾带上换行符,这样非常不方便后续业务处理逻辑,需要去掉每行的换行符,怎么去掉呢?看下面的案例: >>> a = "hello wor ...

  7. Shell按行读取文件的3种方法

    Shell按行读取文件的方法有很多,常见的三种方法如下: 要读取的文件: [root@mini05 -]# cat file.info 写法一: [root@mini05 -]# cat read1. ...

  8. Python按行读取文件、写文件

    Python按行读取文件 学习了:https://www.cnblogs.com/scse11061160/p/5605190.html file = open("sample.txt&qu ...

  9. shell脚本,按行读取文件的几种方法。

    第一种方法用while实现按读取文件.[root@localhost wyb]# cat a.txt 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 e ...

随机推荐

  1. document.referrer 特性

    最近需要用到document.referrer,但是在测试的时候,总是获取为空,百思不得其解. 于是发动百度,看了大量的文章没有一个说到点子上是为什么,后来偶然看到document.referrer ...

  2. zlog使用

    1.使用buildroot编译zlog,在工具链的系统根目录下会生成动态库和静态库,还添加了头文件. 2.把动态库拷贝到开发板文件系统(该文件系统可以不是由buildroot编译得到,而是板上自带的) ...

  3. button 边框

    [_saveButton.layer setMasksToBounds:YES]; [_saveButton.layer setCornerRadius:8.0]; //设置矩圆角半径 [_saveB ...

  4. c# (nop中)下拉列表(有外键)

    第一种情况.view视图加载出来时就有值,实现步骤如下 1.在操作的界面Model中建立public List<SelectListItem> xxx(取名){ get; set; } 2 ...

  5. html5新特性之音频、视频

    1.视频 标签video video标签的属性 属性 描述 autoplay 视频就绪后自动播放 preload 视频在页面加载时加载 loop 视频播放完成后循环播放 controls 显示控件 s ...

  6. Apache+PHP配置运行环境(getenv的使用)

    在开发与上线等多个环境下,常量的配置一般不同,例如开发环境和生产环境的一些域名肯定不一样,为了保证代码上线就能运行,要求在代码运行开始的时候对不同的环境区分这些常规变量. 找到Apache目录下虚拟主 ...

  7. 【Visual Lisp】变体与安全数组

    (vlax-make-variant) ;;创建一个未初始化的变体 ;;01.整型值变体(setq myvar (vlax-make-variant 10)) ;;创建整型值变体,返回 #<va ...

  8. tomee 第一个 远程调用 Message-driven bean(MDB)

    MDB 整体结构 HelloMDB.java package cn.zno; import javax.jms.Connection; import javax.jms.ConnectionFacto ...

  9. Model层数据验证

    问题1:View层如何向Controller的Action传递Model数据?在View中,可以使用Form表单进行模型数据的提交,同样的,我们需要关联提交数据的类型,则需要在View中使用@mode ...

  10. 5.4 String

    JAVA 中为什么String 是immutable的? 1.设计:当创建一个String(String str2 = "abc"), 如果它(原先有一个String str = ...