Questions tagged [fstream]

fstream provides an iostream interface for file I/O in C++.

The C++ standard library type std::fstream is an iostream type for reading and writing files.

The objects of this class internally maintain a pointer to a std::filebuf object (derived from std::streambuf) that can be obtained/modified by calling the rdbuf member function. The filebuf transfers characters to/from a file and performs any necessary conversion between the on-disk format and the in-memory representation.

For questions specific to fstream use this tag or , , or for more general questions use , , or .

2840 questions
49
votes
8 answers

How to use fstream objects with relative path?

Do I always have to specify absolute path for objects instantiated from std::fstream class? In other words, is there a way to specify just relative path to them such as project path?
user336359
  • 1,244
  • 1
  • 11
  • 18
49
votes
5 answers

clear data inside text file in c++

I am programming on C++. In my code I create a text file, write data to the file and reading from the file using stream, after I finish the sequence I desire I wish to clear all the data inside the txt file. Can someone tell me the command to clear…
Zeyad
  • 771
  • 3
  • 9
  • 14
43
votes
8 answers

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

How can I count lines using the standard classes, fstream and ifstream?
malhobayyeb
  • 2,725
  • 11
  • 57
  • 91
38
votes
3 answers

std::fstream doesn't create file

I am trying to use std::fstream for io to file, and I want to create the file if it doesn't already exist. std::fstream my_stream my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out); if(!my_stream) …
herzl shemuelian
  • 3,346
  • 8
  • 34
  • 49
38
votes
6 answers

std::ofstream, check if file exists before writing

I am implementing file saving functionality within a Qt application using C++. I am looking for a way to check to see if the selected file already exists before writing to it, so that I can prompt a warning to the user. I am using an std::ofstream…
cweston
  • 11,297
  • 19
  • 82
  • 107
34
votes
4 answers

Best way to split a vector into two smaller arrays?

What I'm trying to do: I am trying to split a vector into two separate arrays. The current int vector contains an element per line in a text file. The text file is a list of random integers. How I'm planning to do it: My current idea is to…
mightcouldb1
  • 649
  • 2
  • 8
  • 14
33
votes
13 answers

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. What would be the proper…
Tomek
30
votes
4 answers

Reading line from text file and putting the strings into a vector?

I am trying to read each line of a textfile which each line contains one word and put those words into a vector. How would i go about doing that? This is my new code: I think there is still something wrong with it. #include #include…
user977154
  • 1,045
  • 4
  • 19
  • 39
30
votes
2 answers

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

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…
Alan_AI
  • 1,529
  • 3
  • 22
  • 32
30
votes
4 answers

how to correctly write vector to binary file in c++?

first sorry for my bad english. i am just joined this forum and search for how to correctly write vector to binary file. i just got from this forum an answer like this(i have modified it a little): #include #include #include…
dchochan
  • 403
  • 1
  • 4
  • 5
29
votes
3 answers

fstream won't create a file

I'm simply trying to create a text file if it does not exist and I can't seem to get fstream to do this. #include using std::fstream; int main(int argc, char *argv[]) { fstream file; file.open("test.txt"); file << "test"; …
raphnguyen
  • 3,565
  • 18
  • 56
  • 74
28
votes
5 answers

In C++ is there a way to go to a specific line in a text file?

If I open a text file using fstream is there a simple way to jump to a specific line, such as line 8?
Jim
  • 281
  • 1
  • 3
  • 3
27
votes
5 answers

Copy data from fstream to stringstream with no buffer?

Is there anyway I can transfer data from an fstream (a file) to a stringstream (a stream in the memory)? Currently, I'm using a buffer, but this requires double the memory, because you need to copy the data to a buffer, then copy the buffer to the…
Brad
  • 10,015
  • 17
  • 54
  • 77
27
votes
2 answers

are "seekp" & "seekg" interchangeable?

Well I just noticed that by changing the position -in microsoft visual studio- through "seekp" I implicitelly also change the read-position, when handling files. I am wondering however if this is "portable" behaviour? Can I expect the position of…
paul23
  • 8,799
  • 12
  • 66
  • 149
26
votes
1 answer

fstream seekg(), seekp(), and write()

I'm looking for some clarification on how seekg() and seekp() works with respect to when you are writing to a file. Say for instance I had a file like so: offset 0: 2 offset 4: 4 offset 8: 6 offset 12: 8 offset 16: 10 Now I want to open the file…
raphnguyen
  • 3,565
  • 18
  • 56
  • 74