fstream

Using fstream::seekg under windows on a file created under Unix

亡梦爱人 提交于 2019-12-07 19:31:49
问题 I have C++ a cross-platform program (compiled with g++ under Linux and with Visual Studio under PC). This program writes lines to a text file (using << operator and std::endl ) but can also read data back from the generated text file (using std::getline ). To optimize data access and save memory, when reading the data file, I read it a first time and save data position in my program. When data is needed, I later use seekg to move to a specific position and read the data. Creating and reading

Why does fstream.open() fail “If the mode has both trunc and app set”?

落花浮王杯 提交于 2019-12-07 18:41:53
问题 It took me quite a while to figure out that my .open() call wasn't opening a file because I had both the trunc and app mode options set. I only figured this out after catching a little note written on the C++ docs. This seems like a weird gotcha. Why is this the case? Can you not truncate the file and then append only? Or is this considered superfluous specification? 回答1: The iostream open modes correspond roughly to the fopen mode in the C library and fopen has a w mode that truncates and an

ofstream::operator<<(streambuf) is a slow way to copy a file

孤街浪徒 提交于 2019-12-07 17:23:21
问题 I need a cross-platform, no external library, way of copying a file. In my first pass I came up with (error handling omitted): char buffer[LEN]; ifstream src(srcFile, ios::in | ios::binary); ofstream dest(destFile, ios::out | ios::binary); while (!src.eof()) { src.read(buffer, LEN); dest.write(buffer, src.gcount()); } This worked nicely and I knew exactly what it was doing. Then I found a post on stackoverflow (sorry, can't find a link right now) that says I can replace all of the above code

FStream C++ automatic line addition

大兔子大兔子 提交于 2019-12-07 15:32:26
It is a pain to do an endline between each value with OFSTREAM. Here's an example of what I've got: ofstream fout("~/xample.txt"); fout << val1; fout << endl; fout << val2; I want to be able to do, instead, ofstream fout("xample.txt"); fout << val1; fout << val2; I don't care how the file is stored because I will write a configuration wizard anyways. If fout << val1 << endl; does not work with you, you might be able to inherit ofstream and create your own stream that adds endl automatically. But also you can drink water from fire hydrant. It is a pain ... Besides you could simply write fout <<

Using fstream write

柔情痞子 提交于 2019-12-07 13:44:47
问题 I am trying to look for a line in a specified file and replace it with my line. I don’t have access to the library on the machines I’ll be running this on, so I created a custom file. The trouble seems to be the write call to the fstream object. I was wondering if any of you can help. Also, my getline loop stops before reaching the end of the file, and I am not sure why. #include <iostream> #include <fstream> #include <string> #define TARGET2 "Hi" using namespace std; void changeFile(string

c++ fstream concurrent access

我的未来我决定 提交于 2019-12-07 09:38:15
问题 What will happen if files are accessed concurrently from different processes/threads? I understand there is no standard way of locking a file, only os specific functions. In my case files will be read often and written seldom. Now if A open a file for reading (ifstream) and starts reading chunks. And B opens the same file for writing (ofstream) and starts writing. What will happen? Is there a defined behavior? edit My goal is concurrent read, write access to many files. But write access will

C++: reading from a file with null characters

早过忘川 提交于 2019-12-07 08:17:21
问题 I have to read data from a file for an assignment unfortunately instead of spaces separating the various fields there are null characters. When taking integers from the file they are extracted fine however with the strings i just get a blanks space and garbage from my uninitialized character array. Any ideas as how to just extract the characters into my character array ignoring the null characters. EDIT: char fName[15],lName[15],pMethod[5],roomType[10],purpose[15]; int days, roomNum; long

c++文件流基本用法(fstream, ifstream, ostream)

青春壹個敷衍的年華 提交于 2019-12-07 07:47:20
前言: c++的文件流处理其实很简单,前提是你能够理解它。文件流本质是利用了一个buffer中间层。有点类似标准输出和标准输入一样。 c++ IO的设计保证IO效率,同时又兼顾封装性和易用性。本文将会讲述c++文件流的用法。 有错误和疏漏的地方,欢迎批评指证。 需要包含的头文件: <fstream> 名字空间: std 也可以试用<fstream.h> fstream提供了三个类,用来实现c++对文件的操作。(文件的创建,读写)。 ifstream -- 从已有的文件读 ofstream -- 向文件写内容 fstream - 打开文件供读写 支持的文件类型 实际上,文件类型可以分为两种: 文本文件和二进制文件. 文本文件保存的是可读的字符, 而二进制文件保存的只是二进制数据。利用二进制模式,你可以操作图像等文件。用文本模式,你只能读写文本文件。否则会报错。 例一: 写文件 声明一个ostream变量 调用open方法,使其与一个文件关联 写文件 调用close方法. #include <fstream.h> void main { ofstream file ; file . open ( "file.txt" ) ; file<< "Hello file /n " << 75 ; file . close ( ) ; } 可以像试用cout一样试用操作符<<向文件写内容.

How to read a file line by line to a string type variable?

旧街凉风 提交于 2019-12-07 05:46:26
问题 I'm trying to read a file line by line to a string type variable using the following code: #include <iostream> #include <fstream> ifstream file(file_name); if (!file) { cout << "unable to open file"; exit(1); } string line; while (!file.eof()) { file.getline(line,256); cout<<line; } file.close(); it won't compile when I try to use String class, only when I use char file[256] instead. how can I get line by line into a string class? 回答1: Use std::getline : std::string s; while (std::getline

What can go wrong if cout.rdbuf() is used to switch buffer and never set it back?

空扰寡人 提交于 2019-12-07 04:40:08
问题 The author presented this code under the title A bus error on my platform #include <fstream> #include <iostream> int main() { std::ofstream log("oops.log"); std::cout.rdbuf(log.rdbuf()); std::cout << "Oops!\n"; return 0; } The string "Oops!\n" is printed to the file "oops.log". The code doesn't restore cout's streambuf, but VS2010 didn't report a runtime error. 回答1: Since log and std::cout share a buffer, that buffer will probably be freed twice (once when log goes out of scope, then once