Great help Elysia... I will try to buy this version. I assume it is called: C++ Proffesional.
It really sounds what I will need after I have learned the basics etc... But still also for the basics why it sounds perfect... :)
Printable View
Great help Elysia... I will try to buy this version. I assume it is called: C++ Proffesional.
It really sounds what I will need after I have learned the basics etc... But still also for the basics why it sounds perfect... :)
A better name would be Split. I don't know why they chose such a stupid name for such a function.
But sure, you could make such a function. Good training and it's very easy.
I... don't know about that.Quote:
I will try to buy this version. I assume it is called: C++ Proffesional.
Standard costs $280 and Professional costs $730, at least on the prices I could find on Newegg.
Haha... Very expensive.
Ehhh, well... Standard could be had, I guess. It's just the price for a commercial professional product.
ok, thanks I will search for these and see when I will get it :)
I don't see need for MFC here, nor is there any need to buy the Standard or Professional version of VC++. The solution is relatively simple with a stringstream and getline, and the learning curve for those features is probably smaller than for MFC.
As for your attempt, Coding, you are missing the stringstream part. You have to read a line into a string with getline from the file. Then put that string into a stringstream and read in each value with ',' from there.
I have succeded with a code that takes the First and Second value out of: 12/04/2007,2111,35.23,35.23,35.20,35.22,15600,35.22
wich is: 12/04/2007 and 2111
The second step after this is to tell for the code below: If Time == 2111 (wich it is)
it will write the Time to the file. I have tried to do an approch but it doesn´t work.
Perheps I have missed something here.
Code:#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main ()
{
std::string Date;
std::string Time;
ofstream Test;
Test.open ("file2.txt");
ifstream myfile ("file1.txt");
ifstream file0 ("file0.txt");
getline(myfile, Date, ',');// myfile >> tal;
getline(myfile, Time, ',');// myfile >> tal;
If (Time == 2111) // This is what I am trying to do here: If Time is 2111 wich it is in this case.
{
Test << Time <<"\n"; }
return 0;
}
Time is a string variable. If you want to compare it to something you have to compare it to "2111" (with the quotes) because that specifies that it is a string.
BTW, another option is if you know the exact format of your code (date as string,time as int,some other data as double,some other data as double,some other data as double,some other data as double,some other data as int,some other data as double), then you can read in with operator>> and then just read in the delimiters and ignore them.
Getline reads a full line of text - that is why it's called "getline" - so you get that entire line in one, then you get "nothing" in the next stage.
You also can't compre a string with a number. You probably want to split the string, and convert the parts into the right form of number.
--
Mats
>> Getline reads a full line of text
getline reads up to the delimiter specified, so if you specify ',' as the delimiter, it will stop at the first comma. It is called getline because the default delimiter is a newline.
I tried to compile with the "2111" before but this isn´t working.
As you say I want the Date to be a string ("") and the Time to be an Int.
So I have changed to this:
int Time = 0; //std::string Time;
and still using:
If (Time == 2111)
But still for my code above I have no compile success..
When you read in with getline, it reads in a string, so just changing Time to an int won't solve the problem. You have to read in as a string, then convert to an int.
This is what made me suggest the alternate approach of not using getline for the time.
You can use getline for the date, because it is a string and it is difficult to tell where the end of the date is unless you read it character by character.
Then, you can use operator>> to read in the Time. This will stop at the comma because a comma is not part of a number. It will allow you to make Time an int because operator>> works on ints.
Next, you'll have to remember to read in the ',' character. The getline function will read in and remove the comma it finds, but operator>> will not. So just make a dummy char variable and read the comma into that variable with operator>>. Then you will be able to continue and read in the rest of the data.
I follow your description but I am not sure practically how to do. I have experimented with some chars and >> but really dont understand the logic.
I have succeded and understand the logic of how to put Date as string and Time as Int like this:
getline(myfile, Date, ',');
myfile >> Time;
where the ',' is to follow for the Time(int) under this. But from here I dont know how to write the code really for the next value(35.23) in: 12/04/2007,2111,35.23,35.23,35.20,35.22,15600,35.22
I might need some codeassistance perheps...
You're doing fine. After the code you posted above, the input buffer looks like this:Now just add another line using operator>> that reads into a char variable. This will read in the ',' character. Then the input buffer will look like:Code:,35.23,35.23,35.20,35.22,15600,35.22
Then you can use operator>> to read into a double variable to store the 35.23. And so on...Code:35.23,35.23,35.20,35.22,15600,35.22
I beleive this was my problem I am struggling with. I managed to do:
getline(myfile, Date, ',');
myfile >> Time;
myfile >> Comma; //Comma is: char Comma[50];
The output for: Comma in this case is: ,35.23,35.23,35.20,35.22,15600,35.22
If I understand you right here I ´just´ want to put the ',' in the char Comma using
>> and then a New line under this that reads in the: 35.23 using a double varibale.
I am not sure how I will write this. If you have the time I would appreciate how you would continue these 2 lines under:
getline(myfile, Date, ',');
myfile >> Time;
>> //Comma is: char Comma[50];
There's your problem.
A comma is a single character, but you declared the Comma variable as an array of 50 characters. It should just be a char, not a char array.