reading a textfile

  • Thread starter BjoernJackschina
  • Start date
B

BjoernJackschina

Hello,
many thanks for the solution of my last problem but I think I will use
my own simple solution. I want to find anagrams. The file "d.txt"
looks like this:
opts stop
fnu fun
opts post
opts tops
emos some
So I compare every word order but in my programm I can only read the
first dataset and then it is stoped. What is the mistake?

#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{ ifstream in("d.txt");
char zeile[256];char zeile1[256];char zeile2[256];
string tmp = "";string tmp1= "";string tmp2= "";
in.seekg(0);
while ( in.getline(zeile1,255,'\t'))
{ tmp1 = zeile1;
in.getline(zeile1,255,'\n');
double merke=in.tellg();
in.seekg(0);
while ( in.getline(zeile,255,'\t'))
{ tmp = zeile;
in.getline(zeile2,255,'\n');
tmp2=zeile2;
cout << tmp << " " << tmp2 << endl;
if (tmp==tmp1&&tmp2!=tmp1)
{
cout << string(zeile2) << endl;
}
}
in.seekg(merke);
}
return 0;
}
Many thanks
Bjoern
 
J

John Harrison

BjoernJackschina said:
Hello,
many thanks for the solution of my last problem but I think I will use
my own simple solution.

I don't know what solution you were offered but yours looks extremely
inefficient to me, but maybe you aren't concerned about that.
I want to find anagrams. The file "d.txt"
looks like this:
opts stop
fnu fun
opts post
opts tops
emos some
So I compare every word order but in my programm I can only read the
first dataset and then it is stoped. What is the mistake?

You are forgetting to clear the stream error state after you have reached
the end of file. See correction below.
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{ ifstream in("d.txt");
char zeile[256];char zeile1[256];char zeile2[256];
string tmp = "";string tmp1= "";string tmp2= "";
in.seekg(0);
while ( in.getline(zeile1,255,'\t'))
{ tmp1 = zeile1;
in.getline(zeile1,255,'\n');
double merke=in.tellg();
in.seekg(0);
while ( in.getline(zeile,255,'\t'))
{ tmp = zeile;
in.getline(zeile2,255,'\n');
tmp2=zeile2;
cout << tmp << " " << tmp2 << endl;
if (tmp==tmp1&&tmp2!=tmp1)
{
cout << string(zeile2) << endl;
}
}
in.clear();

in.seekg(merke);
}
return 0;
}

You could make your code a lot easier to read if you read directly into a
string instead of using char arrays.

while ( getline(in, tmp1, '\t'))
{

instead of

while ( in.getline(zeile1, 255, '\t'))
{
tmp1 = zeile1;

john
 
D

David Harmon

On 9 May 2004 07:00:53 -0700 in comp.lang.c++, (e-mail address removed)
(BjoernJackschina) wrote,
while ( in.getline(zeile1,255,'\t'))
{ tmp1 = zeile1;
in.getline(zeile1,255,'\n');
double merke=in.tellg();
in.seekg(0);
while ( in.getline(zeile,255,'\t'))
{ tmp = zeile;

I agree with all of John's comments, and would like to add one more
thing:

All of this seeking around in the file is way too susceptible to
mistakes and will soon make you crazy. It would be easier and simpler
to leave the "in" stream positioned where it is, and create a second
ifstream to the file to read it for the inner pass of the loop.

z.B. (with all variables std::string)

while ( getline(in, anagram1, '\t'))
{
getline(in, zeile1, '\n');

ifstream in_pass2("d.txt");
while ( getline(in_pass2, anagram2, '\t'))
{
getline(in_pass2, zeile2, '\n');
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top