Solved

use fstream to extract values from a txt file

Posted on 2011-02-17
2
518 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 33

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

828 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