- 浏览: 17482 次
文章分类
最新评论
//读取方式: 逐行读取, 将行读入字符数组, 行之间用回车换行区分
//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("data.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("data.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);
}
}
//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("data.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("data.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);
}
}
发表评论
-
sizeof()
2013-01-09 09:21 645在写程序的时候遇到了如何求字符串占多少长度的内存空间问题。先看 ... -
c/c++语言好坏pk的两篇文章
2012-12-21 22:30 0http://simpleprogrammer.com/201 ... -
hash算法常备链接
2012-12-21 16:13 571在这里总结几个常用的hash函数出来,留备后续用到时候方 ... -
在写哈弗曼算法的时候遇到的一篇好博客
2012-12-17 21:27 0在自己编写哈弗曼算法的时候,遇到了这篇文章。意外的发现,哈弗曼 ... -
strcpy()/memcpy()
2013-01-04 17:08 832陆陆续续总结几个常用函数的原型出来,为以后再看到时查阅的方便。 ... -
初写c++程序
2012-12-08 20:33 0几乎是第一次完整的写一个c++的程序,真所谓不写不知道,一写吓 ... -
汉诺塔问题
2012-11-20 22:46 687原文出处:http://zh.wikipedia.org/wi ... -
迷宫求解算法
2012-11-20 21:05 907原文出处: http://blog.csdn.net/ ... -
vector list deque 三者间的比较
2012-11-16 21:54 618http://blog.csdn.net/ianleelj/a ... -
双端队列deque (非原创)
2012-11-16 21:44 708http://hi.baidu.com/xuehuo_0411 ... -
头文件的重复包含
2012-10-19 10:48 579#ifndef XXXX_H #define XXXX_H ... -
malloc函数/强制类型转换/单链表案例/
2012-10-11 19:59 9521.malloc()函数的函数原型如下: void *ma ... -
assert 断言
2012-09-17 20:22 585assert宏的原型定义在<assert.h>中, ... -
C++ 简单读写文本文件、统计文件的行数、读取文件数据到数组(1)
2012-07-04 20:33 1235fstream提供了三个类,用来实现c++对文件的操作。(文件 ...
相关推荐
C++ 简单读写文本文件、统计文件的行数、读取文件数据到数组
linux C++ 文本文件创建写入以及读取
C++_简单读写文本文件、统计文件的行数、读取文件数据到数组.详细的介绍
C++读取csv到数组 编译器:Clion C++读取csv到数组
C++实现从.txt文件中读取数据存入数组,将数组数据写入.txt文件.rar C++实现从.txt文件中读取数据存入数组,将数组数据写入.txt文件.rar
// C++读取文本到数组.cpp : 定义控制台应用程序的入口点。 // #include stdafx.h int readfile1D() { char a[100]; int i; FILE *fp = fopen(1.txt,r); if(fp == NULL) { printf(文件读取无效.\n); ...
用C++语音实现一维数组二维数组写入txt,从txt中读取数据存到一维数组、二维数组,数组用指针表示
编译环境: Windows 7 Service Pack 1 C++ Builder Embarcadero RAD Studio XE Version 15.0.3890.34076 ... 余文溪的《C++ STL --数据结构与算法实现》原书代码为控制台。 这里用 C++ Builder代码演示
c++读写文本文件,实现对文件的读和取操作,用到了fstream流的概念。
C++文件读写操作C++文件读写操作 C++文件读写操作C++文件读写操作
读取环境为Win10+VS2015,个人参考资料封装的excel读取模块用来读取Excel2007中的数据,文件为压缩文件,亲测可用。
c++读取csv数据并存储到数组中。csv数据可以为一行多列也可以为多行一列。
C++读写txt文件,逐行读取,判断并根据判断条件写至输出文件
本段代码描述了如何使用C++的fstream库来读写文本文件和二进制文件。对于文本文件,我们使用std::ofstream和std::ifstream类,并通过流操作符和>>或std::istreambuf_iterator来写入和读取文本数据。对于二进制文件,...
c++文件读写.pdf
C++文件读写,使用C++函数进行文件的读取和写入操作。
能从硬盘上读取一文本文件(一篇英语文章),将这篇文章的内容使用适当的数据结构保存起来,能够统计出各个单词出现的频率、和查询特定的单词。
OpenXLSX是一套开源的读、写、创建、修改Excel文件(格式:xlsx)的C++库。附件内容:源码+示例。
https://github.com/nlohmann/json 最好用的C++读写json库
利用C++编写的 读入字符型数组 最后转成TXT格式的文件 主要功能就是文件读入