C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结。
C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结。
一、总结
C++/Php/Python/Shell 程序按行读取文件或者控制台(php读取标准输入:$fp = fopen("/dev/stdin", "r");)
1、php读取标准输入:$fp = fopen("/dev/stdin", "r");
二、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 程序按行读取文件或者控制台
写程序经常需要用到从文件或者标准输入中按行读取信息,这里汇总一下.方便使用 1. C++ 读取文件 #include<stdio.h> #include<string.h> i ...
- 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 ...
随机推荐
- 基于mybatis的BaseDao及BaseService深度结合(转)
原文地址:http://zhaoshijie.iteye.com/blog/2003209 关键字:Mybatis通用DAO设计封装(mybatis) 说明: mybatis默认分页机制为逻辑分页,所 ...
- 5.3.7 UserDict对象
用户自己定义字典类UserDict,它是封装了一个字典类dict.主要使用来拷贝一个字典的数据.而不是共享同一份数据. class collections.UserDict([initialdata] ...
- 第6章4节《MonkeyRunner源代码剖析》Monkey原理分析-事件源-事件源概览-翻译命令字串
在第2节中我们看到了MonkeySourceNetwork是怎样从Socket中获取MonkeyRunner发送过来的命令字串的,可是最后怎样将它翻译成事件的代码我们还没有进行分析,由于在那之前我们还 ...
- eclipse-12.04无图标,无法固定到侧边栏
今天下载了adt-bundle,但是eclipse打开以后,侧边栏显示的是一个问好,而且没办法固定到侧边栏,导致每次打开eclipse都要进入到相应目录,很麻烦.我们可以通过如下方法设置eclipse ...
- 97.TCP通信
运行截图: 客户端 创建通信套接字 //通信套接字,用于创建TCP连接 SOCKET socket_send; 创建tcp通信 //创建tcp通信 socket_send = socket(AF_IN ...
- VMware Windows安装详细过程(详细图解)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一.概论 简介:虚拟机(Virtual Machine)指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算 ...
- android 消息系统Handler、MessageQueue、Looper源代码学习
android消息系统 总体框架如图所看到的 在安卓的消息系统中,每一个线程有一个Looper,Looper中有一个MessageQueue,Handler向这个队列中投递Message,Looper ...
- 深入理解线程本地变量ThreadLocal
ThreadLocal理解: 假设在多线程并发环境中.一个可变对象涉及到共享与竞争,那么该可变对象就一定会涉及到线程间同步操作,这是多线程并发问题. 否则该可变对象将作为线程私有对象,可通过Threa ...
- (转)把Sublime Text 2 加入右键菜单(带图标),Edit with Sublime Text
转自 http://www.turen.me/archives/509 Sublime Text 2 是现在很受大家欢迎的编辑器了,不仅是在web前端,在书定简单的php.Js等代码时,也是相当的好用 ...
- eclipse编译器错误、警告设置
颜色配置步骤:Window->Preferences->General->Editors->Text Editors->Annotations