ifstream

Any way to get rid of the null character at the end of an istream get?

谁说胖子不能爱 提交于 2019-12-12 03:56:09
问题 I'm currently trying to write a bit of code to read a file and extract bits of it and save them as variables. Here's the relevant code: char address[10]; ifstream tracefile; tracefile.open ("trace.txt"); tracefile.seekg(2, ios::beg); tracefile.get(address, 10, ' '); cout << address; The contents of the file: (just the first line) R 0x00000000 The issue I'm having is that address misses the final '0' because it puts a /0 character there, and I'm not sure how to get around that? So it outputs:

Closing C++ File Stream is not Opened

梦想的初衷 提交于 2019-12-12 02:47:25
问题 Suppose you declare an instance of std::ifstream or std::ofstream but is_open() returns 0 Example: std::ifstream file("myfile.txt"); if (!file.is_open()) { printf("Could not open file\n"); return; } Since the file never opened, do I still need to call file.close() after the printf statement? 回答1: No, you can only close an open file (similar to how you cannot close an already closed door - there is nothing to be done). Extra note: Please do not combine the C I/O library (X printf family of

C++ ifstream is_open() fails [duplicate]

断了今生、忘了曾经 提交于 2019-12-12 01:58:17
问题 This question already has answers here : Where to place file in order to read? (4 answers) Closed 3 years ago . Why does my is_open() always fail and goes into the else statement which displays the error message? Another method of mine is similar to this yet it worked. #include <iostream> #include <fstream> using namespace std; int main() { string userName; cout << "Please login to your account here!" << endl; cout << "Username: "; cin >> userName; ifstream openSalt; openSalt.open("salt.txt")

How to read non ascii characters from a text file

时光毁灭记忆、已成空白 提交于 2019-12-12 01:27:48
问题 I found the length using seekg and tellg, then read them into an unsigned char*. The debugger is showing incorrect text though. ifstream is (infile, ifstream::binary); //find length is.seekg (0, is.end); int length = is.tellg(); is.seekg (0, is.beg); char * buffer = new char [length]; is.read (buffer,length); //delete[] buffer; removed //size_t cipherTextLength = length; removed //unsigned char* cipherText = new unsigned char[cipherTextLength]; removed //is.read (reinterpret_cast<char*>

C++ Base Class load function

非 Y 不嫁゛ 提交于 2019-12-12 00:34:30
问题 I have a base class of cars and a load function, if I have a derived class, can I use the load function from the base class and add extra data members such as engine size for sports cars to be loaded from a file. Below is my function definition. void Car::Load(ifstream& carFile) { carFile >> CarName >> Age >> Colour >> Price; } 回答1: yes you can : class Car{ public: virtual void Load(ifstream& carFile); } it is important that the Load method is virtual. class SportsCar : public Car{ public:

Inputting a file into a structure

吃可爱长大的小学妹 提交于 2019-12-11 19:47:50
问题 I am trying to read the lines from a file called 'weapon.txt' and input them into a structure something a long the lines of this struct weapon { char name[20]; //Edited int strength; } The file to be read looks like this: Excalibur 150 Throwing Stars 15 Rapier 200 Bow and Arrow 100 Axe 200 Crossbow 100 Scimitar 250 Rusted Sword 10 Soul Slayer 500 The code I have right now is #include<fstream> #include<iostream> #include<cstring> using namespace std; struct WeaponInfo { char name[16]; int

Passing ifstream as an argument to Class Constructor

北城余情 提交于 2019-12-11 19:18:47
问题 I am trying to pass: ifstream infile; in my main (), to the constructor of a class called "FIFO": FIFO (infile); In the header file of FIFO (FIFO.h), I have: FIFO (std::ifstream); std::ifstream infile; And in FIFO.cc, I have: FIFO::FIFO (std::ifstream & INFILE) { infile = INFILE; } I kept getting like (There are more of them, I just paste one of them): In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/localefwd.h:43, from /usr/lib/gcc/x86_64

infile.open refuses to read the variable in the file

岁酱吖の 提交于 2019-12-11 19:18:39
问题 So, I have this loop: int counter1 = 0; ifstream incard; string card; string cardname; stringstream out; while (counter1 < 4) { counter1 = counter1 + 1; out << counter1; out << ".card"; card = out.str(); cout << card; system("PAUSE"); incard.open(card.c_str()); incard >> cardname; cout << cardname << endl; incard.close(); out.str(""); } 1.card contains text "Angel" 2.card contains text "Devil" 3.card contains text "Firaxis" 4.card contains text "Robert" This is the output I get: 1.cardPress

Save & read double vector from file C++

扶醉桌前 提交于 2019-12-11 17:28:54
问题 I'm trying to save a std::vector<double> to a file and read to rebuild the std::vector<double> . This code from rex (original answer) works for std::vector<char> but not for doubles. Once I tried to modify this to work with doubles, numbers lose decimal points. Here is my attempt (modified code) #include <iostream> #include <algorithm> #include <fstream> #include <iterator> #include <vector> std::string filename("savefile"); std::vector<double> myVector{1321.32132,16.32131,32.321,64,3213.454}

reading from text file to struct vector, but text file lines are of different length

倖福魔咒の 提交于 2019-12-11 15:27:25
问题 I am trying to read from a text file of students and assign each line to a struct data member. The text file structure is as follows: Name,Student code, Ability,Consistency,Program name:Subject list Two students in the file: Average Ant,204932,50,5,Short course:1 Brilliant Bison,234543,80,3,Bachelor of Bounciness:2,5,3 I can read all of the information to a struct no problem except for the last part(subject list),that are of varying length between students. How can I write code so that if a