ifstream

Read/Write struct containing string property to binary file

核能气质少年 提交于 2019-12-20 06:46:14
问题 I am running in to an issue while reading/writing a struct having complex data specifically string. my struct looks like below. struct MyRecord { char name[80]; string location; // <<== note this double balance; unsigned long account_num; }; I use different functions for reading and writing. This is the code I am using to write the data to file struct MyRecord acc; strcpy(acc.name, "R"); acc.location = "newlocation ok"; // <<== note this acc.balance = 1.3; acc.account_num = 34; ofstream

Get each value from a string via std::ifstream

假如想象 提交于 2019-12-20 06:13:30
问题 I am try to use an ifstream with the while loop to get each value. However, when I try it, nothing happens. Why? std::string line; std::getline(cin, line); std::ifstream stream(line); while(stream){ std::cout << stream.get(); } 回答1: You must use an istringstream , not an ifstream . 来源: https://stackoverflow.com/questions/20639889/get-each-value-from-a-string-via-stdifstream

Errors when passing ifstream file through a function

自闭症网瘾萝莉.ら 提交于 2019-12-20 06:09:26
问题 Here is my code: #include <string> #include <iomanip> #include <fstream> #include <iostream> #include <cstring> #include <cctype> using namespace std; // include .h file that holds function ot write header #include "WriteE3RptHdr.h" // declare global constant const int NUM_QTS = 15; // declare struct to hold info on student and answers struct StudRpt { char answers[NUM_QTS]; string firstName; string lastName; char answerKey[NUM_QTS]; string testKey; string testData; }; StudRpt Data; //

Cannot use ifstream in Xcode

徘徊边缘 提交于 2019-12-20 04:27:15
问题 I am new to C++ and have researched this everywhere and cannot seem to figure out how to compile this and have not idea why. It works in visual C++ but not Xcode. The error seems to be on the input stream. Any suggestions? Error reads - "Implicit instantiation of undefined template 'std::_basic_ifstream >' #include <iostream> #include <iostream> #include <string> using namespace std; int main() { cout << "The file is providing the data."; ifstream myFile("/Users/me/Desktop/somewords.txt"); //

Can't declare ifstream class member in header file

和自甴很熟 提交于 2019-12-19 10:25:40
问题 I am trying to declare an ifstream object in a header file as is shown but I get an error saying that it cannot be accessed. I have tried various things such as making it into a pointer instead, initialising in the .c file etc. but my code can't seem to get part the declaration of it. ReadFile.h: #ifndef READFILE_H #define READFILE_H #include "cv.h" #include "highgui.h" #include <iostream> #include <fstream> class ReadFile{ private: std::ifstream stream; public: std::string read(); ReadFile()

ifstream.eof() - end of file is reached before the real end

不打扰是莪最后的温柔 提交于 2019-12-18 13:16:19
问题 I have a roughly 11.1G binary file where stores a series of the depth frames from the Kinect. There are 19437 frames in this file. To read one frame per time, I use ifstream in fstream but it reaches eof before the real end of the file. (I only got the first 20 frames, and the function stops because of the eof flag) However, all frames can be read by using fread in stdio instead. Can anyone explain this situation? Thank you for precious time on my question. Here are my two functions: //

seekg() function fails

本小妞迷上赌 提交于 2019-12-18 09:06:35
问题 I am trying to write some simple code which will read a text file but reads the first line twice. I thought this would be as simple as something like this std::ifstream file; file.open("filename", std::ios_base::in); std::string line; std::getline(file, line); // process line file.seekg(0, ios::beg); while (std::getline(file, line)) { // process line } However the seekg must fail as the first line is not processed twice. Any idea why? PLEASE NOTE: This is not the problem I am faced with but a

Is std::ifstream thread-safe & lock-free?

独自空忆成欢 提交于 2019-12-18 05:11:55
问题 I intend to perform opening for reading a single file from many threads using std::ifstream. My concern is if std::ifstream is thread-safe & lock-free? More details: I use g++ 4.4 on Ubuntu & Windows XP, 4.0 on Leopard. Each thread creates its own instance of std::ifstream Thanks in advance! 回答1: That is implementation defined. Standard C++ says absolutely nothing about threading, and therefore any assumptions about threads inherently invoke unspecified or implementation defined behavior. We

Rewind an ifstream object after hitting the end of file

允我心安 提交于 2019-12-17 23:22:09
问题 Having a text file with a few characters (lets say 10), you can try to read 1000 characters from it. char *buf = new char[1000]; ifstream in("in.txt"); in.read(buf, 1000); This, of course will set the eofbit flag (and the failbit too), however, you will be able to obtain the desired characters. Now, suppose you want to read the file again (from the beginning): in.seekg(0); // Sets input position indicator. in.read(buf, 100); // Try to read again. This don't work, if you call int count = in

Read line of numbers using C++

本小妞迷上赌 提交于 2019-12-17 20:38:36
问题 What's the standard way of reading a "line of numbers" and store those numbers inside a vector. file.in 12 12 9 8 17 101 2 Should I read the file line by line, split the line with multiple numbers, and store the tokens in the array ? What should I use for that ? 回答1: #include <vector> #include <fstream> #include <iterator> #include <algorithm> std::vector<int> data; std::ifstream file("numbers.txt"); std::copy(std::istream_iterator<int>(file), std::istream_iterator<int>(), std::back_inserter