Public Group

This topic is 3812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In fact I have a txt file (or dat file), contain a list to do some operations. The list is like this: date open high low close volume turnover 2003-12-18 4.533 4.582 4.283 4.508 1588699385 7063136408 2003-12-19 4.508 4.632 4.458 4.582 557293890 2510226274 . . . . . . Here is part of my program, if I do not use the function getline(), what other function I can use to save the parameter into different arrays? I know there are other function such as read and get, but I do not know if they can be used as this purpose. void readdata() { string line; ifstream myfile("abc.txt"); if(myfile.is_open()) { while(!myfile.eof()) { getline(myfile,line); } } myfile.close(); } How should I modify my program? Thank you for your kindly help. [Edited by - gingsi on January 15, 2008 10:44:42 AM]

##### Share on other sites
You may find the >> ifstream operator useful. Here is a simple usage example that is not very robust:

void readdata(){    string date;    float open = 0.0;    float high = 0.0;    float low = 0.0;    /* ... etc. for the rest of your data fields */    ifstream myfile("abc.txt");        if (myfile.is_open())    {        myfile >> date;        myfile >> open;        myfile >> high;        myfile >> low;        /* ... etc. for the rest of your data fields */        myfile.close();    }}

The above will read the data fields of the first line of the text file into the corresponding variables. If you want to use the above example, you will need to add a loop to read all lines as well as incorporate your data structure of choice for storing the data. Note that you may need to ignore the first line in the file since it is labeling information rather than actual data. Hope this helps.

##### Share on other sites
Thank you very much for your help. However, I have tried to use operator >> before. It does not work, the error shown is:

C2679:binary '>>': no operator found which takes a right-hand operand of type 'float'(or there is no acceptable conversion)

By the way, the program is writing as window form application. As I am writing a GUI program, does this affect for the use of >> operator?
Thank you very much.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 28
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631407
• Total Posts
2999915
×