fstream

C++文件fstream的操作

最后都变了- 提交于 2019-12-04 08:50:33
                用到的关于输入输出fstream流相关的知识 1.两个主要函数: read( )函数 从流中读取字符串的成员函数read 该成员函数一般形式是:read(char* pch, int nCount) 从输入流中读取nCount个字符。当输入流中的字符数小于nCount时,结束读取。经常使用read函数读取二进制数据。 write( )函数 成员函数write()输出一串字符。 该成员函数一般形式是:write(char* pch, int nCount) 其中,pch是指向字符数组的指针; nCount指明从第一个参数中复制输出字符的个数。 2. 文件的随机读写 (1)输出流随机访问函数   输出流(output) 随机访问函数有seekp和tellp。 (2)输入流随机访问函数   输入流 随机访问函数有seekg和tellg。 3. 获得和设置流指针(get and put stream pointers) 所有输入/输出流对象(i/o streams objects)都有至少一个流指针: ifstream, 类似istream, 有一个被称为get pointer的指针,指向下一个将被读取的元素。 ofstream, 类似 ostream, 有一个指针 p ut pointer ,指向写入下一个元素的位置。 fstream, 类似

Difference between casting ifstream to bool and using ifstream::is_open()

徘徊边缘 提交于 2019-12-04 07:30:29
Maybe a dummy question, but I need a clear answer to it. Is there any difference at all in the return of any of those functions int FileExists(const std::string& filename) { ifstream file(filename.c_str()); return !!file; } int FileExists(const std::string& filename) { ifstream file(filename.c_str()); return file.is_open(); } So in other words, my question is: does casting the fstream to bool give exactly the same result as fstream::is_open()? No. is_open checks only whether there is an associated file, while a cast to bool also checks whether the file is ready for I/O operations (e.g. the

Reading input from text file to array in c++

三世轮回 提交于 2019-12-04 06:54:02
问题 Alright, be gentle, since I'm very much a novice to programming. So far I've only studied C++ and I'm running Visual Studio 2010 as my compiler. For this program, I'm trying to read from a text input file and write the information to a set of three arrays. One array will handle a list of names, and the other two are for hours worked and hourly pay rate, respectively. I will use the latter two to calculate a set of earnings and output the whole thing to another text file as a report. My

Java语言学习(7)-Java中IO

久未见 提交于 2019-12-04 06:24:29
Java中的IO Java中的IO包含流(Stream)、文件(File)的IO,所有IO类都定义在java.io包中。 一、控制台 IO 1、输入   Java的控制台输入由System.in完成。为了获取控制台的输入,可以把System.in封装在一个BufferedReader对象进行操作。样例:      BufferedReader bReader = new BufferedReader(new InputeStreamReader(System.in));   然后对bReader对象进行操作。   1) bReader.read():     从控制台读取一个字符并按照int返回。流读取结束时时候返回-1,并抛出一个IOException。read()函数定义如下:       int read( ) throws IOException   2) br.readline()     从控制台读取一行字符串,返回String类型。readline()函数定义如下:     String readLine( ) throws IOException    样例:     char c;     c = (char)br.read();     System.out.printfln(c);     String str;     str = br.readline()

Seeking in large files with ifstream

时光怂恿深爱的人放手 提交于 2019-12-04 05:14:33
I'm implementing a program in C++ using ifstream that must seek in large files (~1TB). However, this fails after reading 2GB. Is there a way to get file positions, even for large files? I compile for a 32-bit windows machine. std::ifstream f; f.open( filename.c_str(), std::ifstream::in | std::ifstream::binary ); while(true) { std::cout << (uint64_t)(f.tellg()) << std::endl; //read data } Since you are compiling on a 32-bit platform, if you use fstream , you are going to get 32-bits access. To access large files, you need to use a platform dependent solution : for windows, use _lseeki64() for

clang++ fstreams 10X slower than g++

只谈情不闲聊 提交于 2019-12-04 04:10:13
问题 Q: Is there a way to speed up clang++ STD Library fstreams? (And does anybody know why it is so much slower than g++?) I am trying to process very large (many GBs) binary data files and was surprised to find the performance was so poor. At first, I thought it was something to do with my code. But I am seeing the same slow performance in a boiled down example. I even tried allocating different size buffers via rdbuf()->pubsetbuf() but this didn't seem to have much effect. Here is a simple

Detect new line c++ fstream

穿精又带淫゛_ 提交于 2019-12-04 03:54:27
问题 How do I read a .txt copy the content to another .txt by using fstream to a similar content. The problem is, when in the file there is new line. How do I detect that while using ifstream? user enter "apple" Eg: note.txt => I bought an apple yesterday. The apple tastes delicious. note_new.txt => I bought an yesterday. tastes delicious. the resulting note suppose to be above, but instead: note_new.txt => I bought an yesterday. tastes delicious. How do I check if there is a new line in the

Reading a single character from an fstream?

女生的网名这么多〃 提交于 2019-12-04 03:05:45
问题 I'm trying to move from stdio to iostream, which is proving very difficult. I've got the basics of loading a file and closing them, but I really don't have a clue as to what a stream even is yet, or how they work. In stdio everything's relatively easy and straight forward compared to this. What I need to be able to do is Read a single character from a text file. Call a function based on what that character is. Repeat till I've read all the characters in the file. What I have so far is.. not

Returning ifstream in a function

你说的曾经没有我的故事 提交于 2019-12-04 02:41:34
Here's probably a very noobish question for you: How (if at all possible) can I return an ifstream from a function? Basically, I need to obtain the filename of a database from the user, and if the database with that filename does not exist, then I need to create that file for the user. I know how to do that, but only by asking the user to restart the program after creating the file. I wanted to avoid that inconvenience for the user if possible, but the function below does not compile in gcc: ifstream getFile() { string fileName; cout << "Please enter in the name of the file you'd like to open:

c++ - fstream and ofstream

守給你的承諾、 提交于 2019-12-04 00:23:12
问题 What is the difference between: fstream texfile; textfile.open("Test.txt"); and ofstream textfile; textfile.open("Test.txt"); Are their function the same? 回答1: ofstream only has methods for outputting, so for instance if you tried textfile >> whatever it would not compile. fstream can be used for input and output, although what will work depends on the flags you pass to the constructor / open . std::string s; std::ofstream ostream("file"); std::fstream stream("file", stream.out); ostream >> s