fstream

filebuf in ifstream and ofstream

How can i set a new char array to be the buffer of a fstream's filebuf, there is a function (setbuf) in the filebuf but it is protected. while searching on the web, some sites mention fstream::setbuf but it doesn't seem to exist anymore. Thanks ...

C++: Everytime I read in by fstream I got 1 extra character at the end

Everytime I read in by fstream I got 1 extra character at the end, How can I avoid this? EDIT: ifstream readfile(inputFile); ofstream writefile(outputFile); char c; while(!readfile.eof()){ readfile >> c; //c = shiftChar(c, RIGHT, shift); writefile << c; } readfile.close(); writefile.close(); ...

C++ fstream variable

Hi, please, what contains the fstream variable? A can find many tutorials on fstream, but no ona actually says what is the fstream file; declaration in the beginning. Thanks. ...

C++ stringstream reads all zero's

I have a file which contains three integers per line. When I read the line I use a stringstream to separate the values, but it only reads the first value as it is. The other two are read as zero's. ifstream inputstream(filename.c_str()); if( inputstream.is_open() ){ string line; stringstream ss; while( getline(inputstream...

Strange fstream problem

Hi, I have really strange problem. In Visual C++ express, I have very simple code, just: #include <fstream> using namespace std; int main() { fstream file; file.open("test.txt"); file<<"Hello"; file.close(); } This same code works OK in my one project, but when I create now project and use this same lines of code, no file test.txt is ...

fstream file I/O question - when to close a file stream

Hi, I am trying to work out if I need to call close on a fstream object if the intial open failed. i.e. std::fstream strm; strm.open( "filename" ); if( ! strm.fail() ) { // Do something strm.close(); // [1] } strm.close(); // [2] Where should close be called here - should it always be called [2] or only if the open succ...

Beginner C++ - Opening a text file for reading if it exists, if it doesn't, create it empty

Hello everyone! I am writing a high-score sub-routine for a text-based game. Here's what I have so far. void Game::loadHiScores(string filename) { fstream hiscores(filename.c_str()); // what flags are needed? int score; string name; Score temp; if (hiscores.is_open()) { for (size_t i = 0; i < TOTAL_HISC...

Using a macro for fstream file input as part of a class

I have a class that processes a file, and as part of the constructor with one argument I want to input a file using fstream. I basically want it do something like this class someClass{ public: someClass(char * FILENAME) { fstream fileToProcess; fileToProcess.open(<FILENAME>, fstream::in | fstream:...

How to use fgets if you don't know the number of characters to be read?

I need to read a file and send the text from it to a string so I can parse it. However, the program won't know exactly how long the file is, so what would I do if I wanted to use fgets, or is there a better alternative? Note: char *fgets(char *str, size_t num, FILE *stream); ...

fstream in and out on non exsistant file.

Is it possible to open and fstream on a file that does not exist with both ios::in & ios::out with out getting error? Thanks in advance. ...

Deriving from streambuf without rewriting a corresponding stream

Hello, Some days ago, I decided that it would be fun to write a streambuf subclass that would use mmap and read-ahead. I looked at how my STL (SGI) implemented filebuf and realized that basic_filebuf contains a FILE*. So inheriting from basic_filebuf is out of the question. So I inherited from basic_streambuf. Then i wanted to bind my ...

Why do I get corrupt output on my file?

I have a simple program which I have compiled in both MinGW and Visual C++ 2008 Express, and both give an output file larger than 88200. When I set s = 0, both programs work as expected. What am I doing wrong? #include <fstream> using namespace std; int main(int argc, char *argv[]) { int i; short s; fstream f; f.op...

How to count lines of a file in C++?

Hi, In C++, how can I count lines using the standard classes fstream, ifstream? Thank you, Mohammad ...

Read and output possible unicode torrent contents in C++?

I'm trying to write a simple C++ program to open a torrent file (Passed through argv[1]), read all of it, and then print the entire file's contents verbatim with no alterations, it has to print a carbon copy of the original torrent. The issue is, some of the torrents may contain Japanese, Russian, etc. (FIlenames, description, etc.)... A...

Incorrect data input with fstream

I tried to read in data from a text file using fstream but got wrong data. ifstream fin ("C:\\Users\\rEgonicS\\Documents\\test.in"); int number; fin >> number; cout << number; test.in is simply "12". cout reads 4273190. Can someone explain why this is so and how to fix it? Thanks in advance. *Stackoverflow thought I was a bot ; o ;...

Where to place file in order to read?

Hey, where do I place a text file that I'm trying to read using fstream? In this tutorial, http://www.gamedev.net/reference/articles/article1127.asp, they say ifstream fin("input.txt"); where would "input.txt" be located? Before I tried directing a path to the file by doing this "C:\Users\XXXXXXX\Documents\test.in". This however does n...

What permissions does a file written with fstream have?

Suppose I create a file for writing like this: std::ofstream my_file("filename", std::ios_base::out | std::ios_base::trunc); How are the permissions of this file determined? I've had a program running overnight generating files about once a minute - some are 0644 but others are 0660, and there's nothing in my code that should make it ...

C++ header file variable scope issue

Hi, I've got 3 files that relate to this problem. file.h, file.C and user.C. file.h has a private member, fstream logs. In file.C's constructor it opens logs. It doesn't do this in the constructor, but the constructor calls a function OpenLog(). file.h also has an inline close function: CloseLog() {if (logs) logs.close();} The f...

Problem in Reading a Stream

Hi, I'm using fstream to read a stream from a binary file. I use the method get(). fstream f1("Log.dat", ios::in | ios::binary); char zMsgSize[MSG_SIZE]; zMsgSize[0] = '\0'; f1.get(zMsg, CHR_END); CHR_END is a special terminating character that I have put in the file earlier. There are null characters in the file I'm reading, that'...

C++ ifstream UTF8 first characters

Why does a file saved as UTF8 (in Notepad++) have this character in the beginning of the fstream I opened to it in my c++ program? ´╗┐ I have no idea what it is, I just know that it's not there when I save to ASCII. UPDATE: If I save it to UTF8 (without BOM) it's not there. How can I check the encoding of a file (ASCII or UTF8, ev...