ifstream

Open method opens files with full path only C++

心已入冬 提交于 2020-01-15 09:04:20
问题 File opens if I write the full path (full-path/roots.txt). File fails to open if I write the filename only (roots.txt) And yet, roots.txt is in the same folder as the main.cpp. Is there any settings I should check on XCode? Here's the code: string line; ifstream infile; infile.clear(); // infile.open("roots.txt"); infile.open("/Users/programming/C++/roots/roots.txt"); if (infile.fail()) cout << "could not open the file: " << strerror(errno); getline(infile, line); cout << line; 回答1: If it

c++ file bad bit

被刻印的时光 ゝ 提交于 2020-01-15 07:59:27
问题 when I run this code, the open and seekg and tellg operation all success. but when I read it, it fails, the eof,bad,fail bit are 0 1 1. What can cause a file bad? thanks int readriblock(int blockid, char* buffer) { ifstream rifile("./ri/reverseindex.bin", ios::in|ios::binary); rifile.seekg(blockid * RI_BLOCK_SIZE, ios::beg); if(!rifile.good()){ cout<<"block not exsit"<<endl; return -1;} cout<<rifile.tellg()<<endl; rifile.read(buffer, RI_BLOCK_SIZE); **cout<<rifile.eof()<<rifile.bad()<<rifile

ifstream.read() vs. ifstream.readsome() in MSVC++7.1

*爱你&永不变心* 提交于 2020-01-13 16:26:26
问题 I just took some older code of a file reader that had been developed under Linux and tried to use that very same code in my Windows project compiled with MSVC++7.1. The code compiled without any problems, but the file seemed to be empty according to the file reader on Windows. I tracked the problem down to ifstream.readsome() that didn't read anything from the file, without setting any error flags on the stream. The code provided below compiles on either Linux and Windows, but Linux it works

ifstream.read() vs. ifstream.readsome() in MSVC++7.1

邮差的信 提交于 2020-01-13 16:25:14
问题 I just took some older code of a file reader that had been developed under Linux and tried to use that very same code in my Windows project compiled with MSVC++7.1. The code compiled without any problems, but the file seemed to be empty according to the file reader on Windows. I tracked the problem down to ifstream.readsome() that didn't read anything from the file, without setting any error flags on the stream. The code provided below compiles on either Linux and Windows, but Linux it works

C++, reading a file using ifstream

喜夏-厌秋 提交于 2020-01-13 09:54:09
问题 class Person { private: string firstName; string lastName; public: Person() {} Person(ifstream &fin) { fin >> firstName >> lastName; } void print() { cout << firstName << " " << lastName << endl; } }; int main() { vector<Person> v; ifstream fin("people.txt"); while (true) { Person p(fin); if (fin == NULL) { break; } v.push_back(p); } for (size_t i = 0; i < v.size(); i++) { v[i].print(); } fin.close(); return 0; } Please can you explain me, how following code snippet works? if (fin == NULL) {

C++, reading a file using ifstream

[亡魂溺海] 提交于 2020-01-13 09:54:08
问题 class Person { private: string firstName; string lastName; public: Person() {} Person(ifstream &fin) { fin >> firstName >> lastName; } void print() { cout << firstName << " " << lastName << endl; } }; int main() { vector<Person> v; ifstream fin("people.txt"); while (true) { Person p(fin); if (fin == NULL) { break; } v.push_back(p); } for (size_t i = 0; i < v.size(); i++) { v[i].print(); } fin.close(); return 0; } Please can you explain me, how following code snippet works? if (fin == NULL) {

Why does my program produce different results on Windows and Linux, about file reading with ifstream?

天大地大妈咪最大 提交于 2020-01-13 03:07:28
问题 I have a program shown as follows. For it I have several questions: 1). Why does it produce different results on different platforms? I'll paste the screen-shots later. 2). I'm using a fail() method to check if the "file.read()" failed. Is this correct? I use fail() method because this web page says this: The function returns true if either the failbit or the badbit is set. At least one of these flags is set when some error other than reaching the End-Of-File occurs during an input operation.

Read text file into char Array. C++ ifstream

两盒软妹~` 提交于 2020-01-12 19:04:13
问题 Im trying to read the whole file.txt into a char array. But having some issues, suggestions please =] ifstream infile; infile.open("file.txt"); char getdata[10000] while (!infile.eof()){ infile.getline(getdata,sizeof(infile)); // if i cout here it looks fine //cout << getdata << endl; } //but this outputs the last half of the file + trash for (int i=0; i<10000; i++){ cout << getdata[i] } 回答1: Every time you read a new line you overwrite the old one. Keep an index variable i and use infile

Reading a Matrix txt file and storing as an array

痞子三分冷 提交于 2020-01-09 05:18:45
问题 I'm currently writing a Simulated Annealing code to solve a traveling salesman problem and have run into difficulties with storing and using my read data from a txt file. Each row & column in the file represents each city, with the distance between two different cities stored as a 15 x 15 matrix: 0.0 5.0 5.0 6.0 7.0 2.0 5.0 2.0 1.0 5.0 5.0 1.0 2.0 7.1 5.0 5.0 0.0 5.0 5.0 5.0 2.0 5.0 1.0 5.0 6.0 6.0 6.0 6.0 1.0 7.1 5.0 5.0 0.0 6.0 1.0 6.0 5.0 5.0 1.0 6.0 5.0 7.0 1.0 5.0 6.0 6.0 5.0 6.0 0.0 5.0

Writing multiple array pointers to file with ofstream?

╄→гoц情女王★ 提交于 2020-01-06 04:25:52
问题 I'm having some seriously strange trouble writing multiple arrays of data to a file. Basically, I'm wanting to store all the array sizes at the top of the file, and then the array data following. This way I can just read the sizes and use that to construct arrays to hold the data on import, and I'll know exactly where each array begins and ends. Here's the problem: I write the data, but it's different on import. Please take a look at my little test code. At the bottom there are comments about