ifstream

C++ ifstream and “umlauts”

久未见 提交于 2019-12-07 12:01:32
问题 I am having an issue with "umlauts" (letters ä, ü, ö, ...) and ifstream in C++. I use curl to download an html page and ifstream to read in the downloaded file line by line and parse some data out of it. This goes well until I have a line like one of the following: te="Olimpija Laibach - Tromsö"; te="Burghausen - Münster"; My code parses these lines and outputs it as the following: Olimpija Laibach vs. Troms? Burghausen vs. M?nster Things like outputting umlauts directly from the code work:

c++ ifstream function and field separators

对着背影说爱祢 提交于 2019-12-07 06:45:00
问题 For this program i have only used field separators from data files in shell script. But I am trying to use the standard library function ifstream() to read in from a data file. The only problem is I am getting the data like so A:KT5:14:executive desk: This is for a hash table, and I need to separate the values in the line for the data structure as well as the transaction type. I’ve been looking around the web and have not found much on field separators and what I have found was quite

Why seekg does not work with getline?

拜拜、爱过 提交于 2019-12-07 01:56:55
问题 Seekg does not seem to work, when I reach EOF in myFile. ifstream myFile("/path/file"); for(int i; i < 10; i++){ myFile.seekg(0);//reset position in myFile while(getline(myFile, line)){ doSomething } } So, now I am opening input stream every loop: for(int i; i < 10; i++){ ifstream myFile("/path/file");//reset position in myFile while(getline(myFile, line)){ doSomething } } But I would rather seek to position 0. How can I achieve that? 回答1: Make sure you clear the error flags before the call

Getting the nth line of a text file in C++

南楼画角 提交于 2019-12-07 01:26:08
问题 I need to read the nth line of a text file (e.g. textfile.findline(0) would find the first line of the text file loaded with ifstream textfile ). Is this possible? I don't need to put the contents of the file in an array/vector, I need to just assign a specific line of the text file to a varible (specifically a int). P.S. I am looking for the simplest solution that would not require me to use any big external library (e.g. Boost) Thanks in advance. 回答1: How about this? std::string ReadNthLine

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".

C++ How Do I Read All .txt Files in a Directory?

こ雲淡風輕ζ 提交于 2019-12-06 15:58:05
How do I read all .txt files in a certain directory? Let's say in my C:\ I have a foo.txt and a foo2.txt . Is there a way to read both of these two without having to do something like this? string text; string text2; ifstream myFile ("foo.txt"); ifstream myFile2 ("foo2.txt"); while(myFile << text){ }; while(myFile2 << text2){ }; In other words, is it possible to put a wildcard *.txt to instruct to read all .txt files? ifstream myFile ("*.txt"); You'd typically do this by passing the name(s) of the files to read on the command line. Using a Linux-like shell, passing something like foo *.txt

Reading quoted string in c++

泪湿孤枕 提交于 2019-12-06 15:02:23
问题 I am trying to read quoted string from a file and store it in string. I am reading string from the file and input file is like this: "Rigatoni" starch 2.99 "Mac & Cheese" starch 0.50 "Potato Salad" starch 3.59 "Fudge Brownie" sweet 4.99 "Sugar Cookie" sweet 1.50 I have tried to do couple things: 1. input.open(filename); if (input.fail()) { std::cout << "File is not found!"; exit(1); } else { std::string foodName = ""; std::string foodType = ""; double cost; input >> foodName >> foodType >>

How to get the last integer of sequence of integers separated by commas in C++?

混江龙づ霸主 提交于 2019-12-06 13:30:44
问题 What I'm trying to do is to get each integer from a file of integers separated by commas and add them. For example, if the file contains 2,3,4,1 my program has to display that the sum is 10. For that, I wrote the following code. int number_sum(const char* filename) { std::ifstream file(filename); if (file.is_open()) { int sum = 0; //sum char c; //Store each comma int number = 0; //Store each number while ( (file >> number >> c) && (c == ',') ) { std::cout << "Adding up: " << number << " + " <

String is not printing without new line character in C++

爷,独闯天下 提交于 2019-12-06 11:13:52
I'm opening a file, and getting lines from it. The first line should say how many variables there are, and what their names are. The second line should be a logic equation using these variables. The assignment is to have it print out a truth table for the variables and equation. The first line the program is taking in is not printing without me inserting a new line character. I tried converting to a string and using both printf and cout. Main file that inputs everything: #include "truthTable2.h" int main(int argc, const char* argv[]){ ifstream inFile; if(argc != 2){ cout << "Enter an input

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

北城以北 提交于 2019-12-06 02:23:56
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 as expected. The code opens a file and reads the first 512 bytes of the file one time with read() and