I/O Stream <傻瓜机器人对话>

原创 2015年09月05日 18:57:18

程序说明:如果 kingLear.txt 行与行之间有单个或多个空行,则忽略空行内容,继续向下读取文件内容并显示。
不论文件末尾最后一行有无换行符程序均可正常运行。
kingLear.txt

Is not this your son, my lord?
I cannot conceive you.
I cannot wish the fault undone, the issue of it being so proper.
I must love you, and sue to know you better.
Come not between the dragon and his wrath;
I lov’d her most, and thought to set you rest on her kind nursery.
Whom I have ever honor’d as my king,
Lov’d as my father, as my master follow’d,
As my great patron thought on in my prayers.

运行结果:

 Enter your question: 1
 1
 Is not this your son, my lord?
 Enter your question: 2
 2
 I cannot conceive you.
 Enter your question: 3
 3
 I cannot wish the fault undone, the issue of it being so proper.
 Enter your question: 4
 4
 I must love you, and sue to know you better.
 Enter your question: 5
 5
 Come not between the dragon and his wrath;
 Enter your question: 6
 6
 I lov’d her most, and thought to set you rest on her kind nursery.
 Enter your question: 7
 7
 Whom I have ever honor’d as my king,
 Enter your question: 8
 8
 Lov’d as my father, as my master follow’d,
 Enter your question: 9
 9
 As my great patron thought on in my prayers..
 Enter your question: 10
 10
 Is not this your son, my lord?
 Enter your question:

test.cpp

#include <iostream>
#include <fstream>
#include <ctime>

using namespace std;

bool readLine(istream& fin = cin);

int main() {
    ifstream fin;
    fin.open("/Users/br/Documents/C:C++/Test/Test/kingLear.txt");
    if (fin.fail()) {
        cout << "fin file opening failed.";
        exit(EXIT_FAILURE);
    }

    while (true) {
        readLine();

        while (readLine(fin)) {
            if (! fin.eof())
                continue;
            else {
                fin.close();
                fin.open("/Users/br/Documents/C:C++/Test/Test/kingLear.txt");
                readLine(fin);
                break;
            }
        }
    }

    return 0;
}

bool readLine(istream& fin) {
    char c;
    fin.get(c);
    if (c == '\n')
        return true;

    while (c != '\n' && (! fin.eof())) {
        cout << c;
        fin.get(c);
    }
    cout << "\n";

    return false;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

[2014-10-29]JAVA笔记_I/O 系统、File、Stream、Serializable

一、 File 类

old stream I/O new nio mapped file performance

如果读写文件:一定首选mapped file ,几乎比其它快90%左右。 package io; import java.nio.*; import java.nio.channels.*; im...

C++ Stream I/O

C++ Stream I/OStream Input/OutputStream I/O ApplicationsStream Output ConceptStream Input ConceptUsi...
  • Augusdi
  • Augusdi
  • 2011年12月12日 10:11
  • 2855

Java I/O Stream

流是一个抽象的概念。当Java程序需要从数据源读取数据时,会开启一个到数据源的流。数据源可以是文件,内存或者网络等。同样,当程序需要输出数据到目的地时也一样会开启一个流,数据目的地也可以是文件、内存或...

Java基础(十一) Stream I/O and Files

Java基础(十一) Stream I/O and Files1. 流的概念程序的主要任务是操纵数据。在Java中,把一组有序的数据序列称为流。根据操作的方向,可以把流分为输入流和输出流两种。程序从...

C++中的I/O stream类

ios_base:顶层基类 basic_ios template > class basic_ios :public class ios_base {}; basic_istream:两者虚...

C++ I/O Stream 控制小数点位数

manhattan.txt15.33 21.11 3.00 10.32 mami.txt11.33 91.11 13.00 20.32 #include #include using namespa...
  • oadjing
  • oadjing
  • 2015年09月04日 14:43
  • 196

I/O Stream输入输出流

I/O中四大流类OutputStream、InputStream、Writer、Reader。 InputStream读取数据,OutputStream写入数据。均是面向字节的,字节流在操作的时候...

I/O Stream与cin处理行尾'\n'的函数重载

fin.txt#include #include using namespace std;void newLine(istream& in_stream); int main() { ifs...
  • oadjing
  • oadjing
  • 2015年09月04日 21:56
  • 156

I/O stream

I/O指的是程序的输入(Input)和输出(Output)。 “流”是我们的第一个“对象”例子。对象乃是一种特殊的变量,它拥有自己的特殊用途的函数。 流(strea...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:I/O Stream <傻瓜机器人对话>
举报原因:
原因补充:

(最多只允许输入30个字)