C++/Php/Python/Shell 程序按行读取文件或者控制台
写程序经常需要用到从文件或者标准输入中按行读取信息,这里汇总一下。方便使用
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 程序按行读取文件或者控制台的更多相关文章
- C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结。
C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结. 一.总结 C++/Php/Python/Shell 程序按行读取文件或者控制台(php读取标准输入:$fp = fope ...
- Python跳过第一行读取文件内容
Python编程时,经常需要跳过第一行读取文件内容.比较容易想到是为每行设置一个line_num,然后判断line_num是否为1,如果不等于1,则进行读取操作.相应的Python代码如下: inpu ...
- 【Shell】按行读取文件内容
方法1:while循环中执行效率最高,最常用的方法. function while_read_LINE_bottm(){ While read LINE do echo $LINE done < ...
- [转]Python跳过第一行读取文件内容
from itertools import islice file_name='XXXX' input_file = open(file_name) for line in islice(input_ ...
- python 逆序按行读取文件
How to read a file in reverse order? import os def readlines_reverse(filename): with open(filename) ...
- python 按每行读取文件怎么去掉换行符
python按每行读取文件后,会在每行末尾带上换行符,这样非常不方便后续业务处理逻辑,需要去掉每行的换行符,怎么去掉呢?看下面的案例: >>> a = "hello wor ...
- Shell按行读取文件的3种方法
Shell按行读取文件的方法有很多,常见的三种方法如下: 要读取的文件: [root@mini05 -]# cat file.info 写法一: [root@mini05 -]# cat read1. ...
- Python按行读取文件、写文件
Python按行读取文件 学习了:https://www.cnblogs.com/scse11061160/p/5605190.html file = open("sample.txt&qu ...
- shell脚本,按行读取文件的几种方法。
第一种方法用while实现按读取文件.[root@localhost wyb]# cat a.txt 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 e ...
随机推荐
- Hibernate使用count(*)取得表中记录总数
/** * @TODO:查询某一年度的所有计划数量 */ public int findCountByYear(String currYear) { String hqlString = " ...
- DBHelper
DBHelper: using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...
- “FAIL - Deployed application at context path but context failed to start”错误的解决
Netbeans调试错误,出现以下信息,无法启动浏览器调试. Attached JPDA debugger to localhost:tomcat_shared_memory_id 正在取消部署... ...
- 10个经典的C语言面试基础算法及代码
10个经典的C语言面试基础算法及代码作者:码农网 – 小峰 原文地址:http://www.codeceo.com/article/10-c-interview-algorithm.html 算法是一 ...
- 安装python 的 包 paramiko
安装python 的 包 paramiko 安装 依赖 yum -y install gcc python-devel 获取安装 pycryptowget https://pypi.python.or ...
- codeforce 359D 二分+ 动态规划(sparse table)
原题链接:http://codeforces.com/problemset/problem/359/D 思路:首先对符合题目的长度(r-l)从0到n-1进行二分查找,对每一个长度进行check,看是否 ...
- 打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数 其各位数字立方和等于该数本身。 例如:153是一个 "水仙花数 " 因为153=1*1*1+5*5*5+3*3*3
for (int i = 100; i <= 999; i++) { int geWei, shiWei, baiWei; baiWei = i / 100; shiWei = (i - bai ...
- xps 文件操作笔记
1. 在 Silverlight 显示XPS文件,参考:http://azharthegreat.codeplex.com/ 2. Word,Excel, PPT 文件转换为XPS: 参考一(老外写的 ...
- Android编译错误, Ignoring InnerClasses attribute for an anonymous inner class
今天在做android项目时,加入第三方包,一编译就报错.错误如下:[2012-01-13 14:51:25 - xxx] Dx warning: Ignoring InnerClasses attr ...
- hadoop2.X使用手册1:通过web端口查看主节点、slave1节点及集群运行状态
导读内容:1.如何通过web查看hdfs集群状态2.如何通过web查看运行在在主节点master上ResourceManager状态3.如何通过web查看运行在在slave节点NodeManager资 ...