fstream

std::fstream files more than 2gb

what strategy should i use if i have an implimentation of std::fstream with 32-bit std::streampos? If i want to move position i can do it in several steps(10gb - 10 times +1gb). How can i get position? Or should i keep current position in some variable outside fstream? ps i can't change the implimentation of stl. ...

Redirecting C++ fstream

So I have a C++ program, that normally when it executes writes out things to a log file. This is done using an fstream. However, now I want to include functionality to turn the logging off. Essentially, my code looks like this: bool isLogging; fstream* logFilePtr; throughout my code, there are a bunch of statements like: (*logFile...

Keep a stream from fstream open through member functions

Hi, I am trying to keep a stream to a file /dev/fb0 (linux framebuffer) open throughout several Qt member functions. The goal is to use a myscreen::connect function to open up the framebuffer bool myscreen::connect() { std::fstream myscreen_Fb; myscreen_Fb.open("/dev/fb0") QImage* image; image = new QImage(w, h, QImage::Format_RGB...

Load binary file using fstream

I'm trying to load binary file using fstream in the following way: #include <iostream> #include <fstream> #include <iterator> #include <vector> using namespace std; int main() { basic_fstream<uint32_t> file( "somefile.dat", ios::in|ios::binary ); vector<uint32_t> buffer; buffer.assign( istream_iterator<uint32_t, uint32_t>...

Writing to default-constructed fstream: Undefined Behavior?

Consider the following: std::basic_fstream<char> testfile; testfile.write(reinterpret_cast<const char*>(&someInt), sizeof(int)); testfile.close(); This runs with no complaint when built with VC 8.0, but crashes when built with VC 10.0 beta. I have some legacy code that actually relies on the VC 8 behavior, where we inherit from basic...

How do you output variable's declared as a double to a text file in C++

I am very new to C++ and I am wondering how you output/write variables declared as double to a txt file. I know about how to output strings using fstream but I cant figure out how to send anything else. I am starting to think that you can't send anything but strings to a text file is that correct? If so then how would you convert the inf...

Problem with file stream/fstream in Xcode C++

Here is a simple program to output to a text file: #include <iostream> #include <fstream> using namespace std; int main() { double myNumber = 42.5; fstream outfile("test.txt", fstream::out); outfile << "The answer is almost " << myNumber << endl; outfile.close(); } All that ends up being wrote to my text file is, "The answer is almo...

How to check if a file exists and is readable in C++?

I've got a fstream my_file("test.txt"), but I don't know if test.txt exists. In case it exists, I would like to know if I can read it, too. How to do that? I use Linux. ...

writing binary data

Hello, I am writing to binary file using fstream and when open the file using binary flag. I needed to write some text as binary, which a simple write did the trick. The problem is that I need also to write (as shown in hexadecimal) 0. The value when opened in binary notepad is shown zero, but when tried to write this the value not zer...

Where does Visual Studio search for txt files when conducting file management operations?

I know this is a noob question, but I've worked with Python before and when you wanted to simply access a .txt file for example, all you had to do was make sure the txt file was in the same directory. I have the following C++ code below but it's not finding the Numbers.txt file that I have saved on my desktop. All I have in the file is o...

C++ Storing objects in a file

I have a list of objects that I would like to store in a file as small as possible for later retrieval. I have been carefully reading this tutorial, and am beginning (I think) to understand, but have several questions. Here is the snippet I am working with: static bool writeHistory(string fileName) { fstream historyFile; historyFi...

ifstream position in c++

Hi, I am trying to write a simple UTF-8 decoder for my assignment. I'm fairly new with C++ so bear with me here... I have to determine whether the encoding is valid or not, and output the value of the UTF-8 character in hexadecimal in either case. Say that I have read the first byte and used this first byte to determine the number of b...

Unable to open fstream when specifying an absolute path

Hey everyone! I know this is rather laughable, but I can't seem to get simple C++ ofstream code to work. Can you please tell me what could possibly be wrong with the following code: #include <fstream> ... ofstream File("C:\temp.txt"); if(File) File << "lolwtf"; Opening the ofstream fails whenever I specify ...

Reading files and deleting repeated letters

Hello! So my goal is to make a function that has a partially filled array of characters as a formal parameter and deletes all repeated letters from the array. So I just need to read a .txt file with it's contents as something like "11 A B C a b c a A g g t " and have the program spit back out "A B C a b c g t" As of now my program spits...

C++ text file pointer problems

I am writing a function which should (if the file already exists) increment the first number by one and append the parameters of the function to the end of the file. Example: append (4,9); append (5,6); File contents at 1: 1 \n 4 \n 9 File contents at 2: 2 \n 4 \n 9 \n 5 \n 6 int append (int obj, int objType) { ifstream infile; ...

Writing binary files using C++: does the default locale matter?

I have code that manipulates binary files using fstream with the binary flag set and using the unformatted I/O functions read and write. This works correctly on all systems I've ever used (the bits in the file are exactly as expected), but those are basically all U.S. English. I have been wondering about the potential for these bytes to ...

Loading text from a file into a 2-dimensional array (C++)

I'm making a game and I have stored the map data in a 2-dimensional array of size [34][10]. Originally I generated the map using a simple function to fill up the array and saved this data to a file using the following code: ofstream myFile; myFile.open("map.txt"); for ( int y = 0 ; y < MAP_HEIGHT ; ++y ) { for ( int x = 0 ; x < MAP_WI...

Copying Part of Fstream to Istringstream

After a lot searching for solutions I've decided to actually ask for some help! I have a file that consists of a number of blocks, where each block may or may not be compressed. Before each block is an indication of the size of the block and whether the block is compressed. Within each block is a string that identifies the block. Giv...

Writing concurrently to a file

I have this tool in which a single log-like file is written to by several processes. What I want to achieve is to have the file truncated when it is first opened, and then have all writes done at the end by the several processes that have it open. All writes are systematically flushed and mutex-protected so that I don't get jumbled outp...

fstream skipping characters without reading in bitmap

I am trying to read a bmp file using fstream. However it skips the values between 08 and 0E (hex) for example, for values 42 4d 8a 16 0b 00 00 00 00 00 36 it reads 42 4d 8a 16 00 00 00 00 00 36 skipping 0b like it does not even exist in the document. What to do? code: ifstream in; in.open("ben.bmp", ios::binary); unsigned char a='\...