ifstream

Try catch exception handling C++

混江龙づ霸主 提交于 2019-12-05 07:14:33
问题 I have just started with exception handling in C++ using try and catch blocks. I have a text file with some data and I am reading this file using ifstream and getline as shown below, ifstream file; file.open("C:\\Test.txt", ios::in); string line; string firstLine; if (getline(file, line, ' ')) { firstLine = line; getline(file, line); } I would like to know how to implement exception handling in case file.open fails to open the specified file because it does not exist in the given path, for

Is using istream::seekg too much expensive?

余生颓废 提交于 2019-12-05 03:35:45
In c++, how expensive is it to use the istream::seekg operation? EDIT: How much can I get away with seeking around a file and reading bytes? What about frequency versus magnitude of offset? I have a large file (4GB) that I am parsing, and I want to know if it's necessary to try to consolidate some of my seekg calls. I would assume that the magnitude of differences in file location play a role--like if you seek more than a page in memory away, it will impact performance--but small seeking is of no consequence. Is this correct? This question is heavily dependent on your operating system and disk

Creating fstream object from a FILE* pointer

坚强是说给别人听的谎言 提交于 2019-12-04 22:30:23
The well known way of creating an fstream object is: ifstream fobj("myfile.txt"); ie. using a filename. But I want to create an ifstream object using a file descriptor. Reason: I want to execute a command using _popen() . _popen() returns the output as a FILE* . So there is a FILE* pointer involved but no filename. Kerrek SB You cannot do that just in standard C++, since iostreams and C I/O are entirely separate and unrelated. You could however write your own iostream that's backed by a C FILE stream. I believe that GCC comes with one such stream class as a library extension. Alternatively, if

Reading quoted string in c++

不羁的心 提交于 2019-12-04 19:54:48
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 >> cost; foodName = foodName.substr(1, foodName.size()-2); std::cout << foodName << " " << foodType << " " <

C++ buffered file reading

会有一股神秘感。 提交于 2019-12-04 16:56:52
I wonder if reading a large text file line by line (e.g., std::getline or fgets) can be buffered with predefined read buffer size, or one must use special bytewise functions? I mean reading very large files with I/O operations number optimization (e.g., reading 32 MB from the HDD at a time). Of course I can handcraft buffered reading, but I thought standard file streams had that possibility. Neither line-by-line, nor special byte-wise functions. Instead, the following should do your job: std::ifstream file("input.txt"); std::istream_iterator<char> begin(file), end; std::vector<char> buffer

How to convert a string to a ifstream

狂风中的少年 提交于 2019-12-04 12:46:50
i am trying to open a file with ifstream and i want to use a string as the path (my program makes a string path). it will compile but it stays blank. string path = NameOfTheFile; // it would be something close to "c:\file\textfile.txt" string line; ifstream myfile (path); // will compile but wont do anything. // ifstream myfile ("c:\\file\\textfile.txt"); // This works but i can't change it if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); cout << line << endl; } } I am using windows 7, My compiler is VC++ 2010. string path = compute_file_path(); ifstream myfile (path.c

Difference between casting ifstream to bool and using ifstream::is_open()

徘徊边缘 提交于 2019-12-04 07:30:29
Maybe a dummy question, but I need a clear answer to it. Is there any difference at all in the return of any of those functions int FileExists(const std::string& filename) { ifstream file(filename.c_str()); return !!file; } int FileExists(const std::string& filename) { ifstream file(filename.c_str()); return file.is_open(); } So in other words, my question is: does casting the fstream to bool give exactly the same result as fstream::is_open()? No. is_open checks only whether there is an associated file, while a cast to bool also checks whether the file is ready for I/O operations (e.g. the

Reading input from text file to array in c++

三世轮回 提交于 2019-12-04 06:54:02
问题 Alright, be gentle, since I'm very much a novice to programming. So far I've only studied C++ and I'm running Visual Studio 2010 as my compiler. For this program, I'm trying to read from a text input file and write the information to a set of three arrays. One array will handle a list of names, and the other two are for hours worked and hourly pay rate, respectively. I will use the latter two to calculate a set of earnings and output the whole thing to another text file as a report. My

Read text file into char Array. C++ ifstream

主宰稳场 提交于 2019-12-04 06:33:31
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] } Every time you read a new line you overwrite the old one. Keep an index variable i and use infile.read(getdata+i,1) then increment i. std::ifstream infile; infile.open("Textfile.txt", std::ios::binary); infile

How to read unsigned int variables from file correctly, using ifstream?

风格不统一 提交于 2019-12-04 05:12:55
My code reads unsigned int variables from the text file Input_File_Name . unsigned int Column_Count; //Cols unsigned int Row_Count;//Rows try { ifstream input_stream; input_stream.open(Input_File_Name,ios_base::in); if (input_stream) { //if file is opened input_stream.exceptions(ios::badbit | ios::failbit); input_stream>>Row_Count; input_stream>>Column_Count; } else { throw std::ios::failure("Can't open input file"); //cout << "Error: Can't open input file" << endl; } } catch (const ios::failure& error) { cout << "Oh No!!" << error.what() << endl; } catch (const exception& error) { cout <<