ifstream

Why is my std::string obtained via stream being overwritten?

孤街浪徒 提交于 2019-12-11 13:17:19
问题 Assume I have a function like so: std::string get_shader(std::string path) { std::string fullpath = "./resources/shaders/" + path; std::ifstream vertexShaderFile(fullpath); std::ostringstream vertexBuffer; vertexBuffer << vertexShaderFile.rdbuf(); return vertexBuffer.str(); } And then some code like this: GLuint vertex_shader; GLuint fragment_shader; GLuint program; const GLchar * vertex_shader_source = get_shader("triangle_vertex.vs").c_str(); // At this point vertex_shader_source is correct

Reading lines with 2 numbers each in C++

久未见 提交于 2019-12-11 11:16:25
问题 I'm pretty rusty on my C++. I'm wondering what the best way is to read input in the following format: 400 200 138 493 ... I'm currently using while(cin.peek()!=-1) to check for EOF, and then within that, I'm using while(cin.peek()!='\n') to check for newlines. This is fine for reading in full lines of text, but how can I limit it to 2 numbers and/or grab just those 2 numbers? 回答1: int num1,num2; while(cin>>num1>>num2) { //... } or string line; int num1,num2; stringstream ss; while(getline(cin

How to open a file using ifstream and keep reading it until the end

泄露秘密 提交于 2019-12-11 10:39:47
问题 I simply would like to open a file named "test.txt" that contains: cat dog moose rabbit I then want to read from the file and convert its contents into strings for things that I will do later in the program. So far i think what I am doing is going in the right direction. However, I am getting an error and I am unsure what to do. Here is the Error>>> $ make -f makefile.txt g++ -g -D HASH_TABLE_SIZE=10 -c hash.cpp hash.cpp: In member function `void Hash::processFile(std::string)': hash.cpp:12:

Ifstream crashes program when opening file

不打扰是莪最后的温柔 提交于 2019-12-11 07:56:19
问题 I've narrowed my code down, and I found the source of the problem, it's when I open a file. The file does exists, and I don't get any warning or errors when compiling. int main(int argc, const char* args[]) { cout << "Wellcome" << endl; cout << args[1]; ifstream exists(args[1]); if(!exists) { printf("FILE NOT FOUND"); return 1; } exists.close(); ifstream* in; in->open(args[1],ios::binary|ios::in); //do stuff in->close(); return 0; } 回答1: You have created a pointer to an ifstream object, but

C++ - ifstream not liking relative paths

我们两清 提交于 2019-12-11 07:24:09
问题 I have been having trouble with opening files with ifstream if I do: ifstream myfile; myfile.open("C:/Users/build/windows/Debug/map1.xml"); it works fine, but if I do a relative path (the executable is in Debug/) ifstream myfile; myfile.open("map1.xml"); It will not find the file. Any help? Am I missing something silly? 回答1: Most likely, your IDE changes your program's working directory when launching it. Try putting map1.xml into c:\users\build\windows 来源: https://stackoverflow.com/questions

Can't make sense of the varying results of experiments with buffer sizes in C and C++. Also ifstream slower than FILE?

孤者浪人 提交于 2019-12-11 07:08:07
问题 It all started with this question -> How to read blocks of data from a file and then read from that block into a vector? With the aim of minimizing disk I/O operations, I performed a few experiments to see if size of buffer has any kind of effect on the time taken by program. I used the following two codes, one more c-oriented and another more c++ (though both compiled with gcc):- The c oriented code:- int buffer_size=1024; FILE *file; file = fopen(argv[1], "r"); FILE *out_file; out_file =

How do I use an iterator on an ifstream twice in C++?

你离开我真会死。 提交于 2019-12-11 05:26:10
问题 I'm new to C++ and I'm confused about using iterators with ifstream. In this following code I have an ifstream variable called dataFile. In the code I first iterate through the file once to count how many characters it has (is there a more efficient way to do this?). Then I create a matrix of that size, and iterate through again to fill the matrix. The problem is that the iterator refuses to iterate the second time around, and will not do anything. I tried resetting the ifstream from the

Reading in Russian characters (Unicode) using a basic_ifstream<wchar_t>

最后都变了- 提交于 2019-12-11 02:24:45
问题 Is this even possible? I've been trying to read a simple file that contains Russian, and it's clearly not working. I've called file.imbue(loc) (and at this point, loc is correct, Russian_Russia.1251). And buf is of type basic_string<wchar_t> The reason I'm using basic_ifstream<wchar_t> is because this is a template (so technically, basic_ifstream<T>, but in this case, T=wchar_t). This all works perfectly with english characters... while (file >> ch) { if(isalnum(ch, loc)) { buf += ch; } else

How can I get the standard file streams to return a useful error message?

给你一囗甜甜゛ 提交于 2019-12-10 18:49:44
问题 #include <fstream> #include <iostream> #include <exception> int main(int argc, char **argv) { try { std::ifstream sFile(argv[1]); sFile.exceptions(std::ios::badbit | std::ios::failbit); } catch (const std::exception &_r) { std::cerr << "exception: " << _r.what() << std::endl; } } In case of the file passed in does not exist, this code prints out with g++ 4.5.2 (yes I know that this is a very old version, but I don't have enough clout to change this): "exception: basic_ios::clear" Using Visual

Return value of ifstream.peek() when it reaches the end of the file

余生颓废 提交于 2019-12-10 18:41:40
问题 I was looking at this article on Cplusplus.com, http://www.cplusplus.com/reference/iostream/istream/peek/ I'm still not sure what peek() returns if it reaches the end of the file. In my code, a part of the program is supposed to run as long as this statement is true (sourcefile.peek() != EOF) where sourcefile is my ifstream. However, it never stops looping, even though it has reached the end of the file. Does EOF not mean "End of File"? Or was I using it wrong? 回答1: Consulting the Standard,