How do I parse my strInput string into an array of structs?

Attached is my code & fake database.  I have a very large string where the fields are separated by tabs and spaces...

How do I parse it such that each minute of data gets its own struct like this?

struct Marketdata
{
   string strDate;
   string strTime;
   float fOpenPrice;
   float fHighPrice;
   float fLowPrice;
   float fClosePrice;
   long lngVolume;
}

I am new to programming and just finished reading the chapter on arrays and structs.  Also, I don't know how to parse a string into the structs...Any help would be greatly appreciated it.  Thanks!
#include <time.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <fstream>
//#include <afx.h>


using namespace std;


void GetTodaysDate(unsigned short &year, unsigned short &month, unsigned short &day)
{
	time_t rawtime;        //rawtime is variable of data type time_t, related to time.h
    struct tm * timeinfo; //timeinfo points to a struct of type tm, related to time.h
    char str[60];

    rawtime = time (NULL); //retrieves number of seconds from 1/1/70, in GMT, stores in address of rawtime
    timeinfo = localtime ( &rawtime ); //localtime takes rawtime, and creates a "tm" struct

   strftime(str, sizeof(str) , "%Y %m %d", timeinfo);  //formats the timeinfo struct into a string
//   printf(str);                                                       //outputs string to let me see if date is right
   
   year = atoi(&str[0]);   
   month = atoi(&str[5]);
   day = atoi(&str[8]);
}


string FindOneDayEarlier(unsigned short &year, unsigned short &month, unsigned short &day)
{
//months with 31 days: 1, 3, 5, 7, 8, 10, 12
//months with 30 days: 4, 6, 9, 11
//months with 28/29 days: 2

	day = day - 1; //decrement the day

    if (day == 0) //if day = 0, today must be 1st of the month
	{    
			if (month == 2 || month == 4 || month == 6 || month == 8 || month == 9 || month == 11 || month == 1)
			{
				day = 31;
			}

			if (month == 5 || month == 7 || month == 10 || month == 12)
			{
				day = 30;
			}
			if (month == 3)  //if not a leap year, who cares, just search, find nothing, skip, and decrement to 28
			{
				day = 29;
			}

			month = month - 1;
			if (month == 0)  //if this month is Jan, prior month will be Dec.
			{
				month = 12;  //if we are in Dec., we must be in prior year...so...
				year = year - 1; //we will never count back before year 2000, so it's always positive
			}
    }

   char buffer[40];
   sprintf( buffer, "C:\\\\%d\\\\%d\\\\%d\\\\ABC.txt", year, month, day);

   return string(buffer);
}


bool FileExist( string strFilepath )
{     
    // ifstream is used for reading files
	ifstream inf( strFilepath.c_str() );

    // If we couldn't open the output file stream for reading
    if (!inf)
    {
        return false;
    }
	else
		return true;	    
}

void GetValidFilepaths(unsigned short year, unsigned short month, unsigned short day, vector<string>& vFilePaths)
{

// Given today's date, get YESTERDAY's date in string form to be used in a file path
// then loop through past 9 days, and find out if filepath for each day is valid, or not
// if filepath is valid, save in a vector
	string strFilepath;
	int intNumDaystoLookBack = 9;
	int intCount = 0;  

	do
	{
		strFilepath = FindOneDayEarlier(year, month, day);//goes back in time, 1 day at a time

//  Given the FilePath for yesterday's date, find out if the Example.txt file exists
		if (FileExist(strFilepath))
		{
			intCount = intCount + 1;
            vFilePaths.push_back(strFilepath); // make note of the filepath and store it for future use
		}
	}while (intCount <= intNumDaystoLookBack);
}

string GetDataIntoString (vector<string> vFilePath)
{
	std::string strInput; //the String to read all data into

	unsigned int x = 0;
//	for(x = 0; x < vFilePath.size(); x++)  
	for(x = vFilePath.size(); x >= 1 ; x--)  
	{
		ifstream inf(vFilePath[x-1].c_str()); //ifstream is used for reading files, NOTICE (x-1)!!!!
    
		// While there's stuff left to read
		while (inf)
		{
				string s; inf >> s; //read stuff from the file into a string into string s
				strInput += (s + ' '); // append s to strInput
		}
	}

	return strInput;
}

int main()
{
// Get TODAY's date  
   unsigned short year = 0;
   unsigned short month = 0;
   unsigned short day = 0;

	GetTodaysDate (year, month, day);

	vector<string> vFilePaths;

//this goes back X number of days to retrieve file paths that are available and valid
    GetValidFilepaths (year, month, day, vFilePaths);

//this goes through the vector of valid file paths that lead to data, reads the file, puts the data into one, big string
	string strInput;
	strInput = GetDataIntoString (vFilePaths);

    return 0;
}

Open in new window

2010.zip
LVL 1
shaolinfunkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffric\Commented:
I believe the best way is to use getline to read in one line at a time rather than one token at a time. Then pass that string into a function which creates the struct that you have defined. The struct would then be pushed into a vector.
phoffric\Commented:
Be back tomorrow.
shaolinfunkAuthor Commented:
OK!
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

shaolinfunkAuthor Commented:
Tomorrow can you show me how to use getline?
evilrixSenior Software Engineer (Avast)Commented:
>> can you show me how to use getline?
Getline is nice and simple to use to read in a line to a string.
http://www.cplusplus.com/reference/string/getline/

Once you've read the string in parse it as phoffric suggests. You'll probably find the "find" function useful for doing this.
http://www.cplusplus.com/reference/string/string/find/
	string s;
	while(getline(fs, s))
	{
		// Use s
	}

Open in new window

shaolinfunkAuthor Commented:
ok..what is fs?

and where do i use getline?  at the point where i am reading input from the ABC.txt?  
evilrixSenior Software Engineer (Avast)Commented:
>> ok..what is fs?
Oh, sorry -- that's your file stream.

>> and where do i use getline?
>> at the point where i am reading input from the ABC.txt?  
Yes, read it a line at a time and then parse the line.
shaolinfunkAuthor Commented:
ok..is this what you meani should do?

also, how do "parse" the line?  that was the other part of my question that i don't know how to do..
phoffric\Commented:
How about going top down. Add in your struct at the top of your file. Then add a vector of that struct in your main function.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
phoffric\Commented:
Also in the main, you can add to main a variable that is a vector of lines (i.e., record entries that correspond to each line in your files)
phoffric\Commented:
>> and where do i use getline?
Here is getline definition, and a simple example of how to use it. The first argument in the example is cin which is your input stream from a console. (You'll change that later.)
     http://www.cplusplus.com/reference/string/getline/
// getline with strings
#include <iostream>
#include <string>
using namespace std;

int main () {
  string str;
  cout << "Please enter full name: ";
  getline (cin,str);
  cout << "Thank you, " << str << ".\n";
}

Open in new window

shaolinfunkAuthor Commented:
"Then add a vector of that struct in your main function."

Can you show me how to do this?  I never read the chapter on vectors, Chapter 10.  I just used the implementation that jkr gave me.
shaolinfunkAuthor Commented:
i noticed i'm including iostream...i've already included fstream...what's the difference between the two?
shaolinfunkAuthor Commented:
Is this what you mean by adding a vector of my struct?
int main()
{
// Get TODAY's date  
   unsigned short year = 0;
   unsigned short month = 0;
   unsigned short day = 0;

	GetTodaysDate (year, month, day);

	vector<string> vFilePaths;
	vector<MarketData> vFakeData;

//this goes back X number of days to retrieve file paths that are available and valid
    GetValidFilepaths (year, month, day, vFilePaths);

//this goes through the vector of valid file paths that lead to data, reads the file, puts the data into one, big string
	string strInput;
	strInput = GetDataIntoString (vFilePaths);

    return 0;
}

Open in new window

phoffric\Commented:
Headers are needed to define the api to the standard C++ library. Just remove it. Often more headers are added than necessary. Just comment it out, and if no errors, then fine. If errors, the errors will indicate which api was not defined.

vector<Type> variable;
for you:
vector<Marketdata> marketData;

Open in new window

phoffric\Commented:
Yes, now "Also in the main, you can add to main a variable that is a vector of lines (i.e., record entries that correspond to each line in your files)"
shaolinfunkAuthor Commented:
ok..i do not know what you mean here.  isn't vFakeData the variable that holds a vector full of marketdata structs?
shaolinfunkAuthor Commented:
i will comment out fstream and include iostream...but i'm still confused as to what is the difference between the two?
phoffric\Commented:
>> isn't vFakeData
You and I were writing at the same time. You can call a variable name anything you want. I was responding to your previous question "Can you show me how to do this?  I never read the chapter on vectors, Chapter 10.  I just used the implementation that jkr gave me." But then you figured it out.
shaolinfunkAuthor Commented:
ok..i think i implemented getline here in code....how do i parse into the vectors?
#include <time.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
//#include <afx.h>

using namespace std;

struct MarketData
{
   string strDate;
   string strTime;
   float fOpenPrice;
   float fHighPrice;
   float fLowPrice;
   float fClosePrice;
   long lngVolume;
};


void GetTodaysDate(unsigned short &year, unsigned short &month, unsigned short &day)
{
	time_t rawtime;        //rawtime is variable of data type time_t, related to time.h
    struct tm * timeinfo; //timeinfo points to a struct of type tm, related to time.h
    char str[60];

    rawtime = time (NULL); //retrieves number of seconds from 1/1/70, in GMT, stores in address of rawtime
    timeinfo = localtime ( &rawtime ); //localtime takes rawtime, and creates a "tm" struct

   strftime(str, sizeof(str) , "%Y %m %d", timeinfo);  //formats the timeinfo struct into a string
//   printf(str);                                                       //outputs string to let me see if date is right
   
   year = atoi(&str[0]);   
   month = atoi(&str[5]);
   day = atoi(&str[8]);
}


string FindOneDayEarlier(unsigned short &year, unsigned short &month, unsigned short &day)
{
//months with 31 days: 1, 3, 5, 7, 8, 10, 12
//months with 30 days: 4, 6, 9, 11
//months with 28/29 days: 2

	day = day - 1; //decrement the day

    if (day == 0) //if day = 0, today must be 1st of the month
	{    
			if (month == 2 || month == 4 || month == 6 || month == 8 || month == 9 || month == 11 || month == 1)
			{
				day = 31;
			}

			if (month == 5 || month == 7 || month == 10 || month == 12)
			{
				day = 30;
			}
			if (month == 3)  //if not a leap year, who cares, just search, find nothing, skip, and decrement to 28
			{
				day = 29;
			}

			month = month - 1;
			if (month == 0)  //if this month is Jan, prior month will be Dec.
			{
				month = 12;  //if we are in Dec., we must be in prior year...so...
				year = year - 1; //we will never count back before year 2000, so it's always positive
			}
    }

   char buffer[40];
   sprintf( buffer, "C:\\\\%d\\\\%d\\\\%d\\\\ABC.txt", year, month, day);

   return string(buffer);
}


bool FileExist( string strFilepath )
{     
    // ifstream is used for reading files
	ifstream inf( strFilepath.c_str() );

    // If we couldn't open the output file stream for reading
    if (!inf)
    {
        return false;
    }
	else
		return true;	    
}

void GetValidFilepaths(unsigned short year, unsigned short month, unsigned short day, vector<string>& vFilePaths)
{

// Given today's date, get YESTERDAY's date in string form to be used in a file path
// then loop through past 9 days, and find out if filepath for each day is valid, or not
// if filepath is valid, save in a vector
	string strFilepath;
	int intNumDaystoLookBack = 9;
	int intCount = 0;  

	do
	{
		strFilepath = FindOneDayEarlier(year, month, day);//goes back in time, 1 day at a time

//  Given the FilePath for yesterday's date, find out if the Example.txt file exists
		if (FileExist(strFilepath))
		{
			intCount = intCount + 1;
            vFilePaths.push_back(strFilepath); // make note of the filepath and store it for future use
		}
	}while (intCount <= intNumDaystoLookBack);
}

string GetDataIntoString (vector<string> vFilePath)
{
	std::string strInput; //the String to read all data into

	int x = 0;
//	for(x = 0; x < vFilePath.size(); x++)  
	for(x = (unsigned long)vFilePath.size(); x >= 1 ; x--)  
	{
		ifstream inf(vFilePath[x-1].c_str()); //ifstream is used for reading files, NOTICE (x-1)!!!! size = 10, and vector only goes up to 9..so x-1
    
		// While there's stuff left to read
		while (inf)
		{
				string s; 
				getline(inf,s); //read stuff from the file into a string into string s
				strInput += (s + ' '); // append s to strInput
		}
	}

	return strInput;
}

int main()
{
// Get TODAY's date  
   unsigned short year = 0;
   unsigned short month = 0;
   unsigned short day = 0;

	GetTodaysDate (year, month, day);

	vector<string> vFilePaths;
	vector<MarketData> vFakeData;

//this goes back X number of days to retrieve file paths that are available and valid
    GetValidFilepaths (year, month, day, vFilePaths);

//this goes through the vector of valid file paths that lead to data, reads the file, puts the data into one, big string
	string strInput;
	strInput = GetDataIntoString (vFilePaths);

    return 0;
}

Open in new window

phoffric\Commented:
If you make a few posts in rapid succession (minutes apart), then a reply may refer to the first post, not even knowing that there are subsequent posts.

>> i will comment out fstream and include iostream
OK, and uncomment just the ones needed to get things to compile. When you get to the chapters on streams, then it will be clearer.
shaolinfunkAuthor Commented:
turns out commenting out fstream yields errors when compiling,.. in the GetDataintoString function with the ifstream line in line 125.

i will include fstream and iostream both.
phoffric\Commented:
OK, don't post unless it compiles. If it doesn't compile and you cannot figure it out, then post and provide specific error and line numbers.

I meant that to find out the difference, you could comment out just one, say iostream, and see if it compiles.
As far as fstream goes, if you search on fstream, you'll find a ifstream inf( strFilepath.c_str() ); statement. That suggests that maybe fstream is needed. (f stands for file, and i stands for input, so you were defining inf as an input file stream.)
phoffric\Commented:
One thread with multiple questions is going to be confusing. Feel free to have multiple concurrent threads to help you understand all the different C++ language points you wish to learn about.
shaolinfunkAuthor Commented:
ok my fault..too many posts by me has gotten us out of posting rhythm.  i know you said i should implement getline, but am i implementing getline correctly here?

more precisely..is iostream's getline used to get the data INSTEAD of fstream's ifstream?  or do i get the data into a stream first using fstream's ifstream..and THEN use getline on the stream?

i dont know if i should be using one or both?
string GetDataIntoString (vector<string> vFilePath)
{
	std::string strInput; //the String to read all data into

	int x = 0;
//	for(x = 0; x < vFilePath.size(); x++)  
	for(x = (unsigned long)vFilePath.size(); x >= 1 ; x--)  
	{
		ifstream inf(vFilePath[x-1].c_str()); //ifstream is used for reading files, NOTICE (x-1)!!!! size = 10, and vector only goes up to 9..so x-1
    
		// While there's stuff left to read
		while (inf)
		{
				string s; 
				getline(inf,s); //read stuff from the file into a string into string s
				strInput += (s + ' '); // append s to strInput
		}
	}

	return strInput;
}

Open in new window

phoffric\Commented:
fyi - I understand that you got to where you are from multiple questions and from multiple experts. I accept what you have posted at face value since (1) it works, and (2) that you have accepted the results working with other experts.

In http:#29560208 I suggested that we go top down. You started successfully in this path, but didn't complete the main() function.

If you make a change to your program and it builds, then walk through the changes in your debugger, and see whether your changes did what you expected. If not, give it another shot. If still lost, then ask.

>> is iostream's getline used to get the data INSTEAD of fstream's ifstream?  or do i get the data into a stream first using fstream's ifstream..and THEN use getline on the stream?

ifstream is i=input, f=file, stream = steam of data
so inf is the input file stream handler that you defined. It was the way you opened each of the 10 files. But that is old news. Before you were doing something like inf >> s; which pushed one token from the file into a string s. You want to replace that with getline.

But let's get the main() defined first so that we can see a top-level flow of the data. Recall:
"Also in the main, you can add to main a variable that is a vector of lines (i.e., record entries that correspond to each line in your files)"

So, you've modified the main in a couple of ways, but now we have to use these changes in functions in the main.
phoffric\Commented:
I think we're in sync now.
phoffric\Commented:
Here is a tip. When passing in or out large amounts of data (e.g., a large vector), it is a good idea to pass by reference to avoid having to copy a large amount of data into a function's stack (and if not careful, can overflow the stack and cause a crash).
shaolinfunkAuthor Commented:
Ok, I've established my struct at the beginning of my code (see snippet), and established a variable that is a vector of lines, vFakeData (see snippet).

I've included my struct, my"GetDataIntoString" function, and my current main() code for your review.

I think that instead of using the code below where the data is put into a large string and returned to the main function, you want me to put the data, using getline, into the vector vFakeData somehow?  Do I understand you correctly?

If yes, can you show me how to re-write the GetDataIntoString function to accomplish getting the data into a vector?  I do not know how to put data into a vector, let alone a struct.

Once I know how to go from getline into a vector..then I do your 2nd step of passing by reference.  Sorry I have to do this one step at a time to get this straight in my head.

After this is finished, I will rename "GetDataIntoString" to something more precise, perhaps "GetDataintoStruct".
struct MarketData
{
   string strDate;
   string strTime;
   float fOpenPrice;
   float fHighPrice;
   float fLowPrice;
   float fClosePrice;
   long lngVolume;
};

string GetDataIntoString (vector<string> vFilePath)
{
	std::string strInput; //the String to read all data into

	int x = 0;
//	for(x = 0; x < vFilePath.size(); x++)  
	for(x = (unsigned long)vFilePath.size(); x >= 1 ; x--)  
	{
		ifstream inf(vFilePath[x-1].c_str()); //ifstream is used for reading files, NOTICE (x-1)!!!! size = 10, and vector only goes up to 9..so x-1
    
		// While there's stuff left to read
		while (inf)
		{
				string s; 
				getline(inf,s); //read stuff from the file into a string into string s
				strInput += (s + ' '); // append s to strInput
		}
	}

	return strInput;
}


int main()
{
// Get TODAY's date  
   unsigned short year = 0;
   unsigned short month = 0;
   unsigned short day = 0;

	GetTodaysDate (year, month, day);

//this goes back X number of days to retrieve file paths that are available and valid
	vector<string> vFilePaths;
    GetValidFilepaths (year, month, day, vFilePaths);

//this goes through the vector of valid file paths that lead to data, reads the file, puts the data into one, big string
	vector<MarketData> vFakeData;

	string strInput;
	strInput = GetDataIntoString (vFilePaths);

    return 0;
}

Open in new window

phoffric\Commented:
>> you want me to put the data, using getline, into the vector vFakeData somehow?  Do I understand you correctly?
Yes.

>>  I do not know how to put data into a vector, let alone a struct.
Putting data into a struct will be easy. But I thought you knew how to put data into a vector, since you are already doing it in your OP ??
phoffric\Commented:
Well anyway, let's look at the big picture (i.e., look at the data flow at the top level in main).
You have vFilePaths which will be filled in with valid file paths.
You have vFakeData, which for this question, is your final product, the vector of your MarketData structs.

But right now, vFakeData is dangling participle in main. So, let's put vFakeData in a function as an out variable (i.e., pass by reference).
shaolinfunkAuthor Commented:
All I know about vector is that pushback thing jkr put in my code. That's all I know about vectors, since I haven't even got to the chapter on vectors yet.  

I defined my vector of MarketData structs..and tried to pass by reference into my newly renamed function: GetDataIntoVector.  See code below....but I know I'm doing something wrong since I'm trying to cram the string 's' from getline into a struct that holds MarketData.  The string and MarketData struct dont' match.  So I just KNOW that's an error.

I cannot make the jump from string 's' that we getline'd in line 130 to this vector vFakeData that you asked me to create.  


void GetDataIntoVector (vector<string> vFilePath, vector<MarketData> &vFakeData)
{
	int x = 0;
	for(x = (unsigned long)vFilePath.size(); x >= 1 ; x--)  
	{
		ifstream inf(vFilePath[x-1].c_str()); 
    
		while (inf)
		{
				string s; 
				getline(inf,s);  
				vFakeData.push_back(s);
		}
	}
}


PART OF MAIN FUNCTION below:

    vector<string> vFilePaths;
    GetValidFilepaths (year, month, day, vFilePaths);

	vector<MarketData> vFakeData;

	GetDataIntoVector (vFilePaths, &vFakeData);

Open in new window

phoffric\Commented:
>> All I know about vector is that pushback thing jkr put in my code.
pushback thingy is what you need. jkr probably suggests; and you should accept only when you understand what you are accepting.

Your GetDataIntoVector api looks good.
     But your call to it in main is incorrect:
          GetDataIntoVector (vFilePaths, &vFakeData);
Your 2nd argument is a pointer! You want to pass by reference.

In GetDataIntoVector, you are trying to push a string into a MarketData struct. Just delete that for now:
   vFakeData.push_back(s);
We'll get into how to fill in your struct later - you first have to get a single line from a file, and then you want to get that line into the struct. But first get the single line using getline as you have already done. See if your solution to using getline works by stepping in the debugger.


shaolinfunkAuthor Commented:
"Your 2nd argument is a pointer! You want to pass by reference."

Ok, so I think you mean I need to take out the &, and just call "GetDataIntoVector (vFilePaths, vFakeData);"

I commented out vFakeData.push_back, and I stepped through the code and I can see "s" fill up in the locals window with data...line by line.  It works!

How do I get that "s" stuff into a struct?
phoffric\Commented:
Good.

Now you know how you have an Input File Stream, and you know how you have statements from your book (I hope) like:
   cin >> a >> b >> c;
where cin is your Console Input Stream, and >> is the operator that reads the stream (whether it is from a File, or from a Console) and pumps the tokens into a, b, and c.

Right? If not, let me know, and we'll figure out what to do?

Well there's another stream of interest. If you have a string, then you can think of that as a source of a stream of chars just like a file is or a Console is. Do you see the analogy?

In other words, you will be able to setup the single line string and, if you will, pretend for a moment that it is a file. Then you will be able to use the >> operator just as if you were reading a file or a Console and pumping data into a, b, and c.

Do you have the gist of what I'm talking about?
Just as you needed to include <fstream> for the file stream, now you will need to include <sstream> for the string stream.

I'll post some online examples when I find them.


phoffric\Commented:
Here is a simple short 4 example tutorial on string streams. One easy way to understand this topic is to create a learning project and walk through the examples to firm up what is happening. If you do this and have any questions, then it will be easy to ask it (but not as a related question - just a learning C++ question; post the code AND the link where you got it, and ask where you have a problem).

      http://www.java2s.com/Tutorial/Cpp/0240__File-Stream/0380__stringstream.htm
phoffric\Commented:
You will put the proper tokens in a single line string individually into each member of your struct.
phoffric\Commented:
What you can do, is define a temporary struct, fill it in with the tokens from the line, and then use push_back to add the struct to your vector of struts.
shaolinfunkAuthor Commented:
i am going over the stuff you sent and trying to figure out what you're suggesting.
shaolinfunkAuthor Commented:
Ok, I created a temporary struct called mdTemp.

Then I try to stuff "s" into the temporary struct using the >> operators.

But clearly, I am doing something wrong since I get a lot of compile errors.

What did I do wrong?  
void GetDataIntoVector (vector<string> vFilePath, vector<MarketData> &vFakeData)
{
	std::string strInput;

	int x = 0;
	for(x = (unsigned long)vFilePath.size(); x >= 1 ; x--)  
	{
		ifstream inf(vFilePath[x-1].c_str()); //ifstream is used for reading files, NOTICE (x-1)!!!! size = 10, and vector only goes up to 9..so x-1
    
		MarketData mdTemp;

		while (inf)  // While there's stuff left to read
		{
				string s; 
				getline(inf,s); //read stuff from the file into a string into string s
				s >> mdTemp.strDate >> mdTemp.strTime >> mdTemp.fOpenPrice >> mdTemp.fHighPrice >> mdTemp.fLowPrice >> mdTemp.fClosePrice >> mdTemp.lngVolume ;
				vFakeData.push_back(mdTemp);
		}
	}
}



1>------ Build started: Project: junk, Configuration: Debug Win32 ------
1>Compiling...
1>Junk.cpp
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\string(425) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\string(425) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\string(425) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\string(425) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(1016) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(1016) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(1016) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(1016) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(1009) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(1009) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(1009) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(1009) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(1002) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(1002) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(1002) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(1002) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(995) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(995) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(995) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(995) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(971) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(971) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(971) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(971) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem *)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(930) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem *)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(930) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem *)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(930) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem *)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\istream(930) : see declaration of 'std::operator >>'
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(133) : error C2676: binary '>>' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
1>Build log was saved at "file://c:\Documents and Settings\Administrator\Desktop\junk\junk\Debug\BuildLog.htm"
1>junk - 29 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Open in new window

evilrixSenior Software Engineer (Avast)Commented:
Heh. I see phoffric and you have made good progress whilst I've been at the pub enjoying their finest ale :)

I'll kick back and let you both get on with this. I'll continue to monitor in case I can be of any help but you are in very good hands so I doubt there'll be much left for me to contribute :)

-Rx.
shaolinfunkAuthor Commented:
Haha, nice.  Well feel free to weigh in on my current roadblock:

Given string 's'....where 's' might look like:

 "3/11/2010 9:30:00 AM      27.95      28.04      27.94      28.03      14679"

How do I use those >> operators so that I can parse out the elements of the 's' string into my struct?
shaolinfunkAuthor Commented:
I think I might be making just an iota of progress...I changed the code a little bit, and now the date and time (which are strings) are properly stored into the struct, and subsequently pushed into a vector.

But when i step through the code i see that the floats and long members of the struct are filled with garbage, and not the values from the ABC.txt.

What am i doing wrong?
void GetDataIntoVector (vector<string> vFilePath, vector<MarketData> &vFakeData)
{
	std::string strInput;

	int x = 0;
	for(x = (unsigned long)vFilePath.size(); x >= 1 ; x--)  
	{
		ifstream inf(vFilePath[x-1].c_str()); //ifstream is used for reading files, NOTICE (x-1)!!!! size = 10, and vector only goes up to 9..so x-1
    
		MarketData mdTemp;

		while (inf)  // While there's stuff left to read
		{
				string s; 
				getline(inf,s); //read stuff from the file into a string into string s

				stringstream ss;
				ss << s;

				ss >> mdTemp.strDate >> mdTemp.strTime >> mdTemp.fOpenPrice >> mdTemp.fHighPrice >> mdTemp.fLowPrice >> mdTemp.fClosePrice >> mdTemp.lngVolume ;
				vFakeData.push_back(mdTemp);
		}
	}
}

Open in new window

phoffric\Commented:
OK, I'm back from watching Clijsters beat Williams and Roddick beat Nadal. Now I'm motivated to go out and hit later in this nice 75 degree F day today.
phoffric\Commented:
@evilrix,
If there's a gap in time, feel free to join in the party. I have got to get out later for tennis after watching it on TV.
phoffric\Commented:
Interesting, you are using << where I only used >>

Now, there's a mismatch in how many tokens you have in you >> statement vs what you have in your file.
If this is not clear, then try doing one >> for each token.
phoffric\Commented:
I meant to say try having one >> in a separate statement for each token.

In order to use just the >>, then you can modify your
    stringstream ss;
to
   stringstream ss(s);
phoffric\Commented:
I should go back and find out how many times here and in other threads, you ask a question, and within a few minutes you post that you figured it out on your own. Because of this behavior, how about you ask yourself the question, and then answer it within a few minutes. Then if you want, you can keep a log and post all of your own Q&A and intermediate results. Will make for an interesting monologue. :)
shaolinfunkAuthor Commented:
"I meant to say try having one >> in a separate statement for each token"

Lol.  Ok, I thought about it for some time before posting...but I can't visualize what you are suggesting that I do in your statement above..:(
shaolinfunkAuthor Commented:
also, i thought stringstream was a data type.  so i have no idea what this does:

stringstream ss(s);

because it looks like a function!  

very confused now.
phoffric\Commented:
OK, you are learning by patterns rather than by text. Somehow, in your code you have:
    ifstream inf( strFilepath.c_str() );
as well as:
    ifstream inf(vFilePath[x-1].c_str());

So, they look like functions also! But in fact they are called constructors which are used to initialize an object inf of the class ifstream. Likewise,
   stringstream ss(s);
is instantiating an object of type stringstream and initializing this stream with the string s.
phoffric\Commented:
If you were to do (elsewhere)
  cin >> a >> b >> c;
Do you know how to write something equivalent where you only have one >> but three lines of code?
shaolinfunkAuthor Commented:
Interesting..you just taught me something about myself that I didn't really know.  You're 100% absolutely RIGHT, I do learn by observing and repeating patterns!!

In all the examples of cin and cout that I've seen in my book...everything is done in 1 line of code, not three separate lines.

It always looks something like A >> 1 >> 2 >> 3;
phoffric\Commented:
Duh, you're human right? We are used to learn by patterns, including pattern baldness. I took a 10-week language course (4 semesters) where the two instructors didn't speak English for the first 2 weeks. They just kept talking and pointing at us and things. It was pretty cool until they started speaking English. (I think they lost patience.) Since you are choosing to work on a complex project in C++ despite me and others advising against that, you have chosen to throw yourself into a pattern learning approach. BTW, it might have been easier for you to do this in C, since the learning curve is about 85% faster.

Look at:
   http://www.java2s.com/Tutorial/Cpp/0100__Development/cinHandlesDifferentDataTypes.htm
and throw away the cout prompts.

>> something like A >> 1 >> 2 >> 3;
I assume you didn't really mean to pump data into literal integer constants but were speaking figuratively.

I just mentioned the forbidden word "class". Of course, I realize that you don't know what a class is! You should think of it as a type, like int, float, but not a built-in type. It's a type that the programmer can make up - let's call it User Defined Type, or UDT for short. To ease the burden, many types were included in the standard C++ library to facilitate programming. You already are using vector (part of STL). You already are familiar with string, which is not part of the C++ language; it is part of the C++ standard library (remember it came later in the game after some of the file open library mechanisms were created, so that is why file open approaches still use the old c-style strings rather then the awesome C++ string class).
shaolinfunkAuthor Commented:
Ok, hopefully I'm doing below what you're suggesting I do.  

But, I run into the same problem mentioned above in my 2:41PM post:

"But when i step through the code i see that the floats and long members of the struct are filled with garbage, and not the values from the ABC.txt."
void GetDataIntoVector (vector<string> vFilePath, vector<MarketData> &vFakeData)
{
	std::string strInput;

	int x = 0;
	for(x = (unsigned long)vFilePath.size(); x >= 1 ; x--)  
	{
		ifstream inf(vFilePath[x-1].c_str()); 
    
		MarketData mdNow;

		while (inf)  // While there's stuff left to read
		{
				string s; 
				getline(inf,s); 

				stringstream ss(s); //similar to ifstream inf
		 
 				ss >> mdNow.strDate;
				ss >> mdNow.strTime;
				ss >> mdNow.fOpen;
				ss >> mdNow.fHigh; 
				ss >> mdNow.fLow; 
				ss >> mdNow.fClose; 
				ss >> mdNow.lngVolume ;
 
//				vFakeData.push_back();
		}
	}
}

Open in new window

shaolinfunkAuthor Commented:
Specifically  I see -1.0737418e+008.  I think this has to do with the fact that I'm putting a string into a variable designated as a float....do I use that atoi() or atof() or atol() function somehow?
phoffric\Commented:
How many tokens do you have in your file. To determine a token, open a file in notepad and double-click on each token.
shaolinfunkAuthor Commented:
WOW.  THANK YOU SO MUCH!  I think I finally, finally got it.

AM/PM..is its own token.  so I modified the struct accordingly.

Now the code below works!!
void GetDataIntoVector (vector<string> vFilePath, vector<MarketData> &vFakeData)
{
	std::string strInput;

	int x = 0;
	for(x = (unsigned long)vFilePath.size(); x >= 1 ; x--)  
	{
		ifstream inf(vFilePath[x-1].c_str()); //ifstream is used for reading files, NOTICE (x-1)!!!! size = 10, and vector only goes up to 9..so x-1
    
		MarketData mdNow;

		while (inf)  // While there's stuff left to read
		{
				string s; 
				getline(inf,s); //read stuff from the file into a string into string s

				stringstream ss(s); //similar to ifstream inf
		 
 				ss >> mdNow.strDate;
				ss >> mdNow.strTime;
				ss >> mdNow.strAMPM;
				ss >> mdNow.fOpen;
				ss >> mdNow.fHigh; 
				ss >> mdNow.fLow; 
				ss >> mdNow.fClose; 
				ss >> mdNow.lngVolume ;
 
				vFakeData.push_back(mdNow);
		}
	}
}

Open in new window

shaolinfunkAuthor Commented:
Can't thank Phoffric enough!
phoffric\Commented:
Great. Now if you want time to consolidate AMPM, you can do that rather than adding AMPM to the struct. Use the + sign operator to concatenate.
shaolinfunkAuthor Commented:
? You've lost me.  Where do I put this + sign operator?
phoffric\Commented:
See if this works.
...
string ampm;
ss >> mdNow.strTime;
ss >> ampm;
mdNow.strTime += (' ' + ampm);
ss >> mdNow.fOpen;
...

Open in new window

shaolinfunkAuthor Commented:
You're Phoffric, of course it works!

 (yes, i implemented it and tested it and no surprise, it works!)
phoffric\Commented:
I was just one step ahead of you. You did most of the hard work.
phoffric\Commented:
Do you think you graduated yet from Junior HS yet? Or are you still in the 8th grade?
shaolinfunkAuthor Commented:
Haha, many thanks again Phoffric. :)  When it comes time to make this shabby piece of functional code faster, more efficient, and more elegantly structured I'll come looking for the Master.
phoffric\Commented:
haha shaolinfunk, you are the master already.
phoffric\Commented:
>> whilst I've been at the pub enjoying their finest ale :)
Did you drink and drive? I live in the suburbs of Philadelphia and the nearest pub is miles away.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.