简体   繁体   中英

Reading numbers from a space delimited text file in C++

I'm trying to read a file "parameters.txt" from the working directory.

This is parameters.txt:

1.0 1.0 1.0 1.0 3.14159 1.57079 0.001 10.0

I want to input each of these numbers into a vector "data"

This is my code:

int main() {
    ifstream fin("parameters.txt");
    vector<double> data(8);

    int element;
    while (fin >> element) {
        data.push_back(element);
    }
    for (int i = 0; i < 8; i++) {
        cout << data[i];
    }
    cout << endl;
}

However, when I run the code, and try to output data, I get:

00000000

as an output. What is the problem?

If you want to reserve elements before inserting them, to make your code more efficient, you can use

vector<double> data;
data.reserve(8);
//push_back as you did

This creates an empty vector and sets the capazity to 8 elements. You can also omit the reserve, than it will be done for you, but not in one step.

Also note that you can replace the last for loop

for (int i = 0; i < 8; i++) {
        cout << data[i];
}

by a range-based for-loop, because the index is not necessary:

for (double i : data) {
  cout << i;
}

Your code has 3 bugs:

  1. you creating vector with 8 zero values at start: vector<double> data(8); , later you are adding new values at the end

  2. Your variable element has int type, so fin >> element will read only 1 from text file leaving .0 for further reading. During next iteration reading of int will fail since dot is encountered.

  3. You code always prints 8 values from vector (so if file opening failed and problem 1 is fixed you will have undefined behavior).

The solution is:

int main() {
    ifstream fin("parameters.txt");
    vector<double> data;

    double element;
    while (fin >> element) {
        data.push_back(element);
    }
    for (size_t i = 0; i < data.size(); i++) {
        cout << data[i] << ", ";
    }
    cout << endl;
}

https://wandbox.org/permlink/1CvTf7YY89C6hUyh

Which can be simplified to this:

int main()
{
    ifstream f{"parameters.txt"};
    vector<double> v{istream_iterator<double>{f}, {}};
    
    for (auto x : v) {
        cout << x << ", ";
    }
    cout << endl;

    return 0;
}

https://wandbox.org/permlink/Pcxz4gPakXwhzzvl

vector<double> data(8);

You defined data as vector of double type with 8 items that they initiated by double default 0.0 ! So using push_back will increase vector size and inserts at new index! So data[0] to data[7] stills are 0 !

You can try

vector<double> data(8);
int index = 0;
while (fin >> element && index < 8)
{
    data[index] = element;
    ++index;
}

Other problem! your file contains double values! So how you read them as int! such int element; fin >> element int element; fin >> element ?

modify code as below to work properly!

double element;
int index = 0;
while (fin >> element && index < 8)
{
    data[index++] = element;
}

The line

vector<double> data(8);

does not set the initial capacity of the vector, it create a vector with n initial default-constructed elements. Then you're pushing new values to the end of that vector. When you output the first n elements of the vector you are only getting those initial elements.

And, your input statement is inputting into an int variable which you're then storing into a vector of doubles.

Also your output statement does not add any spaces, so your zeros are all mushed together.

change int element to double element

int main() {
ifstream fin("parameters.txt");
vector<double> data(8);

double element;
while (fin >> element) {
    data.push_back(element);
}
for (int i = 0; i < 8; i++) {
    cout << data[i];
}
cout << endl;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM