Edit: In response to your comments in main question.
You have two options.
Edit: How to skip values in file
To choose the 1234th value, use the following code:
int skipped = 1233;for (int i = 0; i < skipped; i++){float tmp;myfile >> tmp;}myfile >> value;
It can depend, especially on whether your file will have the same number of items on each row or not. If it will, then you probably want a 2D matrix class of some sort, usually something like this:
class array2D { std::vector<double> data;size_t columns;public:array2D(size_t x, size_t y) : columns(x), data(x*y) {}double &operator(size_t x, size_t y) {return data[y*columns+x];}};
Note that as it's written, this assumes you know the size you'll need up-front. That can be avoided, but the code gets a little larger and more complex.
In any case, to read the numbers and maintain the original structure, you'd typically read a line at a time into a string, then use a stringstream to read numbers from the line. This lets you store the data from each line into a separate row in your array.
If you don't know the size ahead of time or (especially) if different rows might not all contain the same number of numbers:
11 12 1323 34 56 78
You might want to use a std::vector<std::vector<double> >
instead. This does impose some overhead, but if different rows may have different sizes, it's an easy way to do the job.
std::vector<std::vector<double> > numbers;std::string temp;while (std::getline(infile, temp)) {std::istringstream buffer(temp);std::vector<double> line((std::istream_iterator<double>(buffer)),std::istream_iterator<double>());numbers.push_back(line);}
...or, with a modern (C++11) compiler, you can use brackets for line
's initialization:
std::vector<double> line{std::istream_iterator<double>(buffer),std::istream_iterator<double>()};
The input operator for number skips leading whitespace, so you can just read the number in a loop:
while (myfile >> a){// ...}
you could read and write to a seperately like others.But if you want to write into the same one, you could try with this:
#include <iostream>#include <fstream>using namespace std;int main() {double data[size of your data];std::ifstream input("file.txt");for (int i = 0; i < size of your data; i++) {input >> data[i];std::cout<< data[i]<<std::endl;}}
You can use a 2D vector
for storing the numbers that you read from the text file as shown below:
#include <iostream>#include <vector>#include <string>#include <sstream>#include<fstream>int main() {std::string line;double word;std::ifstream inFile("data.txt");//create/use a std::vectorstd::vector<std::vector<double>> vec;if(inFile){while(getline(inFile, line, '\n')) {//create a temporary vector that will contain all the columnsstd::vector<double> tempVec;std::istringstream ss(line);//read word by word(or double by double) while(ss >> word){//std::cout<<"word:"<<word<<std::endl;//add the word to the temporary vector tempVec.push_back(word);} //now all the words from the current line has been added to the temporary vector vec.emplace_back(tempVec);} }else {std::cout<<"file cannot be opened"<<std::endl;}inFile.close();//lets check out the elements of the 2D vector so the we can confirm if it contains all the right elements(rows and columns)for(std::vector<double> &newvec: vec){for(const double &elem: newvec){std::cout<<elem<<" ";}std::cout<<std::endl;}return 0;}
The output of the above program can be seen here.