ifstream

Looking to add comma delimited values in a text file to members of a struct

断了今生、忘了曾经 提交于 2019-12-13 20:40:52
问题 I have a text file here with two values, a name and a score. I have a student struct that has 4 members which are seen below. I am looking to add the values in the text file to the corresponding members in the struct separating by comma. First five rows of the students.txt file; Nubia,Dufrene,70 Louisa,Trippe,49 Aline,Deniz,34 Shery,Munk,63 Angila,Ping,89 My current code; struct studentType { string studentFName; string studentLName; int testScore; char grade; }; int main() { vector

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

自古美人都是妖i 提交于 2019-12-13 16:34:54
问题 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 sake of keeping this post small I've pastied it here. In my code after running through case a or d all std::ifstream open calls fail. (Prior to posting this

no instance of overloaded function

感情迁移 提交于 2019-12-13 08:59:23
问题 trying to do a project for class, but keep getting the error: no instance of overloaded function matches argument list relating to the implementation of the rows vector. the area that is specifically highlighted is the . operator before push_back and insert. void holdLines(ifstream in, vector<string> rows) { string line; string prevLine; vector<string> rows; int lineNumber = 0; int vectorNumber = 0; while(true) { getline(in, line); if(in.fail()) { break; } lineNumber++; vectorNumber =

How to search record using an id

两盒软妹~` 提交于 2019-12-13 08:35:46
问题 I am creating the system that outputs all the registered student in one of my textfile e.g 123 Michael 412 Voker 512 Karl 143 Riki I need to use their ID to search for the student Means i need to read this register student file. The system will only ask for the ID. e.g Type ID num: 123 OUTPUT: Hello Michael your ID number is 123. 回答1: Probably your are looking for a struct or class. Make a Student class I would say. class Student{ public: int id; char *name; }; Student s; Give a go through

C++: Stacks and Printing something out when you reach the end of a file or when reading the last character in a file

帅比萌擦擦* 提交于 2019-12-13 07:14:02
问题 I need to read from a file, character by character. My specific problem: If it finds a /* , it puts them into the stack, and then goes into "comment mode" where it ignores everything until it finds a */ . If it never finds the matching pair */* in the entire file, it should print out "unbalanced symbol / " but it never prints that* 回答1: The program now needs to handle two states/modes instead of one: It starts in "matching mode", putting ( { [ on the stack and popping ) } ] if they match. As

cant get ifstream to work in XCode

て烟熏妆下的殇ゞ 提交于 2019-12-12 14:33:40
问题 No matter what I try, I cant get the following code to work correctly. ifstream inFile; inFile.open("sampleplanet"); cout << (inFile.good()); //prints a 1 int levelLW = 0; int numLevels = 0; inFile >> levelLW >> numLevels; cout << (inFile.good()); //prints a 0 at the first cout << (inFile.good());, it prints a 1 and at the second a 0. Which tells me that the file is opening correctly, but inFile is failing as soon as read in from it. The file has more then enough lines/characters, so there is

C++ find size of ofstream

大憨熊 提交于 2019-12-12 09:54:05
问题 I have code that currently does something like the following: ofstream fout; fout.open("file.txt"); fout<<"blah blah "<<100<<","<<3.14; //get ofstream length here fout<<"write more stuff"<<endl; Is there a convenient way to find out the length of that line that is written at the stage I specified above? (in real life, the int 100 and float 3.14 are not constant and can change). Is there a good way to do what I want? EDIT: by length, I mean something that can be used using fseek, e.g. fseek

ifstream and ofstream or fstream using in and out

痴心易碎 提交于 2019-12-12 08:29:56
问题 When dealing with files, which of the two examples below is preferred? Does one provide better performance than the other? Is there any difference at all? ifstream input("input_file.txt"); ofstream output("output_file.txt"); vs fstream input("input_file.txt",istream::in); fstream output("output_file.txt",ostream::out); 回答1: Performance-wise, there are probably only negligible differences in this case. At best you're saving a little memory. What matters is that the first case helps with the

function with an istream& parameter C++

淺唱寂寞╮ 提交于 2019-12-12 07:24:21
问题 I would like my program to read a file using the function "readFile" below. I am trying to find out how to call a function with an istream& parameter. The goal of the function is to read the file by receiving the file's name as parameter. #include <iostream> #include <sstream> #include <fstream> #include <string> using namespace std; bool readFile(std::istream& fileName); //error 1 this line int main(void) { string fileName; cout << "Enter the file name: "; cin >> fileName; readFile(fileName)

Reading formated date from a file C++

你说的曾经没有我的故事 提交于 2019-12-12 05:38:00
问题 So I have this file with multiple dates like this: 2.10.2015 13.12.2016 ... I'm wondering how to read from this file and store day, month and year into 3 separate integers. Thanks. 回答1: You could try something like this: // construct stream object and open file std::ifstream ifs(file_name.c_str()); // check if opened successfully if (!ifs) std::cerr <<"Can't open input file!\n"; int year, month, day; char dot; // extract date ifs >> day >> dot >> month >> dot >> year; // check input format if