Hello all,

I am trying to read in the text file but it seems my code cannot read the text file which has tab space

eg

Mine only reads

Sydney NewYork 1300

but

It should read

Sydney New York 1300

( and the space between city name or number can be tab spaced )

can ne1 plz help me???

I'll attach my code with text file..

Can't you read each line in and then use the tab as a delimiter?

Don't use [B]!inf.eof()[/B] as a condition in a loop for reading a whole file:

void get_graph ( string const &filename, NodeMap &node_map )
{
   ifstream inf ( filename.c_str() );
   string from, to;
   double weight;
   while ( [B]!inf.eof()[/B] )
   {
      inf >> from >> to >> weight;
      if ( inf.good() )
      {
         Node *Target = node_map.find_in_nodemap ( to );
         Node *Source = node_map.find_in_nodemap ( from );
         Edge *connector = new Edge ( weight, Target );
         Source->neighbors.push_back ( connector );
      }
   }
}

You could begin by changing your code to:

void get_graph ( string const &filename, NodeMap &node_map )
{
   ifstream inf ( filename.c_str() );
   string from, to;
   double weight;
   [B]while ([/B] [B]inf >> from >> to >> weight[/B] [B])[/B]
   {
         Node *Target = node_map.find_in_nodemap ( to );
         Node *Source = node_map.find_in_nodemap ( from );
         Edge *connector = new Edge ( weight, Target );
         Source->neighbors.push_back ( connector );
   }
}

By the way: where do you close that file ?

Well, I did a small test to see what your problem is, but with me it works fine, this program reads successfully from a file which contains tabs:

The code I used:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	string tmp;
	ifstream infile("t.txt");
	while( infile >> tmp )
		cout << tmp << " ";
	infile.close();
	return 0;
}


t.txt file contents:

Hello			beautiful world   1230 		!!!!

The output I'm getting:

Hello beautiful world 1230 !!!!

Just try and adapt it to your program.

commented: noob error. -4

I disagree with the above since the OP wants to separate the lines just by tabs. For example New York, should be considered as one but tux4life's code will treat it as two separate entities.

>Can't you read each line in and then use the tab as a delimiter?
It is possible, if you use strtok for example, you could use a TAB as delimiter as follows: strtok(tmp, "[B]\t[/B]"); (assume that tmp is a character string)

Why would you want to mix up c functions with c++?

Why would you want to mix up c functions with c++?

Remember that it's still a part of C++, I only gave him an advice, do you know something better, then just tell it OK?

> Remember that it's still a part of C++

I'll agree with you on this, but where you can it makes sense to use c++ functions.

Take a look at this excellent snippet and call the Split function as follows:
(I assume you already read a line from the file) Split( [I]string_from_file[/I], [B]"\t"[/B], words ); Remember that string_from_file has to be a c-string :)

> Remember that it's still a part of C++

I'll agree with you on this, but where you can it makes sense to use c++ functions.

Use what's easiest, while keeping an eye on performance and code consistency if possible. If a C solution is easier to program/understand than a C++ solution, why bother with a C++ solution (which would most probably be slower anyway).

For example, dont be afraid to use a non-std::string method in this particular case, but it's things like mixing printf and cout which cause problems.

>By the way: where do you close that file ?
File stream destructor closes that file. That's OK.

I might choose to read each line and then parse tab-delimited fields like this.

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

int main()
{
   std::ifstream file("verging.txt");
   std::string line;
   while ( getline(file, line) )
   {
      std::cout << line << "\n"; // Display the whole input line.

      // Parse fields in 'line' delimited by tabs.
      std::istringstream iss(line);
      std::string field;
      while ( getline(iss, field, '\t') )
      {
         std::cout << " " << field << "\n";
      }
   }
   return 0;
}

/* partial output
Glasgow	New York	884
 Glasgow
 New York
 884
New York	Edinburgh	761
 New York
 Edinburgh
 761
 */
commented: much better than the other crap in this thread +20