c++ - Importing values from a *.dat file to an array -
i have file called numbers.dat consists of 20000 random numbers. need create array in *.cpp file elements numbers numbers.dat file. sure basic, however, nothing have found online has answered question. thank help.
you need provide lot more information if expect useful answer, started, here's simple example of can do.
i'm assuming file text file (doesn't matter extension is; text file 1 can open in notepad , read contents.)
#include <fstream> #include <vector> int main () { std::vector<int> data; std::ifstream fin ("numbers.dat"); int temp = 0; while (fin >> temp) data.push_back (temp); // here have data in "data" vector. return 0; }
the above code reads many integers in file named "numbers.dat". these integers must separated whitespace (space characters, tabs, new lines, etc.) not things commas , semicolons.
if need read numbers binary file, write comment , i'll extend answer.
Comments
Post a Comment