473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need help with Loading data from Text file

I am very new to C++. I need some help with loading/reading data from text file and write in an array.

I have the following data.
12/31/2004 1213.55 1217.33 1211.65 1211.92 786900000 1211.92
12/30/2004 1213.45 1216.47 1213.41 1213.55 829800000 1213.55
12/29/2004 1213.54 1213.85 1210.95 1213.45 925900000 1213.45
12/28/2004 1204.92 1213.54 1204.92 1213.54 983000000 1213.54
12/27/2004 1210.13 1214.13 1204.92 1204.92 922000000 1204.92
12/23/2004 1209.57 1213.66 1208.71 1210.13 956100000 1210.13

I want to read the data from "CompFinSumAssi.txt" file and write in an array. I want access the data by columns. Here is what I have done so far. Program runs but goes into a infinite loop. I doesn't display the right values either. Someone please let me know what I am doing wrong. Note that so far I didn't try to write into an array, I just tried to display on the screen first. Hint into how to write into an array will be well appreciated.
Expand|Select|Wrap|Line Numbers
  1. #include<fstream>
  2. using std::ifstream;
  3. #include<cstdlib> // this is exit function
  4. // Read the values from CompFinSumAssi.txt
  5.  
  6. int Load_Price_Data()
  7. {
  8.  
  9.   ifstream indata;
  10.  
  11.   float date, Open, high, low, close,volume, price; //variable for input price value
  12.  
  13.   //char date; //variable for input date
  14.  
  15.   indata.open("CompFinSumAssi.txt"); //opens the file
  16.   if(!indata) {
  17.     cerr << "Error: file could not be opened" << endl;
  18.     exit(1);
  19.   }
  20.  
  21.   // Read the data from the file
  22.  
  23.   indata >> date >> Open >> high >> low >> close >> volume >> price; 
  24.  
  25.   while( !indata.eof() ) { // keep reading until end-of-file
  26.     cout << "The data in the file is:" << date << " "<<Open<<" "<<high<<" "<<low<<" "<<close<<" "<<volume<<" "<<price<<endl;
  27.  
  28.     indata >> date >> Open>> high >> low >> close >> volume >> price;   
  29.  
  30.     //indata >> date;
  31.   }
  32.  indata.close();
  33.   cout <<"End of file is reached" << endl;
  34.   return 0;
  35. }
  36.  
  37. int main() {
  38.  Load_Price_Data();
  39. }
  40.  
Thanks
Raam
Sep 6 '07 #1
6 3515
Savage
1,764 Expert 1GB
I am very new to C++. I need some help with loading/reading data from text file and write in an array.

I have the following data.
12/31/2004 1213.55 1217.33 1211.65 1211.92 786900000 1211.92
12/30/2004 1213.45 1216.47 1213.41 1213.55 829800000 1213.55
12/29/2004 1213.54 1213.85 1210.95 1213.45 925900000 1213.45
12/28/2004 1204.92 1213.54 1204.92 1213.54 983000000 1213.54
12/27/2004 1210.13 1214.13 1204.92 1204.92 922000000 1204.92
12/23/2004 1209.57 1213.66 1208.71 1210.13 956100000 1210.13

I want to read the data from "CompFinSumAssi.txt" file and write in an array. I want access the data by columns. Here is what I have done so far. Program runs but goes into a infinite loop. I doesn't display the right values either. Someone please let me know what I am doing wrong. Note that so far I didn't try to write into an array, I just tried to display on the screen first. Hint into how to write into an array will be well appreciated.

Expand|Select|Wrap|Line Numbers
  1. #include<fstream>
  2. using std::ifstream;
  3. #include<cstdlib> // this is exit function
  4. // Read the values from CompFinSumAssi.txt
  5.  
  6. int Load_Price_Data()
  7. {
  8.  
  9.   ifstream indata;
  10.  
  11.   float date, Open, high, low, close,volume, price; //variable for input price value
  12.  
  13.   //char date; //variable for input date
  14.  
  15.   indata.open("CompFinSumAssi.txt"); //opens the file
  16.   if(!indata) {
  17.     cerr << "Error: file could not be opened" << endl;
  18.     exit(1);
  19.   }
  20.  
  21.   // Read the data from the file
  22.  
  23.   indata >> date >> Open >> high >> low >> close >> volume >> price; 
  24.  
  25.   while( !indata.eof() ) { // keep reading until end-of-file
  26.     cout << "The data in the file is:" << date << " "<<Open<<" "<<high<<" "<<low<<" "<<close<<" "<<volume<<" "<<price<<endl;
  27.  
  28.     indata >> date >> Open>> high >> low >> close >> volume >> price;   
  29.  
  30.     //indata >> date;
  31.   }
  32.  indata.close();
  33.   cout <<"End of file is reached" << endl;
  34.   return 0;
  35. }
  36.  
  37.  
  38.  
  39. int main() {
  40.  Load_Price_Data();
  41. }
  42.  
Thanks
Raam

You might want to check for the stream state in your while loop.You can do this either with good or bad basic_stream methods
Sep 6 '07 #2
sicarie
4,677 Expert Mod 4TB
Yeah, I believe what Savage is getting to lies in your date (though I'm not sure, Savage, please let me know if I am mis-quoting you), and attempting to read the '/'s into floats. I believe that conversion is messing you up. Have you looked into a stringstream to parse those?

ie, use getline() to get a line of data, stringstream to parse into variables...

As for arrays, do you know how many there are going to be? Are you going to create an array of structures/classes to hold each line, or separate arrays for each different type of variable (and then for each reference, say the first, all the entries will be in slot 0 of all the arrays)?
Sep 6 '07 #3
Yeah, I believe what Savage is getting to lies in your date (though I'm not sure, Savage, please let me know if I am mis-quoting you), and attempting to read the '/'s into floats. I believe that conversion is messing you up. Have you looked into a stringstream to parse those?

ie, use getline() to get a line of data, stringstream to parse into variables...

As for arrays, do you know how many there are going to be? Are you going to create an array of structures/classes to hold each line, or separate arrays for each different type of variable (and then for each reference, say the first, all the entries will be in slot 0 of all the arrays)?
Thank you, I realized my problem is in the date '/', Can I do that using a char or char*?

Thanks
Raam
Sep 6 '07 #4
sicarie
4,677 Expert Mod 4TB
Thank you, I realized my problem is in the date '/', Can I do that using a char or char*?

Thanks
Raam
You might be able to declare a string and still use the fin >> operator as you are now, but if you can't, I would recommend a stringstream.
Sep 6 '07 #5
Yeah, I believe what Savage is getting to lies in your date (though I'm not sure, Savage, please let me know if I am mis-quoting you), and attempting to read the '/'s into floats. I believe that conversion is messing you up. Have you looked into a stringstream to parse those?

ie, use getline() to get a line of data, stringstream to parse into variables...

As for arrays, do you know how many there are going to be? Are you going to create an array of structures/classes to hold each line, or separate arrays for each different type of variable (and then for each reference, say the first, all the entries will be in slot 0 of all the arrays)?
Thanks, I know exactly how many lines of data I have. I plan to use an m*n dimensional array. In this case I know exactly 'm' and 'n' are. May be I should do is use getline() and store the line in the first row of the matrix. Then hope to access the data column wise.

Thanks
Raam
Sep 6 '07 #6
sicarie
4,677 Expert Mod 4TB
Thanks, I know exactly how many lines of data I have. I plan to use an m*n dimensional array. In this case I know exactly 'm' and 'n' are. May be I should do is use getline() and store the line in the first row of the matrix. Then hope to access the data column wise.

Thanks
Raam
Just keep types in mind when you declare that size m*n array, you're only going to be able to use the type it is declared as. If you pull them directly from getline (ie as strings), you can put them all in a char* or string array, and then use atof() to convert those you need to, or you can convert them, create separate m number of n-sized arrays of different types, and do what you need with them. The second method might be preferred just for the sake of code preservation - if you need to go back and change it later you'll get weird errors until you remember that you need to atof() them...

Seems like you've got it, though. Please feel free to post again if you get stuck.
Sep 6 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Bill Nguyen | last post by:
I ran into errors trying to open an xml file in IE with content as below. I need to access this file using ReadXML in .NET but do not where to start. Can you please tell me what XML files I may...
5
by: kutty | last post by:
Hi All, I am loading data to a child table from a text file. the text files also contains data not referenced by parent key. while loading the data if one row fails to satisfies the constraint...
5
by: yeoj13 | last post by:
Hello, I have a db2load script I'm using to populate a large table. Ideally, my target table is required to have "Not Null" constraints on a number of different columns. I've noticed a ...
4
by: shelley_2000 | last post by:
What is the best approach to collect and load Employee Resume Data from External Employees who may not have Microsoft access? If is likely they will have Microsoft Word, but not Microsoft Access. ...
9
by: igor.tatarinov | last post by:
Hi, I am pretty new to Python and trying to use it for a relatively simple problem of loading a 5 million line text file and converting it into a few binary files. The text file has a fixed format...
2
by: sriniwas | last post by:
Hi Frnd's, m using prefuse visulation,it's have one display class and this class have one saveImage(outPutStream, String jpg,double size);. now graph is converting ia jpg image properly.now my...
0
by: deepakNagpal | last post by:
hi, friends, i got stuck in a problem, where i am not able to load the swf files in the another swf files through xml file, running online in ie browser. 1. There are two button on the stage...
5
by: Chuck Anderson | last post by:
I run Apache 2.0.55, and Php (both 4.4.1 and 5.2.5) on my home PC (Windows XP). One of the scripts that I run daily needs to access a secure URL (https://..............). When I am running Php4,...
3
by: prayinjamat | last post by:
here are the parameters in which i am working: i have Google Maps mashup that uses a text file to contain all the data points i use the text file because i have no experience with mysql or any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.