c++ - How to create a function that reads in multiple files? -
hi im working on application want create functions reads in files ex.
readfiles() { f1, f2=null, f3=null; }
but i'm having hard time getting multiple files load. make function reads in files , sets them specified variable use in other functions within program. or ideas great. again! ps. not homework assignment.
i start writing function can read 1 file. call function function knows files need read.
for example, assuming files aren't enormouse (gigabytes large), function reads file single string:
std::string readfile(std::string name) { ifstream f(name.c_str()); std::stream tmp; std::stream res; while(getline(f, tmp) { res += tmp; } return res; } void somefunc() { std::string file1 = readfile("file1.txt"); std::string file2 = readfile("file2.txt"); }
of course, if file list of integers, may want read data vector of integer instead. if content more complex, reading vector or struct or class work. basic principle applies [assuming several files have same general type of content @ all, of course].
Comments
Post a Comment