Solved

use fstream to extract values from a txt file

Posted on 2011-02-17
2
519 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 34

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
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 NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

751 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