473,657 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading a textfile

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(zeil e1,255,'\t'))
{ tmp1 = zeile1;
in.getline(zeil e1,255,'\n');
double merke=in.tellg( );
in.seekg(0);
while ( in.getline(zeil e,255,'\t'))
{ tmp = zeile;
in.getline(zeil e2,255,'\n');
tmp2=zeile2;
cout << tmp << " " << tmp2 << endl;
if (tmp==tmp1&&tmp 2!=tmp1)
{
cout << string(zeile2) << endl;
}
}
in.seekg(merke) ;
}
return 0;
}
Many thanks
Bjoern
Jul 22 '05 #1
2 1316

"BjoernJackschi na" <ja*******@hotm ail.com> wrote in message
news:a2******** *************** ***@posting.goo gle.com...
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(zeil e1,255,'\t'))
{ tmp1 = zeile1;
in.getline(zeil e1,255,'\n');
double merke=in.tellg( );
in.seekg(0);
while ( in.getline(zeil e,255,'\t'))
{ tmp = zeile;
in.getline(zeil e2,255,'\n');
tmp2=zeile2;
cout << tmp << " " << tmp2 << endl;
if (tmp==tmp1&&tmp 2!=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(zeil e1, 255, '\t'))
{
tmp1 = zeile1;

john
Jul 22 '05 #2
On 9 May 2004 07:00:53 -0700 in comp.lang.c++, ja*******@hotma il.com
(BjoernJackschi na) wrote,
while ( in.getline(zeil e1,255,'\t'))
{ tmp1 = zeile1;
in.getline(zeil e1,255,'\n');
double merke=in.tellg( );
in.seekg(0);
while ( in.getline(zeil e,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_pass 2, anagram2, '\t'))
{
getline(in_pass 2, zeile2, '\n');

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2151
by: Anders Eriksson | last post by:
Hello! I'm a beginner at PHP and I wonder how I would go about to read a textfile into an array(hash). The textfile looks like this: A/S=age/sex? A/S/L=age/sex/location? AA=alcoholics anonymous
2
1882
by: Suchi | last post by:
Hi all: I want to read a textfile from an ASP program. My program is like this: Set fso = CreateObject("Scripting.FileSystemObject") workFile=http://localhost/Readme.txt) Set textFile = FSObj.OpenTextFile(workFile) lines = split(textFile.ReadAll, VBCrLf) textFile.Close
2
2575
by: BjoernJackschina | last post by:
Hello, I just look for a capability to sort several words in view of alphabet. An example: stop is 'opst' reach is 'aechr' This should read in a new file so that I can look for same letter orders. When I find the same orders it is an anagram. My problem is to sort and to select the textfile. I need dataset for dataset. What is the best way to realize that.
16
4780
by: Roy | last post by:
I use a Access 2K application.I am trying to use Chuck Grimsby(clsReadTextFile.txt)class utility to read a text file and then do a import of the same into my database.The question is how to call these in a button event or any other method. Thanks, Roy
2
2595
by: chris | last post by:
Hi there, I am reading in a textfile which looks like this (there is no new line after the last number) 03 98661881 0407 566453 The code to load the textfile looks like this:
2
3022
by: novacreatura | last post by:
Hi, I have a project that's supposed to create a program for a "Dating Service". The first part of the program is to read a textfile of profiles which include names, age, etc...into a string array, and be able to add,edit,remove to the textfile of profiles during runtime. What would be the most efficient way to do this to make it easiest as possible to make changes to the textfile during time and access elements of the array?
1
1424
by: Justin Fancy | last post by:
Hi everyone, I have a textfile which I need to read and compare dates. The text file summarizes every time I do an update to an internet site. Sample output is as follows: Copying humanresources\compensation files from tcinfotest to tcinfo -----------------------
6
22517
by: =?Utf-8?B?UmljaA==?= | last post by:
'--this code works but only reads text into one column when contains multiple cols Dim ds1x As New DataSet Dim ConStr As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\dir1A\;Extended Properties=""Text;HDR=No;FMT=Delimited\""" Dim conn1x As New OleDb.OleDbConnection(ConStr) Dim dax1 As New OleDbDataAdapter("Select * from testabc1x.txt", conn1x)
3
2897
by: Arn | last post by:
I just started to learn C++ and have some problem when I read a hole line from a textfile. I would be grateful if anyone can tell me what is wrong with my code. I'm using Borland Developer Studio 2006. //------------------------------------------------------------------------------ // getFile //------------------------------------------------------------------------------ // Read from a textfile // Input: nameList (string) , FILENAME...
1
2809
by: asedt | last post by:
With my Excel macro and two text files I want to create a new textfile containing the first textfile then text from the sheet and then the second textfile. My problem is that i don't know how to append the second textfile. Sub mysub() Dim TempString As String 'Open the first file Dim fileA As Integer
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8603
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.