Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

use fstream to extract values from a txt file

Posted on 2011-02-17
2
Medium Priority
?
524 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 35

Accepted Solution

by:
sarabande earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.

670 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