fstream

《c++ primer 》chapter10

落爺英雄遲暮 提交于 2020-02-06 02:52:00
1、使用流迭代器读取文本文件,存入一个vector中的string里,并用流迭代器和copy输出 # include <iostream> # include <fstream> # include <iterator> # include <vector> # include <string> using namespace std ; int main ( void ) { ifstream FILE ( "1.txt" ) ; //输入流FILE绑定文件1.txt istream_iterator < string > string_it ( FILE ) ; //从FILE输入流中读取string类型 istream_iterator < string > string_eof ; //默认构造定义尾后迭代器 //获取输入流FILE中的string类型元素加入vector中 vector < string > string_vt ; while ( string_it != string_eof ) string_vt . push_back ( * string_it ++ ) ; //使用迭代器输出vector中的内容 ostream_iterator < string > os_it ( cout , " " ) ; copy ( string_vt . begin

Reading txt multiple times

♀尐吖头ヾ 提交于 2020-02-05 06:48:08
问题 I have a program which uses a text_file to store lots of numbers. When I have to load those numbers I have to load it with 2500 numbers a time. I have a while loop to load it again and again and again... Now, the problem occurs in the while loop I guess. ifstream mfile("abc.txt", ifstream::out); if(mfile.is_open()) { getline(mfile, b); char* ch = new char[b.length() + 1]; strcpy(ch, b.c_str()); result = atof(strtok (ch,";")); while(i<125) { cout<< strtok (NULL,";")<<" "; i++; } i=0; } else {

C++ fstream文件操作问题记录

巧了我就是萌 提交于 2020-02-01 18:14:25
今天测试阿里云OSS文件上传接口,用fstream获取文件指针,代码如下 std::shared_ptr<std::iostream> content = std::make_shared<std::fstream>(__FILE__, std::ios::in); 该代码读取当前文件,拿到文件指针content,测试没有问题 但是后面讲__FILE__改为某个dwg文件后测试,总是只能上传一部分。 分析过程,肯定是文件读取不完整,提前读到了文件结尾导致的,改为如下方式后读取上传成功 std::shared_ptr<std::iostream> content = std::make_shared<std::fstream>(localFiePath, std::ios::in | std::ios::binary); 即添加 std::ios::binary ,表示按二进制文件读取。 所以,就像C的 fopen 函数一样, fstream 如果不指定打开方式,默认会以文本方式打开文件,此时对于二进制文件就会出现提前读取结束的问题。 来源: https://www.cnblogs.com/jixiaohua/p/12249180.html

C++ 文件的简单操作(自学笔记,可能有错)

痴心易碎 提交于 2020-01-31 02:43:29
IOS istream (输入) ostream(输出) ifstream (文件输入) iostream(输入输出) ofstream(文件输出) fstream(文件输入输出) 一般用黄色标记的两个头文件 特别注意 相对路径是指.cpp的当前目录 还有一种是.exe文件的当前目录 open函数的写入和读取 ios::in 读文件 ios::out 写文件 ios::app 在后面追加文件 #include <iostream> #include <string> #include <fstream> using namespace std; int main() { fstream file; string str; file.open(".\\1.txt", ios::out); if (file.is_open()) { cout << "打开文件成功" << endl; } else { cout << "打开文件失败" << endl; } file << "123" << endl; file.close(); file.open(".\\1.txt", ios::in); //读取的时候用这个 //getline(file,str,'a') 这个意思是以字符a做分隔符,但不会读到a //默认 getline(file,str) 这是以 \n(换行符) 做分割

Having trouble with fstream in Xcode

南笙酒味 提交于 2020-01-30 08:47:03
问题 I'm having trouble validating the existence of REGISTER.txt for input purposes in a function (see below). My understanding is that if the file doesn't exist, then the file won't be opened and the file stream variable (inData) will be false. Thus, I can use that variable in an if/else statement to verify whether or not it opened. But even though REGISTER.txt is in the same directory as my .cpp file, my code still says that it wasn't opened. Here's the thing though. When I run the same exact

Having trouble with fstream in Xcode

六月ゝ 毕业季﹏ 提交于 2020-01-30 08:46:06
问题 I'm having trouble validating the existence of REGISTER.txt for input purposes in a function (see below). My understanding is that if the file doesn't exist, then the file won't be opened and the file stream variable (inData) will be false. Thus, I can use that variable in an if/else statement to verify whether or not it opened. But even though REGISTER.txt is in the same directory as my .cpp file, my code still says that it wasn't opened. Here's the thing though. When I run the same exact

c++ 文件的简单操作

那年仲夏 提交于 2020-01-30 00:16:27
文件的读取操作 在程序设计中,文件常用的操作不外乎——打开、读、写、流指针操作、关闭。我日常中使用的比较多,但从来 没有细细总结今天就总结下具体的用法。 相关概念 计算机上的文件其实是数据的集合,对文件的读写其实是对数据的读写。 文件可以大致分为两种:文本文件和二进制文件。   1、文本文件:它的每一个字节存放的是一个ASCII码,代表一个字符。 2、二进制文件:将内存中的数据按照其在内存中的存储形式原样存放在磁盘上。我们用记事本打开则是乱码。 文件的路径 文件的相对路径(重在理解)——英文的双引号 定义:目标文件相对于当前文件的路径。 表达形式: (1)"./“或”.”表示显示当前目录,也可以省略,表示默认当前目录 (2)”…/"或”…”表示返回到上一级的目录。 (3)如果有多个上一级目录,可以使用多个“…/”或”…”。 文件的打开方式 (三)文件的打开方式 ios::in 以输入方式打开文件,支持读数据流。如果文件不存在会找不到文件。 ios::out 以输出方式打开文件,支持写数据流。如果文件不存在则新建,如果文件存在就清空其原有内容。 ios::app 输出的数据追加到文件末尾,只支持读,不支持写 ios::ate 打开一个文件,并将指针定位到文件末尾 ios::trunc 打开一个文件,如果文件不存在则新建,如果存在,则清空原有文件中的内容 ios::binary

C++"文件流"的一些总结

瘦欲@ 提交于 2020-01-29 00:57:33
文件流概述 书面概述: 在C++中,文件被看作是字符序列,即文件是有一个个字符数据顺序组成的,是一个字符流。 小编总结过程: 1.创建一个流 2.将流与文件关联 3.打开文件,进行操作 4.关闭文件 好了,接下来进入正题,开始我们的系统学习。 首先,C++提供的文件流类包括ifstream(文件的输入),ofstream(输出),fstream(输入和输出)。 1.文件的打开: C++中,定义了用成员函数open()和构造函数打开文件 f.open("**c:\\3.txt**",**ios::in**,**filebuf::openprot**) 小括号中第一个加黑是将要打开的文件的路径和文件名,第二个加黑是文件的打开方式(以输入方式打开) ,第三个加黑是打开时的访问方式(以共享方式打开) 2.文件的关闭: f . close ( ) 3.用 open()打开文件失败时,应如何提醒: fstream f ; f . open ( "c:\\b.txt" , ios :: nocreate ) ; if ( ! f ) { cout << "无此文件" return 0 ; } 4.文件的读写 ifstream(文件的输入),ofstream(输出),fstream(输入和输出)从ios,istream,ostream继承过来的成员函数有read(),write(),get() 等

进程间通信二:管道技术之输入输出重定向

。_饼干妹妹 提交于 2020-01-26 06:10:23
转载自: http://blog.csdn.net/morewindows/article/details/7390350 本篇将介绍 输入输出的重定向问题, 先来看一个小小的实例,设有一个程序,该程序的输入输出为标准输入输出即从键盘上输入,输出到屏幕。现在要重定向输入法输出,使程序从文件中读取数据,处理后输出到文件。程序代码如下(称此程序为示例程序): [cpp] view plain copy #include <stdio.h> int main() { int n; while (scanf( "%d" , &n) != EOF) //标准输入时,可按ctrl+z来输入EOF { n *= 2; printf( "%d\n" , n); } } 试给出几种不同的实现方法,另外如果没有程序代码,只有可执行文件,又应该如何做了? 实现方法一使用C语言的freopen()函数 函数功能:重定向控制台的输入输出 函数原型: FILE * freopen ( const char * path , const char * mode , FILE * stream ); 函数说明: 第一个参数为文件指针(也可以用来指向标准输入输出)。 第二个参数为打开方式, "w" 表示 写, "r" 表示 读, "a" 表示 追加。其它设置可以参考 MSDN 。 第三个参数为 FILE 类型的指针

Remove memory from the middle of a file

不羁的心 提交于 2020-01-26 04:34:13
问题 I have a binary format which is build up like that: magic number name size blob name size blob name size blob ... it is build up to easy move through the file and find the right entry. But I would like also to remove an entry (let's call it a chunk as it is one). I guess I can use std::copy/memmove with some iostream iterators to move the chunks behind the one to delete and copy them over the chunk to delete. But then I have the space I deleted at the end filled with unusable data(I could