ifstream

extraneous empty lines when reading from an ifstream

In my program, I've redirected stdout to print to a file 'console.txt'. A function writes to that file like this: void printToConsole(const std::string& text, const TCODColor& fc, const TCODColor& bc) { // write the string cout << text << "@"; // write the two color values cout << static_cast<int>(f...

strange garbage data when opening a previously truncated file

In my program, I've redirected stdout to print to a file 'console.txt'. A function writes to that file like this: void printToConsole(const std::string& text, const TCODColor& fc, const TCODColor& bc) { // write the string cout << text << "@"; // write the two color values cout << static_cast<int>(f...

how do I open and read a file using ifstream in C++?

I would like to open a file and read a line from it. There will be only one line in the file so I don't really need to worry about looping, although for future reference it would be nice to know how to read multiple lines. int main(int argc, const char* argv[]) { // argv[1] holds the file name from the command prompt int numbe...

Best way to parse a large floating point file stored in ascii?

What would be the fastest way to do it? I remember someone telling me using ifstream was bad because it worked on a small number of bytes, and it would be better to just read the file into memory first. Can anyone confirm this? Thanks Edit: I am running on windows, and the file format is for a point cloud that is stored in rows like ...

how can I read exactly 128 bytes from an fstream into a string object?

How do I read exactly 128 bytes from an fstream into a string object? I wrote some code to read the first 128 bytes of a file and print it and then the last 128 bytes of the file and print that. The last part works, since you can easily iterate to EOF, but how do I get exactly 128 bytes from the front? The code below doesn't work since ...

How to read formatted data in C++?

I have a formatted data like the following: Words 5 AnotherWord 4 SomeWord 6 It's in a text file and I'm using ifstream to read it, but how do I separate the number and the word? The word will only consist of alphabets and there will be certain spaces or tabs between the word and the number, not sure of how many. ...

Find the end of stream for cin & ifstream?

Hi, I'm running myself through a C++ text book that I have as a refresher to C++ programming. One of the practice problems (without going into too much detail) wants me to define a function that can be passed ifstream or cin (e.g. istream) as an argument. From there, I have to read through the stream. Trouble is, I can't figure out a wa...

Using getline when reading from a textfile

I'm initially building an index-like mechanism, read each line of the textfile using getline, checking if it matches a known header declaration (string.compare) and saving the tellg position as an index to that point. My intention is then to use seekg(index,ios::beg) to seek to the place in the file where the header is. After reading th...

Do I need to define ">>" operator to Use cin With Int32's ?

I need to read exactly 32 bits from a file. I'm using ifstream in the STL. Can I just directly say: int32 my_int; std::ifstream my_stream; my_stream.open("my_file.txt",std::ifstream::in); if (my_stream && !my_stream.eof()) my_stream >> my_int; ...or do I need to somehow override the >> operator to work with int32? I don't see t...

parse an unknown size string

I am trying to read an unknown size string from a text file and I used this code : ifstream inp_file; char line[1000] ; inp_file.getline(line, 1000); but I don't like it because it has a limit (even I know it's very hard to exceed this limit)but I want to implement a better code which reallocates according to the size of the coming s...

Return value of ifstream.peek() when it reaches the end of the file

I was looking at this article on Cplusplus.com, http://www.cplusplus.com/reference/iostream/istream/peek/ I'm still not sure what peek() returns if it reaches the end of the file. In my code, a part of the program is supposed to run as long as this statement is true (sourcefile.peek() != EOF) where sourcefile is my ifstream. Howeve...

ifstream::unget() fails. Is MS' implementation buggy or is my code erroneous?

Yesterday I discovered an odd bug in rather simple code that basically gets text from an ifstream and tokenizes it. The code that actually fails does a number of get()/peek() calls looking for the token "/*". If the token is found in the stream, unget() is called so the next method sees the stream starting with the token. Sometimes, see...

C++ std::ifstream in constructor problem

Hello. I've got a problem with this code: #include <fstream> struct A { A(std::ifstream input) { //some actions } }; int main() { std::ifstream input("somefile.xxx"); while (input.good()) { A(input); } return 0; } G++ outputs me this: $ g++ file.cpp file.cpp: In function `int mai...

istream not working in DEV C++

Hi. I'm using ifstream and ofstream operations in DEV c++ but they don't seem to work correctly. I've been trying to write a little prime generator code but it doesn't work :\ When I display fstream::tellg() at any point, it displays -1. Please check the code. Thanx. #include<iostream> #include<math.h> #include<fstream> using namespace ...

File extraction doesn't seem to advance to the next word in the file

I think this has happened to me before. This isA3.txt: %INSERT MARK 29 DAVID 21 JOHN 44 JOHN 51 LARRY 39 MARK 21 DAVID 18 JOHN 28 MARK 35 DONALD 41 PHIL 26 Even though I use sourcefile >> reader at the end of the loop, the program keeps outputting "reader: MARK", meaning the sourcefile >> reader; statement isn't working (i.e., it kee...

ifstream fails to open in recursive calls

hello. we are running into an odd issue when trying to parse an input file. the idea is that this file can include other files, which must be parsed as well. We are doing this recursively in a function defined as int parse_inp(const char* filename) The main file parses no problem, but recursive calls cannot open their file streams. ...

C++ version of isspace (Convert code to C to C++)

I am converting code from C to C++. I am currently using the C function, isspace, what is the C++ equivalent when using an ifstream? Specifically while (!isspace(lineBuffer[l])) id is the first the number (2515, 1676, 279) and name is the set of letters after the first "space" (ABC, XYZ, FOO). Example NewList.Txt 2515 ABC 23.5 3...

Any reason why an std::ofstream object won't close properly?

I noticed in my C++ code that anytime I close an std::ofstream object I'm unable to reopen the file I closed with std::ifstream. std::ifstream's open function will always fail. Is there anything 'extra' I can do to ensure that my std::ofstream object closes properly? Someone's probably going to ask to see my specific code so for the sa...