fstream

Using fstream tellg to read a portion of the stream till the end.

Hello: I have this simple code that needs to get a chunk of a large log file that is being written into. At some point it stores the current location returned from streampos start = istream::tellg(); method. Later on the code has to read from the stream a buffer from the start till the end. The code is approximately like this: streamp...

Possible reasons for tellg() failing?

ifstream::tellg() is returning -13 for a certain file. Basically, I wrote a utility that analyzes some source code; I open all files alphabetically, I start with "Apple.cpp" and it works perfectly.. But when it gets to "Conversion.cpp", always on the same file, after reading one line successfully tellg() returns -13. The code in questi...

what's the difference between opening a file with ios::binary or ios::out or both ?

hello i'm trying to figure out the difference between opening a file like: fstream *fileName*("FILE.dat",ios::binary); or fstream *fileName*("FILE.dat",ios::out); or fstream *fileName*("FILE.dat",ios::binary | ios::out); i found that all of these forms are identical,i.e. in all cases ,the same output on the file is produced usin...

in C++ files: what a file opened as an ios::binary differs from one opened as ios::binary | ios::out ?

if i opened a file like: ofstream file("file.dat",ios::binary); or ofstream file("file.dat",ios::binary | ios::out); what can i do with a file opened in the latter form that i can't do with the former form and vice versa thank you ...

Partially truncating a stream (fstream or ofstream) in C++

Hi, I am trying to partially truncate (or shorten) an existing file, using fstream. I have tried writing an EOF character, but this seems to do nothing. Any help would be appreciated... ...

Why can't I read and append with std::fstream on Mac OS X?

Consider the following C++ program, which takes a file and prints each line. It's a slice of a larger program where I later append to the file, based on what I see. #include <fstream> using std::fstream; #include <iostream> #include <string> using std::string; int main() { fstream file("file.txt", fstream::in | fstream::out | fstream:...

C++, read and write to a binary file at the same time

Hi I wanted to know if it's possible to read a byte from a binary file with "fstream" and then change that byte and write it back. I tried this code but it didn't work and nothing happens but I'm sure it reads correctly. file.open(path, ios::in|ios::out|ios::binary|ios::ate); file.seekg(0, ios::end); int size=file.tellg(); file.seekg(0,...

Get FILE* from fstream

Possible Duplicate: Getting a FILE* from a std::fstream Is there a way to obtain a FILE* from an a iostream derived class? Particularly from an fstream? ...

Returning ifstream in a function

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 ...

Using C++ filestreams (fstream), how can you determine the size of a file?

I'm sure I've just missed this in the manual, but how do you determine the size of a file (in bytes) using C++'s istream class from the fstream header? ...

C++ Newbie: Passing an fstream to a function to read data

I have a text file named num.txt who's only contents is the line 123. Then I have the following: void alt_reader(ifstream &file, char* line){ file.read(line, 3); cout << "First Time: " << line << endl; } int main() { ifstream inFile; int num; inFile.open("num.txt"); alt_reader(inFile, (char*)&num); cout << "...

c++: how can i read a file line by line to a string type variable?

Hiya. 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 co...

How do you search a document for a string in c++?

Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() { int count = 0; string fileName; string keyWord; string word; cout << "Please make sure the document is in the same file as the program, thank you!" << endl << "Please input document name: " ; ...

What's the correct way to do a 'catch all' error check on an fstream output operation?

What's the correct way to check for a general error when sending data to an fstream? UPDATE: My main concern regards some things I've been hearing about a delay between output and any data being physically written to the hard disk. My assumption was that the command "save_file_obj << save_str" would only send data to some kind of buffer...

C++ iostream not setting eof bit even if gcount returns 0

Hi I'm developping an application under windows, and i'm using fstreams to read and write to the file. I'm writing with fstream opened like this : fs.open(this->filename.c_str(), std::ios::in|std::ios::out|std::ios::binary); and writing with this command fs.write(reinterpret_cast<char*>(&e.element), sizeof(T)); closing the file a...

C++ File I/O problem

I am trying to open a file which normally has content, for the purpose of testing i will like to initialize the program without the files being available/existing so then the program should create empty ones, but am having issues implementing it. This is my code originally void loadFiles() { fstream city; city.open("city.txt", ios::...

Why doesn't ifstream read to the end?

I'm making a notepad-like program. To get the text from a file I read each character into the buffer by doing while (!file.EOF()) { mystr += file.get(); } however if I load in an exe it stops after MZ but Notepad reads the whole exe. I set my ifstream to binary mode but still no luck. What am I doing wrong? Thanks code: (messy) vo...

Problem with reading file line-by-line

I'm trying to complete an exercise to write a program that takes the following command line arguments: an input file, an output file, and an unspecified number of words. The program is to read the contents of the input file line by line, find for each word given which lines contain the word, and print the lines with their line number to ...

How to construct a c++ fstream from a POSIX file descriptor?

I'm basically looking for a C++ version of fdopen(). I did a bit of research on this and it is one of those things that seems like it should be easy, but turns out to be very complicated. Am I missing something in this belief (i.e. it really is easy)? If not, is there a good library out there somewhere to handle this? EDIT: here is a...

how to write any custom data type to file using ifstream?

as question says, i want to write custom data type data of a class maybe to a file using ifstream in c++. Need help. ...