`

使用(C++库)ifstream读文件数据

 
阅读更多
入门计划->使用(C++库)ifstream读文件数据 选择自 awzzz 的 Blog
关键字 入门计划 C++ ifstream 读文件 读文件数据
出处
收藏+原创
//awzzz@2002
//入门计划->使用(C++库)ifstream读文件数据
//APO->using ifstream.
//accidence project of using ifstream.(c++ iostream library)

/*
  Reference: http://www.cplusplus.com/ref/iostream/ifstream/

  Description

  ifstream class provides a stream interface to read data from files.

  The class mantains internally (privately) a pointer to a filebuf object
  in charge of the interaction with the file. This pointer can be
  obtained/modified by calling member rdbuf.

  The file to be processed can be specified as a parameter for the
  constructor or by calling member open.

  After a file is processed it can be closed by calling member close.
  In this case the file stream may be used to open another file.

  Member is_open can be used to determine wether the stream is currently
  operating on a file or not.

  Please refer to parent classes for more details on information mantained
  by an ifstream object.
*/

//使用(C++库)ifstream读文件数据
//simple example

#include <iostream>
#include <fstream>
using namespace std;

#ifdef WIN32
#define TEST_FILE   "c://tmp//test.txt"
#else
#define TEST_FILE   "/tmp/test.txt"
#endif

void get()
{
    //ifstream ifs;
    //ifs.open(TEST_FILE);
    ifstream ifs(TEST_FILE);

    //while (ifs.good()) cout << (char) ifs.get();//simple
    while (ifs.good()) {
        
        char ch = 0;
        //ch = ifs.get();
        ifs.get(ch);

        cout << ch;
    }

    ifs.close();
}

void getline()
{
    //ifstream ifs;
    //ifs.open(TEST_FILE);
    ifstream ifs(TEST_FILE);

    while (ifs.good())
    {
        char buf[1024] = {0};
        ifs.getline(buf, sizeof(buf));

        cout << buf << endl;
    }

    ifs.close();
}

int main(int argc, char* argv[])
{   
    get();
    getline();
    return 0;
}

分享到:
评论

相关推荐

    C++格式化读写文件操作

    C++中的文件读写可以使用标准库中的iostream库来完成。下面是描述如何进行格式化的文件读写的步骤,不包含具体的代码实现: 包含头文件:在程序中包含iostream头文件,以便使用输入输出流。 打开文件:使用fstream...

    C++中进行txt文件读入和写入的方法示例

    vs里面直接输入数据,这一般常见于简单算法和数据的处理,但是一旦处理大数据的话,几百万,几千万,上亿个数据手打似乎不能轻易实现的,那么这篇文章我们来搞懂C++环境下如何进行io流读取txt文件,其实我们需要一个...

    C++文件读写总结 !!!

    但这篇文章介绍的方法,我们分别使用"ifstream"?和 "ofstream" 来作输入输出。 如果你用过标准控制台流"cin"?和 "cout," 那现在的事情对你来说很简单。 我们现在开始讲输出部分,首先声明一个类对象。 ofstream fout...

    JPG图片转化成十六进制数据的程序

    在VS2010中用IFSTREAM来读取JPG文件,然后用OFSTREAM来写入txt文件,txt文件输出十六进制数据(例.. FF D8 .... FF D9)

    利用C++如何覆盖或删除指定位置的文件内容

    我们经常使用ofstream或者fstream可写文件,使用ifstream可以写文件,但需要设置文件的打开状态为iOS::out。C++中IO流打开模式使用位掩码来表示。 IO流打开模式有: 成员常量 app append,追加模式,设置流...

    c++,IO,文件传输程序

    在vs 2005下编写的文件传输程序,使用ifstream 读入数据,ofstream 输出数据,对正在学习fstream 的同学可能会有帮助,需要先启动服务端,再启动客户端才能正确执行

    详解C++文件读写操作

    在看C++编程思想中,每个练习基本都是使用ofstream,ifstream,fstream,以前粗略知道其用法和含义,在看了几位大牛的博文后,进行整理和总结: 这里主要是讨论fstream的内容: #include ofstream //文件写操作 ...

    C++ ofstream与ifstream详细用法

    在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包括我们要认识的文件I/O,stream这个类有两个重要的运算符: 1、插入器(&lt;&lt;) 向流输出数据。比如说系统有一个默认的标准输出流(cout),一般...

    C++中读写txt文件并分离字符的方法

    在实际工程中,经常遇到需要读取txt文件,txt文件中存的是一些小数或者整型数据,在C++中,可以利用string类和ifstream库文件对txt进行的读取,不过读回的数据经常是以字符串的形式返回,一般是txt的一行为一个字符...

    C++——文件和流

    这就需要用到 C++ 中另一个标准库 fstream,它定义了三个新的数据类型: 数据类型 描述 ofstream 该数据类型表示输出文件流,用于创建文件并向文件写入信息。 ifstream 该数据类型表示输入文件流,用于从...

    C++ I/O文件读写操作的示例代码

    ifstream 对文件输入(读文件) ofstream 对文件输出(写文件) fstream 对文件输入或输出 //写文件 #include #include #include using namespace std; int main() { string name; int age; ofstream ...

    c++读取数据文件到数组的实例

    如果在一个txt文件当中有以下数据 1 2 3 4 5 6 7 8 9 10 1、如果我们只是简单将这些数据保存在一个数组中: #include #include using namespace std; int main() { int i,datalen=0; double num[100]; ...

    C++实现图书管理系统/控制台程序+课件设计+毕业设计

    本程序是一个纯c++编写的运行在windows系统上的图书管理系统。...使用到了c++标准模板库vector,utf-8和gbk相互转码,以及ifstream和ofstream的文本文件的读写操作。为了实现方便每次修改数据都是全覆盖操作。

    深入理解C++中的文件操作

    前言 我们在编写程序的时候,最密不可分的... ifstream: 读操作(输入)的文件类(由istream引申而来)  fstream: 可同时读写操作的文件类 (由iostream引申而来) 打开文件(Open a file) 对这些类的一个对象所做的第一个

    足球联赛积分排名程序(C++版)

    构造函数(ifstream& file_name) 从文件里获得数据 char *得到球队名() Void 输入各场数() // Int 得到名次() // Int 得到积分() // Int 得到净胜数() Void friend 排名(teams& A,teams& B)...

    读取BMP文件的C++程序.pdf

    读取 BMP 文件的 C++程序 最近做一个图像处理的作业,写了个 BMP 文件读取的程序。 在网上找了些资料,还是当作学习笔记记下来,以后也好看看。 读取的文件是 cameraman.bmp 黑白图像,256×256×256 色的。 #include ...

    C++实现学生成绩统计管理系统

    ifstream infile("ok.txt",ios::in); if(! infile) cerr文件打开失败。"; for(int i=0;i;i++) { infile&gt;&gt;a[i]&gt;&gt;b[i]&gt;&gt;c[i]&gt;&gt;d[i]&gt;&gt;e[i]&gt;&gt;f[i]; item[size].sinto(a[i],b[i],c[i],d[i],e[i]);size++; if(f[i]==1) ...

    C++ 读取文件内容到指定类型的变量方法

    如下所示: #include #include #include #include using namespace std;... // ifstream infile&#40;"1.txt"&#41;; ifstream infile&#40;file_name.c_str(&#41;); string line; while (std::get

    C++ 小型复数计算器

    //如果不是,说明复数数据格式错误 else com.Image=sign*n; //是最后一个字符,复数对象已接受完,用sign*n为虚部赋值 break; } while(s[k]&gt;='0' && s[k]) //当前字符在0~9之间,将数字字符转换成数字数值 { n=n*...

    消费者生产者模型 c++

    //提取每个线程的信息到相应数据结构中; while(inFile){ inFile &gt;&gt; Thread_Info[n_Thread].serial; inFile &gt;&gt; Thread_Info[n_Thread].entity; inFile &gt;&gt; Thread_Info[n_Thread].delay; char c; inFile.get...

Global site tag (gtag.js) - Google Analytics