I wonder something about this code that read(ifstream) a .txt file.
In my file I have these 2 lines:

Hello,1000,Yes,walker
How,2000,No,walker

So with my code I use getline with a delimiter to put these 4 different strings/int to variables.
The code works fine for the 2 first, wich is "Hello" and "1000".
The problem occurs when I am extracting "Yes" wich is a std::string.

I have used a MessageBox below to display what happens.
What happens is that when extracting "Yes" to std::string two. I get this output:

"Yes,walker"

Why does this happening though I use ',' as delimiter ? It seems that this is happening when a std::string occurs on the line.
What is possible to do to extract this line correctly (string, int, string, string)

ifstream File("C:\\Read.txt");
							 
	std::string one;
	 int Number = 0;
	 std::string two;
	 std::string three;
	 char Comma;


	 while( getline(File, one, ',') )
	 {
				 

		 File >> Number;
		 File >> Comma;
		 File >> two;
		 File >> Comma;
		 File >> three;
		 File.get();
			 
			 
	String^ ShowThis = gcnew String(two.c_str());
	MessageBox::Show(ShowThis);
	 }

Each istream method/operator has it's quirks. getline() discards the delimiting char once found, unlike >> or gets(). Therefore, the call getline() within the conditional places Hello in one and discards the delimiting comma. The first call to >> places 1000 in Number but stops at the second comma since commas aren't part of ints in C++. The second call to >> reads the second comma into Comma. The third call to >> reads the rest of the line into two since the first terminating whitespace char in the line is the newline char at the end of the line and, in the first analysis at least, std strings aren't limited by size. The next call to >>, the fourth, will place the H of How into Comma overwriting the comma placed there in the second call to >>. The last call to >>, the fifth, will place the remainder of the second line in three. Then the call to get() will read the newline char remaining to be read from the file (assuming there is one) and proceed to the next line.

At least that's how I analyze. It's easy enough to tell if I'm correct. Just output the value of each variable or review in your debugger if you prefer.

here is an example that uses stringstream as helper.
See this thread for explaination of line 27.

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <limits>
using namespace std;

int main()
{
	std::string one;
    int Number = 0;
    std::string line;
    std::string two;
    std::string three;

    ifstream File("..\\TextFile1.txt");
    if(!File.is_open())
    {
        cout << "Can't open the file\n";
        return 1;
    }
    while( getline(File, line) )
    {
        stringstream stream(line);
        getline(stream,one,',');
        stream >> Number;
        stream.ignore ( std::numeric_limits<std::streamsize>::max(), ',' );
        getline(stream,two,',');
        getline(stream,three,',');
    }
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.