ifstream

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

一世执手 提交于 2019-12-05 23:53:16
问题 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:

C++ find size of ofstream

浪子不回头ぞ 提交于 2019-12-05 20:02:29
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(pFile, -linelength, SEEK_END); You want tellp . This is available for output streams (e.g., ostream,

Using ifstream as fscanf

天大地大妈咪最大 提交于 2019-12-05 19:58:22
问题 Assume that I have an input as follows: N (X_1,Y_1) (X_2,Y_2) .... (X_N, Y_N) where N, X_i and Y_i are integers. An example: 2 (55,1) (521,7) To read this, I can do something like this(assume all variables are defined, etc.): fscanf(fin,"%d ",&N); for (int i = 0; i < N; i++) fscanf(fin,"(%d,%d) ", &X[i], &Y[i]); The question is, how can I do this easily using ifstream. I can get string's, and then I can get rid of nondigits and using stringstream I can get two numbers but this seems a bit

Returning ifstream in a function

风格不统一 提交于 2019-12-05 19:25:58
问题 Here's probably a very noobish question for you: How (if at all possible) can I return an ifstream from a function? Basically, I need to obtain the filename of a database from the user, and if the database with that filename does not exist, then I need to create that file for the user. I know how to do that, but only by asking the user to restart the program after creating the file. I wanted to avoid that inconvenience for the user if possible, but the function below does not compile in gcc:

fstream ifstream I don't understand how to load a data file into my program

倖福魔咒の 提交于 2019-12-05 17:21:55
My professor is very smart but expects complete noobs like me to just know how to program c++ . I don't understand how the fstream function works. I will have a data file with three columns of data. I will have to determine with a logarithm whether each line of data represents a circle, rectangle or triangle - that part is easy. The part I don't understand is how the fstream function works. I think I: #include < fstream > then I should declare my file object? ifstream Holes; then I open it: ifstream.open Holes; // ? I don't know what the proper syntax is and I can't find straightforward

What does ifstream::rdbuf() actually do?

我的梦境 提交于 2019-12-05 11:12:34
问题 I have the following code and it works pretty good (other than the fact that it's pretty slow, but I don't care much about that). It doesn't seem intuitive that this would write the entire contents of the infile to the outfile. // Returns 1 if failed and 0 if successful int WriteFileContentsToNewFile(string inFilename, string outFilename) { ifstream infile(inFilename.c_str(), ios::binary); ofstream outfile(outFilename.c_str(), ios::binary); if( infile.is_open() && outfile.is_open() && infile

clang 3.3/Xcode & libc++: std::getline does not read data after calling ifstream::clear()

半城伤御伤魂 提交于 2019-12-05 10:51:19
The following program demonstrates an inconsistency in std::getline behavior between libc++ and libstdc++ (using clang3.3). The program opens the file testfile, reads it until eof, then clears the error bits using ifstream::clear and tries to read from the same filehandle again to see if new data was appended to the file. #include <fstream> #include <iostream> #include <unistd.h> using namespace std; int main() { ifstream* file = new ifstream("testfile"); if ( ! file->is_open() ) { cout << "testfile does not exist" << endl; return -1; } while ( 1 ) { file->clear(); // remove end of file evil

c++ ifstream function and field separators

这一生的挚爱 提交于 2019-12-05 10:47:36
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 confusing. The question then being, is there a way to set a field separator with the ifstream function or is

ifstream:: What is the maximum file size that a ifstream can read

女生的网名这么多〃 提交于 2019-12-05 10:16:29
I tried to read a 3GB data file using ifstream and it gives me wrong file size, whereas when I read a 600MB file, it gives me the correct result. In addition to wrong file size, I am also unable to read the entire file using ifstream. Here is the code that I used std::wstring name; name.assign(fileName.begin(), fileName.end()); __stat64 buf; if (_wstat64(name.c_str(), &buf) != 0) std::cout << -1; // error, could use errno to find out more std::cout << " Windows file size : " << buf.st_size << std::endl;; std::ifstream fs(fileName.c_str(), std::ifstream::in | std::ifstream::binary); fs.seekg(0,

Why seekg does not work with getline?

自闭症网瘾萝莉.ら 提交于 2019-12-05 07:18:34
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? Joseph Mansfield Make sure you clear the error flags before the call to myFile.seekg() : myFile.clear(); After the EOF flag has ben set, you will not be able to