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++
读取文件
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\n", 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\n"; 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."\n"; 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."\n"; 7 } 8 9 fclose($fp); 10 ?>
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
转载于:https://www.cnblogs.com/Renyi-Fan/p/9099520.html
C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结。相关推荐
- python读取大文件的某行_Python按行读取文件的实现方法【小文件和大文件读取】...
本文实例讲述了Python按行读取文件的实现方法.分享给大家供大家参考,具体如下: 小文件: #coding=utf-8 #author: walker #date: 2013-12-30 #func ...
- python按行读取文件效率高吗_Python按行读取文件的实现方法【小文件和大文件读取】...
本文实例讲述了Python按行读取文件的实现方法.分享给大家供大家参考,具体如下: 小文件: #coding=utf-8 #author: walker #date: 2013-12-30 #func ...
- shell脚本按行读取文件的几种方式
1. #!/bin/bash while read line do echo $line done < filename(待读取的文件) 2. #!/bin/bash cat filename( ...
- 文件------概念、基本操作、打开文件的方式、按行读取文件内容、复制文件、文件/目录的常用管理操作、文本文件的编码格式、练习1-4
文件的概念 文件的概念 计算机的文件,就是存储在某种长期储存设备上的一段数据 长期存储设备包括:硬盘.U盘.移动硬盘.光盘... 文件的作用 将数据长期保存下来,在需要的时候使用 文件的存储方式 在计 ...
- Python readline()和readlines()函数:按行读取文件
Python readline()和readlines()函数:按行读取文件 前面章节中讲到,如果想读取用 open() 函数打开的文件中的内容,除了可以使用 read() 函数,还可以使用 read ...
- Python按行读取文件、写文件
Python按行读取文件 学习了:https://www.cnblogs.com/scse11061160/p/5605190.html file = open("sample.txt&qu ...
- python读取文件多行内容-Python逐行读取文件内容的方法总结
Python四种逐行读取文件内容的方法 下面四种Python逐行读取文件内容的方法, 分析了各种方法的优缺点及应用场景,以下代码在python3中测试通过, python2中运行部分代码已注释,稍加修 ...
- Shell按行读取文件的3种方法
Shell按行读取文件的方法有很多,常见的三种方法如下: 要读取的文件: 1 [root@mini05 20180930-2]# cat file.info 2 111 3 222 4 333 444 ...
- python按行读字符串,python按行读取文件并找出其中指定字符串
python怎么提取出文件里的指定内容 python读取文件内容的方法: 一.最方便的方法是一次性读取文件中的所有内容并放置到一个大字符串中: all_the_text = open('thefile ...
最新文章
- android Run模式也会出现Waiting for debugger的解决方法
- linux中sort命令
- 写给初学者的Tensorflow介绍
- android webview 获取图片,Android – 保存WebView中的图片
- jooq 入门_jOOQ,H2和Maven入门
- oracle 结果集比较,Oracle 结果集之间的问题(积并交叉)
- access汇总_区块链或密码学相关论文汇总,持续更新中
- Codeforces 964B(贪心)
- python下载慢怎么办
- BZOJ 3224: Tyvj 1728 普通平衡树(替罪羊树)
- 嵌入式(三十一):DACADC
- 服务器win2003的远程在哪里,走进win2003远程桌面的神奇世界
- 自娱小程序--超大文件topN
- 终极解决重启服务器后mysql启动失败 报 ERROR! The server quit without updating PID file (/var/run/mysqld/mysqld.pid).
- 汽车中的软件测试(一)
- ATmega32U4 芯片 时钟研究
- T4模版引擎之基础入门
- C语言——男女cp配对的小软件
- macbook pro 2015 13寸装单系统win_我的MacBook|这六款实用软件让我一见倾心
- oppo装鸿蒙系统,针对华为鸿蒙系统,小米、OPPO相继表态,魅族的态度截然相反...