fstream

C++: Reading from text file until EOF repeats last line

The following C++ code uses a ifstream object to read integers from a text file (which has one number per line) until it hits EOF. Why does it read the integer on the last line twice? How to fix this? Code: #include <iostream> #include <fstream> using namespace std; int main() { ifstream iFile("input.txt"); // input.txt has intege...

mmap() vs. reading blocks

I'm working on a program that will be processing files that could potentially be 100GB or more in size. The files contain sets of variable length records. I've got a first implementation up and running and am now looking towards improving performance, particularly at doing I/O more efficiently since the input file gets scanned many times...

Getting a FILE* from a std::fstream

Is there a (cross-platform) way to get a C FILE* handle from a C++ std::fstream ? The reason I ask is because my C++ library accepts fstreams and in one particular function I'd like to use a C library that accepts a FILE*. ...

using fstream to read every character including spaces and newline

I wanted to use fstream to read a txt file. I am using inFile >> characterToConvert, but the problem is that this omits any spaces and newline. I am writing an encryption program so i need to include the spaces and newlines and was wondering what would be the proper way to go about accomplishing this. Thanks in advance, Tomek ...

Fstreams C++

Hello I want the ofstream to write at the end of a file without erasing its content inside. how can i do it? (i'm talking about txt files,and C++) Thanks. ...

How do I read a text file from the second line using fstream?

Hello, how can i make my fstream object start reading a txt file from the second line? Thanks. ...

Why can't c++ ifstreams read from devices?

I knew I should never have started using c++ io, the whole "type safety" argument is a red herring (does anyone really find that it's one of their most pressing problems?). Anyhow, I did, and discovered a strange difference between ifstreams and FILE*s and plain old file descriptors: ifstreams cannot read from a device. Can anyone think ...

How can I set the flag FILE_FLAG_BACKUP_SEMANTICS for an fstream object?

How can I set the flag FILE_FLAG_BACKUP_SEMANTICS for an fstream object? You can set most flags with fstream, but it seems like this one is not availble. You can read about the flag here. ...

Doubt in C++ file handling

Following code, when compiled and run with g++, prints '1' twice, whereas I expect '1' to be printed only once, since I am dumping a single structure to the file, but while reading back it seems to be reading two structures. Why? #include <iostream.h> #include <fstream.h> int main(){ struct student { int rollNo; }; ...

Error in outputting to a file in C++ that I can't find

Not as in "can't find the answer on stackoverflow", but as in "can't see what I'm doing wrong", big difference! Anywho, the code is attached below. What it does is fairly basic, it takes in a user created text file, and spits out one that has been encrypted. In this case, the user tells it how many junk characters to put between each ...

[C++] Detect newline byte from filestream

I'm trying to collect information from a textfile which contains names of organisations (without spaces) and floating integers. I want to store this information in an array structure. The problem I'm having so far is collecting the information. Here is a sample of the textfile: CBA 12.3 4.5 7.5 2.9 4.1 TLS ...

What am I doing wrong with my serializing a vector with structs in it to a .dat file?

If I type in Description: Apple Quantity: 10 Wholesale Cost: 30 Retail Cost: 20 Date Added: December These are the contents in my .dat file: 1Apple103020December But when I load my program, it doesn't load the struct back in correctly resulting in there being 0 items in my list. Is that what it is suppose to look like or am I do...

Why won't my ofstream work when I put it outside my while statement?

Every time I do anything, and my while(1) gets called in my main function, my file gets cleared. It's driving me crazy. I've tried EVERYTHING. I try to put my ofstream out("data.dat"); outside the while(1) statement so it isn't called everytime but then nothing is written to the file like the ofstream isn't even working! I've tried to m...

How to write bitset data to a file?

I have a std::bitset that I'd like to write to a file, bit for bit, but of course fstream's write function doesn't support this. I can't think of another way besides converting each 8-bit group to a char using string and writing that... Anyone know of a good way? ...

fstream replace portion of a file

When I do fstream someFile("something.dat", ios::binary|ios::out); someFile.seekp(someLocation, ios::beg); someFile.write(someData, 100); It seems to replace the entire file with those 100 bytes instead of replacing only the appropriate 100 bytes, as if I had specified ios::trunc. Is there a portable way to not have it truncate the f...

Trouble with seekp() to replace portion of file in binary mode

Hi, I'm having some trouble with replacing a portion of a file in binary mode. For some reason my seekp() line is not placing the file pointer at the desired position. Right now its appending the new contents to the end of the file instead of replacing the desired portion. long int pos; bool found = false; fstream file(fileName, ios::b...

C++ adding a carriage return at beggining of string when reading file

I have two questions: 1) Why is my code adding a carriage return at the beggining of the selected_line string? 2) Do you think the algorithm I'm using to return a random line from the file is good enough and won't cause any problems? A sample file is: line number one # line number two My code: int main() { srand(time(0)); i...

What is the cause of this error message: "...fstream(934)...cannot access private member..."? (C++)

When I compile a program in C++ (that is too large to simply cut abd paste here), I get this error: c:\program files\microsoft visual studio 9.0\vc\include\fstream(934) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>' What does this error mean, wh...

What does a ten digit return number mean? (C++)

When I run a program using the read part of fstream, I get this return value: -1073741819 the actual function is part of a corrupt for loop, which I will try to explain: for(int i = 0; i < vrs_top_i * 3; i += 3) { int X1x = FileRead(file2, i + 1); int X1y = FileRead(file2, i + 2); char X1sym = FileRead(file2, i + 3); viral_data.a...

working with fstream files in overflow chaining in c++

Hello, I have a file that I want to read and write to a binary file using records. In the beginning I have an empty file and I want to add new record, but when I use the seekp function, then the location is at (-1) is it ok? Because when I check, I see that it hasnt written anything to the file. See code: void Library::addBook(Book ne...