1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

(C++) trouble importing numerical, non integer, data

Discussion in 'C++' started by lnname, Jan 20, 2010.

  1. lnname

    lnname New Member

    Joined:
    Jan 20, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    york uk
    i have a file called "test_data_4" which contains a single column of 20001 numerical values

    "1.09964047e+001
    1.10765811e+001
    1.11526284e+001
    1.12244983e+001
    1.12921441e+001
    1.13555205e+001
    1.14145834e+001
    1.14692903e+001
    1.15196004e+001
    ...."

    and so on

    I am writting a program that reads the file and have got stuck.

    Code:
    #include <vector>
    #include <cstdlib>
    using namespace std;
    int main()
     {
     ifstream infile("test_data_4.dat");
            cout << "reading from file"<<endl;
            cout<<"how many points shall we consider ";
            int N=0;cin>>N;         //the numbers of data points we shall take from the file
            vector<long> data(N);   //the vector we assemble from the file
            cout<<"reading from the file "<<endl;
            long n=2;int j=0;
            while(j<N)
                {
                infile >> n;cout<<n<<endl;
                data[j]=n;
                j++;
                }
            // Note that n is overwritten every time we loop
     infile.close();
        cout << "The file comtains the list of values:"<<endl;
            for(int i=0; i<(int) data.size(); i++)
                {cout << data[i] << endl;}
     
     return 0;
     }
    
    It currently seems to think that all the above are simply repetitions of the number 1.
     
    Last edited by a moderator: Jan 21, 2010
  2. lnname

    lnname New Member

    Joined:
    Jan 20, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    york uk
    Hmmnn i've tried importing various different versions of test_data_4 and it seems to import all the data up to the full stop
     
  3. lnname

    lnname New Member

    Joined:
    Jan 20, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    york uk
    actually problem solved; i was using longs instead of long doubles
     

Share This Page