I've been working on Microsoft Visual C++ 2010(32-bit system)
In the compilation phase i get an error that says:
1>------ Build started: Project: pruebavecot, Configuration: Debug Win32 ------
1> pruebavecot.cpp
1>c:\users\andresgraco\desktop\pruebavecot\pruebavecot\pruebavecot.cpp(64): error C2057: expected constant expression
1>c:\users\andresgraco\desktop\pruebavecot\pruebavecot\pruebavecot.cpp(64): error C2466: cannot allocate an array of constant size 0
1>c:\users\andresgraco\desktop\pruebavecot\pruebavecot\pruebavecot.cpp(64): error C2440: 'initializing' : cannot convert from 'std::string' to 'double *[]'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("Vetor_Oscilacao.txt");
if (myfile.is_open())
{
int i=1;
while ( getline (myfile,line) )
{
cout << stod(line) << '\n';
for(double i=1; i<100; i++)
{
double in[i]=line;
}
}
myfile.close();
}
else cout << "Unable to open file";
getchar();
return 0;
}
I am trying to receive data from a .txt file and store it in the vector (in [i]) for later use in an fftw. The data in the .txt file is organized as follows:
21.000000
24.000000
25.000000
25.000000
21.000000
22.000000
24.000000
25.000000
...(data #100)
Thank you very much for your help.
You cannot assign a string directly to an array. And your for
loop doesn't make sense to have, either.
Try something more like this instead:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main ()
{
std::ifstream myfile ("Vetor_Oscilacao.txt");
if (myfile.is_open())
{
std::vector<double> in;
std::string line;
while (std::getline(myfile, line))
{
double value = std::stod(line);
std::cout << value << '\n';
in.push_back(value);
}
myfile.close();
// use 'in' as needed...
}
else
std::cout << "Unable to open file";
std::cin.get();
return 0;
}
Alternatively, since all of the lines are floating point numbers, you can use operator>>
instead of std::getline()
, and let it handle the parsing for you:
#include <iostream>
#include <fstream>
#include <vector>
int main ()
{
std::ifstream myfile ("Vetor_Oscilacao.txt");
if (myfile.is_open())
{
std::vector<double> in;
double value;
while (myfile >> value)
{
std::cout << value << '\n';
in.push_back(value);
}
myfile.close();
// use 'in' as needed...
}
else
std::cout << "Unable to open file";
std::cin.get();
return 0;
}
Which can then be simplified further by using std::copy()
with std::istream_iterator
, instead of a manual loop:
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
int main ()
{
std::ifstream myfile ("Vetor_Oscilacao.txt");
if (myfile.is_open())
{
std::vector<double> in;
std::copy(
std::istream_iterator<double>(myfile),
std::istream_iterator<double>(),
std::back_inserter(in)
);
myfile.close();
for (size_t i = 0; i < in.size(); ++i)
std::cout << in[i] << '\n';
// use 'in' as needed...
}
else
std::cout << "Unable to open file";
std::cin.get();
return 0;
}