#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//输出空行
void OutPutAnEmptyLine()
{
cout << "\n";
}

//读取方式: 逐词读取, 词之间用空格区分
//read data from the file, Word By Word
//when used in this manner, we'll get space-delimited bits of text from the file
//but all of the whitespace that separated words (including newlines) was lost.
void ReadDataFromFileWBW()
{
ifstream fin("C:\\Users\\byte\\Desktop\\huang.txt");
string s;
while (fin >> s)
{
cout << "Read from file: " << s << endl;
}
}

//读取方式: 逐行读取, 将行读入字符数组, 行之间用回车换行区分
//If we were interested in preserving whitespace,
//we could read the file in Line-By-Line using the I/O getline() function.
void ReadDataFromFileLBLIntoCharArray()
{
ifstream fin("C:\\Users\\byte\\Desktop\\huang.txt");
const int LINE_LENGTH = 100;
char str[LINE_LENGTH];
while (fin.getline(str, LINE_LENGTH))
{
cout << "Read from file: " << str << endl;
}
}

//读取方式: 逐行读取, 将行读入字符串, 行之间用回车换行区分
//If you want to avoid reading into character arrays,
//you can use the C++ string getline() function to read lines into strings
void ReadDataFromFileLBLIntoString()
{
ifstream fin("C:\\Users\\byte\\Desktop\\huang.txt");
string s;
while (getline(fin, s))
{
cout << "Read from file: " << s << endl;
}
}

//带错误检测的读取方式
//Simply evaluating an I/O object in a boolean context will return false
//if any errors have occurred
void ReadDataWithErrChecking()
{
string filename = "dataFUNNY.txt";
ifstream fin(filename.c_str());
if (!fin)
{
cout << "Error opening " << filename << " for input" << endl;
exit(-1);
}
}

int main()
{
ReadDataFromFileWBW(); //逐词读入字符串
OutPutAnEmptyLine(); //输出空行

ReadDataFromFileLBLIntoCharArray(); //逐词读入字符数组
OutPutAnEmptyLine(); //输出空行

ReadDataFromFileLBLIntoString(); //逐词读入字符串
OutPutAnEmptyLine(); //输出空行

ReadDataWithErrChecking(); //带检测的读取
getchar();
return 0;
}

c++从txt中读取数据,数据并不是一行路径(实用)的更多相关文章

  1. 如何实现MySQL表数据随机读取?从mysql表中读取随机数据

    文章转自 http://blog.efbase.org/2006/10/16/244/如何实现MySQL表数据随机读取?从mysql表中读取随机数据?以前在群里讨论过这个问题,比较的有意思.mysql ...

  2. C# 操作地址 从内存中读取写入数据(初级)

    本示例以植物大战僵尸为例, 实现功能为 每1秒让阳光刷新为 9999.本示例使用的游戏版本为 [植物大战僵尸2010年度版], 使用的辅助查看内存地址的工具是  CE. 由于每次启动游戏, 游戏中阳光 ...

  3. R中读取文件,找不到路径问题 No such file or directory

      R中读取文件,找不到路径问题 No such file or directory 近日,读取文件时.出现例如以下问题 > passenger = read.csv('internationa ...

  4. QT中读取文本数据(txt)

    下面的代码实现读取txt文档中的数据,并且是一行一行的读取. void MainWindow::on_pushButton_clicked() { QFile file("abcd.txt& ...

  5. R中读取EXCEL 数据的方法

    最近初学R语言,在R语言读入EXCEL数据格式文件的问题上遇到了困难,经过在网上搜索解决了这一问题,下面归纳几种方法,供大家分享: 第一:R中读取excel文件中的数据的路径: 假定在您的电脑有一个e ...

  6. C/C++程序从文本文件中读取(保存)数据

    :本文仅供初学者参阅,解惑 在C程序中: 与程序代码外的数据(文件)打交道,我们使用到流(stream)这个概念,实现进程的虚拟内存与文件之间的数据交换. ——文件流:C标准库提供了FILE(之所以命 ...

  7. python中读取mongodb数据并保存为csv格式的文件

    import pandas as pd import matplotlib.pyplot as plt import pymongo %matplotlib inline # 连接mongodb数据库 ...

  8. 以ORM的思路来从Excel文件中读取JSON数据列表

    1.一个常见的问题就是如何读取excel. 这里面有几个分支的问题,一个是如何使用poi读取excel,网上例子很多,但是这只解决了第一步.如何将excel读取入一定的数据结构这是第二个问题,还有就是 ...

  9. JAVA中读取xls数据方法介绍

    用例编号(UI-0001) 用例名称({验证页面跳转|验证元素文本}-简要明确表述) 验证类型 是否执行 初始URL 初始元素xpath 目标元素xpath 目标元素属性 期望结果 UI-0001 验 ...

随机推荐

  1. Python02 标准输入输出、数据类型、变量、随记数的生成、turtle模块详解

    1 标准输出 python3利用 print() 来实现标准输出 def print(self, *args, sep=' ', end='\n', file=None): # known speci ...

  2. vray学习笔记(4)混合材质是个什么东西

    看下定义: The Blend material lets you mix two materials on a single side of the surface. Blend material材 ...

  3. 1.5快速上手OpenCV图像处理

    在上一节中,已经完成了OPENCV的配置,在本节接触几个Opencv图像处理相关的程序,看看opencv用简洁的代码能够实现哪些有趣的图像效果. 1.第一个程序:图像显示 #include<op ...

  4. Mat 与 IplImage 和 CvMat 的转换

    在 OpenCV 2 中虽然引入了方便的 Mat 类,出于兼容性的考虑,OpenCV 依然是支持 C 语言接口的 IplImage 和 CvMat 结构.如果你要与以前的代码兼容,将会涉及 Mat 与 ...

  5. java 包装类的应用

    package integer; public class baozhuang { public static void main(String[] args) { System.out.printl ...

  6. [学习笔记]scanf弊端以及解决方案

    #include<stdio.h> #include<stdlib.h> #include<unistd.h> int main(void) { ]; //mems ...

  7. 【C#】如何打开Model Browser(实体数据模型浏览器)

    Visual Studio 2017 如何打开Model Browser(实体数据模型浏览器) 2017-10-11 十有三 2 浏览:4956 开发工具 Visual Studio 做个笔记,记录下 ...

  8. UWP&WP8.1 基础控件——Border

    border 是边框控件 border是UWP和WP8.1最常用的控件之一. border字面意义是用来添加边框的. 基础用法 <border BorderThickness="1&q ...

  9. E - 稳定排序(结构体)

    大家都知道,快速排序是不稳定的排序方法. 如果对于数组中出现的任意a[i],a[j](i<j),其中a[i]==a[j],在进行排序以后a[i]一定出现在a[j]之前,则认为该排序是稳定的. 某 ...

  10. Linux基础学习(二)

    前言: 我们在上一节了解了一下linux的硬件组成,虽然也许对具体的东西还不甚了解,但是我们知道了linux下一切皆文件这一特性 我们装好了CentOS7的虚拟机(这个可以看别人教程来装起来,比较简单 ...