Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 525
  • Last Modified:

use fstream to extract values from a txt file

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
pgmerLA
Asked:
pgmerLA
1 Solution
 
phoffricCommented:
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
 
sarabandeCommented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now