ifstream

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

Vector of ifstreams

How can I create and manipulate a vector of ifstreams? Something like this, except this doesn't work: vector<ifstream> Files(10, ifstream()); Files[0].open("File"); ...

How to use different ifstream modes in c++?

According to the reference, if I use ifstream infile ( "test.txt" , ifstream::in ); it will Allow input operations on the stream. But what are some of the examples of the "input operations"? Is ifstream infile ( "test.txt" , ifstream::in | ifstream::binary ); the right syntax to use multiple flags? Will it make a difference if I change...

Reading through file using ifstream

I am trying to read from file: The file is multiline and basically i need to go over each "word". Word being anything non space. Sample input file would be: Sample file: test 2d word 3.5 input { test 13.5 12.3 another { testing 145.4 } } So I tried something like ...

Can an ifstream variable be a global variable?

// stream from file. ifstream file; int main (int argc, char * argv[]) { // get argument passed from command line // This is file name if (argc != 2 ) { cout << "use: ./executable <filename>"; }else { //cout << "You are using filename: " << argv[1]; // start the file stream file (argv[1]); } Is there any reason why file(argv[1...

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

Maintaining a valid position using seekg in ifstreams

I am trying to make my file parsing more robust. Using an ifstream, how can I ensure seekg keeps me in a valid position within the file? This does not work: while(m_File.good() && m_File.peek() != EOF) { ...a seekg operation moves file position past end of file... } I assume the current iterator has been pushed way past the end iter...

How come I am getting weird results with istream::get(char*, streamsize n, char delim)?

I am reading in a file with a format similar to: TIME, x, y, z 00:00:00.000 , 1, 2 , 3 00:00:00.001 , 2 , 3 , 4 etc, and code similar to the following: std::ifstream& istream; char buffer[15]; double seconds, hours, mins; // initialised properly in real code // to read in first column istream.get(buffer, 14, ','); int scanned = std...

C++ - Failed loading a data file !

I have a simple data file that I want to load, in a C++ program. For weird reasons, it doesn't work: I tried it on Windows assuming the file was in the same directory: failed. I tried it on Windows by moving the file in C:\ directory: worked. I tried it on Linux putting the file in the same directory: failed. The snippet: void World...

cant get ifstream to work in XCode

No matter what I try, I cant get the following code to work correctly. ifstream inFile; inFile.open("sampleplanet"); cout << (inFile.good()); //prints a 1 int levelLW = 0; int numLevels = 0; inFile >> levelLW >> numLevels; cout << (inFile.good()); //prints a 0 at the first cout << (inFile.good());, it prints a 1 and at the second a 0....

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 does ifstream::rdbuf() actually do?

I have the following code and it works pretty good (other than the fact that it's pretty slow, but I don't care much about that). It doesn't seem intuitive that this would write the entire contents of the infile to the outfile. // Returns 1 if failed and 0 if successful int WriteFileContentsToNewFile(string inFilename, string outFilenam...

C++: assign cin to an ifstream variable?

You know the common stdio idiom that stdin is specified by a filename of "-", e.g. if ((strcmp(fname, "-")) fp = fopen(fname); else fp = stdin; What's the best way to do this with an ifstream instance? I've received a bit of code that has an ifstream as part of a class and I'd like to add code to do the equivalent, something ...

declaring generic istream in c++

I need to write a program that reads in either from ifstream or cin, depending on parameters passed into the program at runtime. I was planning on doing the following: istream in; if(argv[1] == "cin") { in = cin; } else { ifStream inFile; inFile.open(argv[1].c_str()); in = inFile; } However, istream in...

why are special characters read false from file

Hi my program saves some settings (mostly string) to a text file, to retrieve them later, but alas! The special characters come back unrecognizable! saveSettings saves the strings one by one... void email::saveSettings(string filename){ ofstream savefile(filename.c_str(),ios::out | ios::trunc); email settingsemail(this); s...

Passing ifstream to a function in C++

I'm trying to build 16 different suffix trees for a gene sequencing project. They're being built in main as such int main() { ifstream fp; fp.open("filepath", ifstream::in); Tree I(fp); fp.close(); I'm attempting to use them in my constructor with this code: Tree::Tree(ifstream &fp) { string current = ""; ...

C++ overload >> for ifstream on mac pointer being freed was not allocated

Hello friends, I am trying the following code and it fails with the following error: malloc: *** error for object 0x10000d8c0: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug Program received signal: “SIGABRT”. Here are contents of file input.txt : It has full permissions and file is success...

Why can't I create a std::stack of std::ifstreams?

Why does the following not work: #include <iostream> #include <fstream> #include <stack> std::stack<std::ifstream> s; -PT ...

Compact pointer notation for opening files

I'm writing a program to open up multiple files given at the command line. I first did it with array notation. That seems to work. Now I am trying to use compact pointer notation for practice and getting used to pointers, but I am not doing it right. Anyone want to tell me what I'm doing wrong? Thanks. #include <cstdlib> #include <f...

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