fstream

Defining fstream inside a 'if' conditional

断了今生、忘了曾经 提交于 2019-12-06 20:23:54
问题 In an answer there was the following code: if (std::ifstream input("input_file.txt")) ; Which seems convenient, limiting the scope of the 'input' variable to where it's confirmed to be valid, however neither VS2015 nor g++ seems to compile it. Is it some compiler specific thing or does it require some extra flags? In VS2015 the IDE highlights "std::ifstream" and the "input_file.txt" as well as the last parentheses. "std::ifstream" is flagged with "Error: a function type is not allowed here".

Assign txt file data to struct node in linked list

≯℡__Kan透↙ 提交于 2019-12-06 15:47:42
Ok so I have never worked with fstream before or opened and read and files in a program. My instructor just gave a few lines of code that open, read, and close a text file. I'm supposed to take the data out of the text file and put it into separate nodes in a linked list and then go on to do other things with it which is not important because I know how to do it. My problem is that I don't know how to a assign these values to the struct values. The txt file looks like this: Clark Kent 55000 2500 0.07 Lois Lane 56000 1500 0.06 Tony Stark 34000 2000 0.05 … I have created a structure called

Text File Binary Search

做~自己de王妃 提交于 2019-12-06 15:33:22
I have a test file that looks like this: Ampersand Gregorina 5465874526370945 Anderson Bob 4235838387422002 Anderson Petunia 4235473838457294 Aphid Bumbellina 8392489357392473 Armstrong-Jones Mike 8238742438632892 And code that looks like this: #include <iostream> #include <string> #include <fstream> class CardSearch { protected: std::ifstream cardNumbers; public: CardSearch(std::string fileName) { cardNumbers.open(fileName, std::ios::in); if (!cardNumbers.is_open()) { std::cout << "Unable to open: " << fileName; } return; } std::string Find(std::string lastName, std::string firstName) { //

fstream to const char *

蹲街弑〆低调 提交于 2019-12-06 11:58:33
问题 What I want to do is read a file called "test.txt", and then have the contents of the file be a type const char *. How would one do this? 回答1: #include <string> #include <fstream> int main() { std::string line,text; std::ifstream in("test.txt"); while(std::getline(in, line)) { text += line + "\n"; } const char* data = text.c_str(); } Be careful not to explicitly call delete on data 回答2: It's highly unlikely you really want to do that. The contents of the file (which may be either text, or

c++ Reading string from binary file using fstream

旧城冷巷雨未停 提交于 2019-12-06 09:28:16
I am trying to read a string from a binary file but cant seem to get it to work. I am a pretty new to c++. Can anybody help please? Thanks. string Name = "Shaun"; unsigned short int StringLength = 0; int main() { StringLength = Name.size(); ofstream oFile("File.txt", ios::binary|ios::out); oFile.write((char*)&StringLength, sizeof(unsigned short int)); oFile.write(Name.c_str(), StringLength); oFile.close(); StringLength = 0; Name = "NoName"; ifstream iFile("File.txt", ios::binary|ios::in); if(!iFile.is_open()) cout << "Failed" << endl; else { iFile.read((char *)&StringLength, sizeof(unsigned

Detect newline byte from filestream

£可爱£侵袭症+ 提交于 2019-12-06 05:52:44
I'm trying to collect information from a textfile which contains names of organisations (without spaces) and floating integers. I want to store this information in an array structure. The problem I'm having so far is collecting the information. Here is a sample of the textfile: CBA 12.3 4.5 7.5 2.9 4.1 TLS 3.9 1 8.6 12.8 4.9 I can have up to 128 different numbers for each organisation, and up to 200 organisations in the textfile. This is what my structure looks like so far: struct callCentre { char name[256]; float data[20]; }; My main: int main() { callCentre aCentre[10]; getdata(aCentre);

Why does fstream.open() fail “If the mode has both trunc and app set”?

蓝咒 提交于 2019-12-06 05:51:24
It took me quite a while to figure out that my .open() call wasn't opening a file because I had both the trunc and app mode options set. I only figured this out after catching a little note written on the C++ docs . This seems like a weird gotcha. Why is this the case? Can you not truncate the file and then append only? Or is this considered superfluous specification? The iostream open modes correspond roughly to the fopen mode in the C library and fopen has a w mode that truncates and an a mode that appends, but no combination of the two. The allowable combinations of flags are specified in

If file exist, work with it, if no, create it

橙三吉。 提交于 2019-12-06 04:23:41
问题 fstream datoteka; datoteka.open("Informacije.txt", fstream::in | fstream::out | fstream::app); if(!datoteka.is_open()){ ifstream datoteka("Informacije.txt") datoteka.open("my_file.txt", fstream::in | fstream::out | fstream::app); }/*I'm writing IN the file outside of that if statement. So what it should do is create a file if it was not created before, and if it is created write into that file. Hello there, so what I wanted from my program is that it check if the file already exists, sothe

C++ Trouble Reading a Text File

一曲冷凌霜 提交于 2019-12-05 21:30:16
I'm trying to read a text file but nothing is coming out. I feel like maybe It's not linking correctly in my Visual Studio Resources folder but if I double click it - it opens fine in visual studio and it doesn't run into any problems if I test to see if it opens or if it is good. The program compiles fine right now but there's not output. Nothing prints to my command prompt. Any suggestions? Code #include <iostream> #include <iomanip> #include <fstream> using namespace std; int main() { char str[100]; ifstream test; test.open("test.txt"); while(test.getline(str, 100, '#')) { cout << str <<

ofstream::operator<<(streambuf) is a slow way to copy a file

≡放荡痞女 提交于 2019-12-05 20:56:28
I need a cross-platform, no external library, way of copying a file. In my first pass I came up with (error handling omitted): char buffer[LEN]; ifstream src(srcFile, ios::in | ios::binary); ofstream dest(destFile, ios::out | ios::binary); while (!src.eof()) { src.read(buffer, LEN); dest.write(buffer, src.gcount()); } This worked nicely and I knew exactly what it was doing. Then I found a post on stackoverflow (sorry, can't find a link right now) that says I can replace all of the above code with: dest << src.rdbuf(); Which is nice and compact, but hides a lot about what it's doing. It also