Solved

use fstream to extract values from a txt file

Posted on 2011-02-17
2
515 Views
Last Modified: 2013-12-14
Hi, I am a newbie c++ programmer. I haven't done OOP in class yet.

I have a bit of an issue with my code.
I am trying to get stores consecutive values into their appropriate identifier from a txt file.
I am having problems doing it.

Thanks for your help.
#include <iostream>
#include<fstream>
#include<string>

using namespace std;


int main(){
	ifstream in;
	ofstream out;
	string line;

	in.open("data.txt");
	in>>line;

	//skip the first line
	in>>line;

	//print into a second txt file the date, open, high, low, close, up, and down
	//declare variables
	int day0,day1,month,year;
	float open,close, high, low;
	int up, down;

	//use while(!in.eof()) for the whole file
	for (int i = 0; i<10;++i)
	{day0 = line.substr(0,2);
	month = line.substr(3,2);
	year = line.substr(6,4);

	//find the ',' so that I will be able to extract the different float values
	int m1 = line.find(',');
	int m2 = line.find(',',m1+1);
	int m3 = line.find(',',m2+1);
	int m4 = line.find(',',m3+1);
	int m5 = line.find(',',m4+1);
	int m6 = line.find(',',m5+1);

	open =line.substr(m2,m3);
	//it's not working.
	//any suggestion on how to grab those values and store them into my variables?????

	//write the variables into a different txt file
	out.open("data2.txt";
	out<<day0<<'//'<<month<<'//'<<year<<':'<<(open+close)/2;
	out.close();
	}
return 0;}

Open in new window

data.txt
0
Comment
Question by:pgmerLA
2 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 34922613
use atoi and atof to convert substring to int or float.
use c_str() to convert string to c-style string.
move file open/close outside of loop to get all data into one output file.
removed warnings about unreferenced variables.
close variable was not initialized, so I dummied up a value for it, but now you should be able to get the right value.

Had to use VS 2010 debugger to get the substring indexes straight. Easy to see what is wrong when the line string is right in front of you.

If you haven't already done so, you can download the free Visual Studio Expresss C++ 2010:
    http://www.microsoft.com/express/Downloads/

I like the VS C++ debugger so much that I wrote articles on it. To quickly get started (about 15 minutes learning curve), you can read:

   C/C++ Beginner's Debugging Guide

After becoming familiar with the basics, move onto these two articles:
   Breakpoint Tips for C/C++

   Watch, Memory, Stack Tips: C/C++


#include <iostream>  
#include<fstream>  
#include<string>  
#include <stdlib.h>

using namespace std;  


int main(){  
   ifstream in;  
   ofstream out;  
   string line;  

   in.open("data.txt");  
   in>>line;  

   //skip the first line  
   in>>line;  

   //print into a second txt file the date, open, high, low, close, up, and down  
   //declare variables  
   int day0,month,year;  
   float open,close = 100.0;  

   //write the variables into a different txt file  
   out.open("data2.txt"); 
   //use while(!in.eof()) for the whole file  
   for (int i = 0; i<10;++i)  
   {
      month  = atoi( line.substr(0,2).c_str() );  
      day0 = atoi(line.substr(3,2).c_str() );  
      year = atoi( line.substr(6,4).c_str() );  

      //find the ',' so that I will be able to extract the different float values  
      int m1 = line.find(',');  
      int m2 = line.find(',',m1+1);  
      int m3 = line.find(',',m2+1);  
      int m4 = line.find(',',m3+1);  
      int m5 = line.find(',',m4+1);  
      int m6 = line.find(',',m5+1);  

      open = (float)atof( line.substr(m2+1,m3-m2-1).c_str() );

      out<<day0<<"/"<<month<< "/"<<year<<':'<<(open+close)/2 << endl;  

   }  
   out.close();  
   return 0;
}

Open in new window

0
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
ID: 34924764
you could use

   string strline;
   while (getline(in, strline))
   {
        ...
   }

to read the file line by line.

for parsing the line you might take stringstream of header <sstream>

      char s1, s2, s3, s4;  // for separators
      istringstream istemp(strline);  // feed it with the line
      if (!(istemp >> month >> s1
                        >> day0  >> s2
                        >> year  >> s3
                        >> t        >> s4
                        >> open >> s5
                         // ...   and so on
             ))
          continue;  // ignore errors ?


Sara

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now