假设文件内容为

1. hello1 hello2 hello3 hello4
2. dsfjdosi
3. skfskj ksdfls

输出每个单词

代码

#include <iostream>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <string> using namespace std; int main()
{
string word;
ifstream infile("text");
if(!infile)
{
cout << "cannot open the file" << endl;
return ;
}
while(infile >> word)
cout << "word:" << word << endl;
infile.close();
}

结果

分析

定义文件流infile,并绑定文件“text”,之后判定,如果打开错误,则提示错误,结束程序。否则,继续执行程序:

输入文件流infile把文件中的内容全部读入缓冲区(文件结尾符时停止往输入缓冲区内存),通过重定向符>>传到word(间隔福为Tab, Space, Enter)

输出每一行

代码

#include <iostream>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <string> int main()
{
ifstream infile;
string line;
infile.open("text");
if(!infile)
cout << "error: cannot open file" << endl;
while(getline(infile, line))
{
cout << "line:" << line << endl;
}
infile.close();
}

结果

分析

函数原型:istream& getline ( istream &is , string &str , char delim );

is为输入流, str为存储读入内容的字符串(不存储分割符), delim为终结符(不写默认为'\n')

返回值与参数is一样

同时输出每行和该行的每一个单词

代码

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdexcept>
using namespace std; int main()
{
ifstream infile;
string line, word;
infile.open("text");
if(!infile)
cout << "error: cannot open file" << endl;
while(getline(infile, line))
{
cout << "line:" << line << endl;
istringstream instream(line);
while(instream >> word)
cout << "word:" << word << endl;
cout << endl;
}
infile.close();
}

结果

分析

<<是类istream 定义的,istringstream, ifstream 是其子类,都可以用

>>是类ostream 定义的,ostringstream, ofstream 是其子类,都可以用

用读出的每行初始化字符串流,在输出赋给单词(依旧分隔符隔开,但没有分隔符的事儿)

【c++】输出文件的每个单词、行的更多相关文章

  1. OC获取文件(夹)的代码行数

    /* 考察NSString NSArray NSFileManager */ #import <Foundation/Foundation.h> /* 计算单个文件的代码行数 path:文 ...

  2. Linux常用基本命令wc-统计文件的字节,字符,行数

    wc命令 作用:统计文件的字节,单词,行数 用法:wc [option] [file] -c:统计字节 ghostwu@dev:~/linux/uniq$ cat ghostwu.txt 192.16 ...

  3. wc - 输出文件中的字节数、单词数、行数

    SYNOPSIS 总览 wc [选项列表]... [文件名列表]... DESCRIPTION 描述 对每个文件输出行.单词.和字节统计数,如果指定了多于一个文件则还有一个行数的总计.没有指定文件或指 ...

  4. python123期末四题编程题 -无空隙回声输出-文件关键行数-字典翻转输出-《沉默的羔羊》之最多单词

    1. 无空隙回声输出 描述 获得用户输入,去掉其中全部空格,将其他字符按收入顺序打印输出. ‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬ ...

  5. [Linux] 输出文件的指定行

    1.获取第k行(以k=10为例) 要注意的是,如果文件包含内容不足10行,应该不输出. # Read from the file file.txt and output the tenth line ...

  6. rev---将文件中的每行内容以字符为单位反序输出

    rev命令将文件中的每行内容以字符为单位反序输出,即第一个字符最后输出,最后一个字符最先输出,依次类推.

  7. tail命令 输出文件后n行,默认查看文件的后10行

    默认查看文件的后10行 -n 3 数字   也可以忽略-n 直接加数字 tail 3 查看文件后3行 [root@localhost ~]# tail /etc/passwd // 默认查看文件的后十 ...

  8. tail命令 查看文件尾部 输出文件后n行,默认查看文件的后10行

    tail命令 查看文件尾部  用于查看日志 默认查看文件的后10行 -n 3 数字   也可以忽略-n 直接加数字 tail 3 查看文件后3行 [root@localhost ~]# tail /e ...

  9. C++实现对文件中各单词词频的统计及其代码优化

    先给出github上的代码链接以及项目需求 1.项目概述 这个项目的需求可以概括为:对记事本(txt)文件进行单词的词频统计和排序,排序结果以指定格式输出到默认文件中,并要求能够快速地完成整个统计和结 ...

随机推荐

  1. RobotFramework做接口自动化(内部接口需要登录token)

    背景: 项目中需要做接口自动化测试,使用robot,有一个收货地址列表的接口直接传参数访问时会返回:{"msg":"缺少参数","code" ...

  2. leetcode 反转字符串

    请编写一个函数,其功能是将输入的字符串反转过来. 示例: 输入:s = "hello" 返回:"olleh" /** * @param {string} s * ...

  3. 使用PLSQL Developer时中文乱码问题

    使用PLSQL Developer时中文乱码问题 一.问题: 执行一些查询结果有中文的SQL语句,显示不了中文,显示???. 二.产生的原因: 客户端与服务器端的编码不一致造成的. 三.解决方案: 1 ...

  4. oracle-01427

    Oracle / PLSQL: ORA-01427 Learn the cause and how to resolve the ORA-01427 error message in Oracle. ...

  5. 对drf序列化器的理解

    序列化: 将对象的状态信息转换为可以存储或传输的形式的过程.(百度定义) 对应到drf中,序列化即把模型对象转换为字典形式, 再返回给前端,主要用于输出 反序列化: 把其他格式转化为程序中的格式. 对 ...

  6. CSS 两个行内块元素,宽度相加刚好等于父盒子容器的元素,但第二个元素掉在第二行解决办法

    我们可以发现:两个行内块元素,宽度相加刚好等于父盒子容器的元素,但第二个元素掉在第二行,这是什么问题呢? 我们先来看一下效果: <!DOCTYPE html> <html lang= ...

  7. (USB HID) Configuration Descriptor

    最近完成了HID的基本收發,使用的配置用了2個Endpoint,把一些特別重要要的地方紀錄下來 整個Configuration 分成4大部分 : 1. Configuration 2. Interfa ...

  8. (C/C++) 亂數應用

    因為公司需要寫了一個亂數產生測試條件的小程式,再此紀錄下來 int _tmain(int argc, _TCHAR* argv[]) { fstream file; file.open("t ...

  9. 在makefile中打印错误或警告信息

    在makefile中打印警告或者错误消息的方法: $(warning xxxxx) 或者 $(error xxxxx) 输出变量方式为: $(warning $(XXX))

  10. weex 自定义Modul

    扩展iOS的功能 一. 新建 NSOjbect 子类, 并遵循协议<WXModuleProtocol> .h 代码 #import <Foundation/Foundation.h& ...