FAQ Database Discussion Community


ifstream and ofstream or fstream using in and out

c++,fstream,ifstream,ofstream
When dealing with files, which of the two examples below is preferred? Does one provide better performance than the other? Is there any difference at all? ifstream input("input_file.txt"); ofstream output("output_file.txt"); vs fstream input("input_file.txt",istream::in); fstream output("output_file.txt",ostream::out); ...

Vector of input streams?

c++,vector,ifstream,cin,istream
Im looking to create a vector that stores a list of different input streams including cin and a few text files. So far I have this. vector<istream> inStreams; vector<istream>::iterator streamsIterator; streamsIterator = inStreams.begin(); streamsIterator = inStreams.insert(streamsIterator, cin); ////this has error! for (char i = 1; argv[i] != NULL; i++) {...

C++ getline segmentation fault, std::vector

c++,ifstream,stdvector,getline
I've been looking at this for a while in the debugger while Googling around, but I think I've stumbled upon some C++ behavior that I'm not familiar with. I'm going to give a quick outline on what I'm doing and what/where the problem is. I'll put code block below. The...

Is it better to read an entire file in std::string or to manipulate a file with std::ifstream?

c++,string,ifstream
I am actually developing scientific C++ simulation programs which read data, compute lots of values from them and finally store the results in a file. I wanted to know if reading all the data at once at the beginning of the program is faster than keep accessing the file via...

ifstream attempting reference to a deleted function

c++,c++11,ifstream
I'm writing a code for a virtual tournament. The problem is that team class has an ifstream object, I understand that stream objects do not have copy constructors, therefore i converted playing8 from vector of team objects to pointer to object, So that team objects will not be copied.But now...

Fstream is draining the life out of me

c++,fstream,ifstream,ofstream
I have a code writing assignment for my computer class and it includes fstream classes. I have to write a code that manages and stores user data. I had a hard time understanding this concept in class and C++ text books just want to keep showing me IPO charts not...

segmentation fault by overloading operators with ifstream returning

c++,file,ifstream
I wrote this program in c++ in ubuntu. I wrote 2 operator overloadings like bellow. But I received "segmentation fault(core dumped)". What should I do? #include<iostream> #include<fstream> using namespace std; class Complex{ private: double x; double y; public: Complex(double a,double b){ x=a; y=b; } void setx(double a){ x=a; } void...

C++: Input file (data of unknown size) into vector issue

c++,vector,filestream,ifstream
I'm trying to put a file with two columns of data, x and y, into two vectors, one only containing x and the other only containing y. No column headers. Like this: x1 y1 x2 y2 x3 y3 However, when I run this code, I encounter an error: (lldb) Can...

Compiler throws an exception when trying to compile code containing binary ifstream

c++,ifstream
I am running into a problem with the accessing a binary file via the input file stream class (ifstream). My approach starts with the following calling function: void ReadFile(vector<string>& argv, ostream& oss){ string FileName = argv.at(2) + "INPUT" ; ifstream BinFile ; OpenBinaryFile(FileName, BinFile) ; return ; } The called...

How do I stop reading a line when I encounter a whitespace?

c++,c,ifstream,readfile
I'm opening a file and reading it using getline(x,y) currently but I want to stop reading the line when I encounter a whitespace and then continue reading from there. Thanks

Reading from file in c++ ifstream

c++,oop,while-loop,ifstream
I am working on a program that reads from a file and pushes back the contents of that file into a vector. It will read until the file reaches a space and push that string into a vector, then continue after the space. I have written this code. ifstream inFile;...

Splitting string into smaller strings from text input

c++,string,ifstream
Reading in a text file to a C++ program I'm working on, and storing each string in a node for a double-linked list. Problem is, I don't know how to split up a line into smaller strings, separating them where the space is. For instance, one input is "Duck Donald...

c++: exception error after closing console app

c++,binary,ifstream,ofstream
I am in the process of learning c++ and have a question regarding reading and writing objects to files. I created a class called Person. In my main file I created two instances of the Person class. I write instance one to a file called "person.dat" and later read it...

How do I point to an input stream?

c++,pointers,iostream,ifstream,cin
Im trying to use a pointer to point to my desired input stream depending on user decision. This is what ive got so far. string fileName = "test.txt"; ifsteam = myFile; myFile.open(fileName.c_str(), ifstream::in); istream * myStream; if (file_mode) { myStream = &myFile; } else { myStream = &cin; } string...

ifstream stopped working in eclipse

c++,eclipse,ifstream
I have a C++ project that I haven't used in a year. It used to run perfectly, opening files with: Matrix pmatrix; pmatrix.readFromFile("pmtx.txt"); and in the function: void Matrix::readFromFile(string filename){ ifstream tmfile; tmfile.open(filename.c_str()); if (!tmfile) { cout << "unable to open tm file"; //return 1; } etc } It keeps...

Retrieving File Data Stored in Buffer

c++,performance,buffer,ifstream,large-files
I'm new to the forum, but not to this website. I've been searching for weeks on how to process a large data file quickly using C++ 11. I'm trying to have a function with a member that will capture the trace file name, open and process the data. The trace...

Read/Write to PPM Image File C++

c++,ifstream,ofstream,ppm
Trying to read and write to/from a PPM Image file (.ppm) in the only way I know how: std::istream& operator >>(std::istream &inputStream, PPMObject &other) { inputStream.seekg(0, ios::end); int size = inputStream.tellg(); inputStream.seekg(0, ios::beg); other.m_Ptr = new char[size]; while (inputStream >> other.m_Ptr >> other.width >> other.height >> other.maxColVal) { other.magicNum =...

how to call txt files from a directory one by one in c++ [duplicate]

c++,file-io,ifstream
This question already has an answer here: How do you iterate through every file/directory recursively in standard C++? 12 answers I know the number of files in my \data directory (n). I want to do something like that: #include <string> #include <fstream> ifstream myFile; string filename; for(int i=0;i<n;i++) {...

C++ - ifstream and array functions

c++,error-handling,ifstream
I have a problem with a function that is having ifstream and string in it. This my code: #include <iostream> #include <time.h> #include <iomanip> #include <stdlib.h> #include <fstream> #include <string> const int ArrayMax = 100; int DisplayMenu(); void LoadNames(); void ReadFile(ifstream& , ifstream& ,string[],string[]); using namespace std; int main() {...

C++ ifstream exception masks for C#

c#,c++,ios,exception,ifstream
How would I convert this line from C++ to C#? std::ifstream in; in.exceptions(std::ios_base::badbit); //*THIS LINE* According to this page, the previous line of code sets a new exception mask for the stream and clears any current error state flags. I'm also guessing that it is the "second form" of std::ios::exceptions...

How to write to middle of a file in C++?

c++,fstream,ifstream,ofstream
I think this should be quite simple, but my googling didn't help so far... I need to write to an existing file in C++, but not necessarily at the end of the file. I know that when I just want to append text to my file, I can pass the...

C++, getline() cant properly load all characters

c++,get,ifstream,getline
I have text file filled with characters similar to these: ¶§čç†8,ĄyŚ}¦PÜĎY®Všw†inmĘkÚhíU”*Q–­Ö}W"ĄőK=Ć“7ßńĎ_0§ĄŚĆš‰ö¤˝†’?šŕý î7ź®xż¶¸tÔuYÖa7Ź+6cE#U+:ç˛ů€°6%·˝Î÷Ľm‹¶ĺ¬‰,Y¤r$Ň“üDGhô‚J°ÜYŽäóa·7¶1Ľľ˝.— bőľaŇ•Ďűż3«ű4°JT.ń¨kŢ&Ś6ÄmĆŕY0Ż/´~9.źÍéşď®ŁýçľURíOQS ›iJ±bĄB“˝¸&§řőUdüió^˛l˛...

Problems of passing a const char* address of file to an ifstream object

c++,string,oop,ifstream
An ifstream object takes a const char*. I am giving my ifstream object the path of my file ifstream in_file("E:\\myfile.csv"); //File opens If i want to want to pass a string containing the address of my file i would have to convert it to a const char* first, i do...

C++ How to read txt file and retrieve numerical values, apart from string (and vice versa)

c++,fstream,ifstream
I have been teaching myself C++, and searching for how to do this. Let me give an example to clarify my intention. Here is a txt file, and the content is following. Matt 18 180.0 88.5 Angela 20 155.5 42.2 Each row has information about a person's name, age, height...

std::ifstream read reading wrong size for large numbers

c++,segmentation-fault,valgrind,ifstream
I have a Problem while reading a lare chunk of binary data from a file at once. Reading the same amount of bytes bytewise is working. I have to following sample code: std::ifstream inFile; inFile.open("example.bin", std::ios::binary | std::ios::in); uint32_t bytesToAllocate = static_cast<uint32_t>(this->sectionLength)-4; this->binaryData = new uint8_t[bytesToAllocate]; inFile.read(reinterpret_cast<char*>(&this->binaryData), bytesToAllocate); if i...

What is the difference between std::ifstream::in and std::ios::in? (obviously as an openmode flag for an std::ifstream in C++)

c++,ifstream
I used SymbolHound to look for ressources on the web comparing the two but couldn't find anything. Looking for std::ifstream::in's declaration and definition in VS13 brings me to basic_ifstream's. Looking for std::ios::in's brings me to basic_ios's. It seems std::ios::in is more widely used. Thanks in advance, Hans...

How to integrate c++ code into working LAPACK code

c++,ifstream,lapack
I have a very simple piece of C++ code justread.cc reading numbers from a file. After the command [email protected]:~/Desktop/tests$ g++ -Wall -pedantic -o justread.x justread.cc && ./justread.x it compiles without any errors or warnings. This is the code: #include <fstream> #include <vector> int read_covariance () { std::vector<double> data; double tmp;...

C++ Finding the number of times something appears in two elements a vector of structs

c++,vector,struct,ifstream
Sorry for the confusing title, not sure how to phrase it. Basically, I have a vector full of structures consisting of some information on teams that were in the finals of some sport. The structure looks like this: struct Year{ string year; string host; string winner; string score; string runnerUp;...

ifstream variable not reading correct character 1

c++,boolean,ifstream
I have an issue with the following code snippet. I have a file referred to with the character array FileName. The file could be basically anything, but in my case it is a file the contains some irrelevant text on the first line and then some lines that start with...

IO on file descriptors

c++,linux,file,ifstream,file-descriptor
I just ran into the following behavior and would like to know the reasons behind it: Assume a simplified program like that ... { std::ifstream in(argv[1]); assert(in.good()); while (std::getline(in, line)) { // Area 1 } in.close(); } { std::ifstream in(argv[1]); assert(in.good()); while (std::getline(in, line)) { // Area 2 } in.close();...

How to write from file to string

c++,file,ifstream
I am new to C++ and I'm having trouble understanding how to import text from a file. I have a .txt file that I am inputting from and I want to put all of the text from that file into a string. To read the text file I am using...

ifstream no conversion from char to char exists error

c++,char,ifstream,getline,c-strings
int main() { char buffer[1024]; ifstream dataFile ("./data.dat"); while(buffer) { localHouse->location = dataFile.getline(buffer, 1024); } } This throws the error: No suitable converion function from "std::basic_istream<char, std::char_traits<char>>" to "char" exists. It continues to throw this error if I use a pointer to buffer instead. as far as I can...

ifstream.read only reads half the file

c++,file,ifstream
I'm trying to make a simple image converter (ppm format to a custom one) and i'm having a problem with the ifstream.read method. Despite having this: int rows,cols, maxV; char header [100], *ptr; std::ifstream im; //open image in binary format im.open(name.c_str(), std::ios::in | std::ios::binary); if (!im) { std::cout << "Can't...

ifstream crashing program if file contains long words (not extreme)

c++,fstream,ifstream
im having this weird situation - im trying to read words from a file. #include <iostream> #include <string> #include <fstream> #include <stdio.h> int main(int argc, const char* argv[]) { if (argc != 2) { std::cout << "bad number of arguments" << std::endl; return 1; } std::cout << "trying to open...