Link to home
Start Free TrialLog in
Avatar of decornez
decornez

asked on

extraction from text file

I have a problem slicing out information out of a text file. I have previously asked some questions on this site to try to sort it out but it has not helped me so far.

I am trying to read numbers from various lines to later dump them into a dialog box.

I am looking for code which is flexible and can cater for lines of variable lengths and get data of variable length as well.

Therefore, I will e-mail a sample file (very small) to those  who want to get a crack at it. The first one who provides me with a satisfactory answer will get 185 pts which I will gladly dish out as this problem has been getting on my t..s for a while.

Thanks a lot.
A.
Avatar of ozo
ozo
Flag of United States of America image

If the sample file is small, why not post it here?
Avatar of decornez
decornez

ASKER

ozo,

You mean I just copy the contents in the comment area ?

Or is there a way to post my file on this site?
How do I do that ?

Let me know.

Thanks,
A.
Just copy it to the comment area.  Or you can e-mail me at nietod@theshop.net.
hi, my e-amil is :
sarnon@techst02.technion.ac.il
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I ignored the first few lines.  I assume there is nothing in particular you want to extract from them.  I assume the ppoint is to read the table at the end.  If not, let me know.
neitod, you grabed this one right from under my nose. I was this close to answering. ;-).
Nietod, Wow that was fast !

let me try it out and I'll get back to you.

A.
arnond, you should post it anyways.  Your answer might be better and might get accepted in the end.
na, it uses the same principles as your answer. it's a simple parsing method.
thanks anyway.
I have used your code, but I seem to get some error messages.

1. I added the #include <fstream> statement at the top of the file to get rid of most errors
2. My program did not recognize "string" so I replaced it by CString

Yet I still have a couple errors (down from 39 when compiling it first) for which I will need further help. Below is the related text:

C:\TADS\TADS\DPRMYLDHKDdlg.cpp(378) : error C2065: 'getline' : undeclared identifier
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(396) : error C2678: binary '>>' : no operator defined which takes a left-hand operand of type 'class istream' (or there is no acceptable conversion)
Error executing cl.exe.

Any clues ?

Thanks,
A.
The code used the STL string class defined in the <string> header.  It can be changed to use the MFC CString class, but I wouldn't do that unless you plan to use MFC for other reasons as well.  Do you?
Nietod,

I do not mind using either method as long as it works.
The <string> header is already in my file.
When I enter your exact code here the error messages I get:

C:\TADS\TADS\DPRMYLDHKDdlg.cpp(376) : error C2065: 'string' : undeclared identifier
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(376) : error C2146: syntax error : missing ';' before identifier 'CurLin'
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(376) : error C2065: 'CurLin' : undeclared identifier
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(378) : error C2065: 'getline' : undeclared identifier
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(391) : error C2146: syntax error : missing ';' before identifier 'CashOrSwap'
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(391) : error C2065: 'CashOrSwap' : undeclared identifier
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(394) : error C2146: syntax error : missing ';' before identifier 'Hyphen'
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(394) : error C2065: 'Hyphen' : undeclared identifier
Error executing cl.exe.

Could help me to find out why I get these messages.

by the way, below is the list of the header files included in my code already:

#include      <iostream.h>
#include      <stdlib.h>
#include      <string.h>
#include      <stdio.h>
#include      <afx.h> //for CString
#include      <fstream.h>

hope this helps you to pinpoint what is happening.

Thanks,
A.

the problem is namespaces.  The string class is defined in the STL namespace called "std".  You can prefix the class name with "std" everywhere, like

  string std::CurLin;

instead of

  string CurLin;

But I don't like that.  (It gets too messy for me).  I prefer to use a "using" declaration so that the string class and all of STL are used in my namespace, this I would place the line

using namespace std;

after the include files are included.  That should fix it.  Let me know if not.
I have tried your suggestion and added "using namespace std; ".

I now get additional errors:

C:\TADS\TADS\DPRMYLDHKDdlg.cpp(376) : error C2653: 'std' : is not a class or namespace name
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(376) : error C2065: 'string' : undeclared identifier
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(376) : error C2146: syntax error : missing ';' before identifier 'CurLin'
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(376) : error C2065: 'CurLin' : undeclared identifier
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(378) : error C2065: 'getline' : undeclared identifier
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(391) : error C2653: 'std' : is not a class or namespace name
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(391) : error C2146: syntax error : missing ';' before identifier 'CashOrSwap'
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(391) : error C2065: 'CashOrSwap' : undeclared identifier
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(394) : error C2653: 'std' : is not a class or namespace name
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(394) : error C2146: syntax error : missing ';' before identifier 'Hyphen'
C:\TADS\TADS\DPRMYLDHKDdlg.cpp(394) : error C2065: 'Hyphen' : undeclared identifier
Error executing cl.exe.

It looks like I am missing a header file or stg as I do not understand why getline is not recognized !

Do you have any other suggestions which I can try ?

Thanks,
A.
Sorry.  I'm not really familair with namespaces.  I put the line in my "generic" app months ago and never look back at it  I gave it to you from memory and my memory wasn't good.  It  should read

using namespace::std;
I now get 64 errors.

Here is a sample:

C:\TADS\TADS\DPRMYLDHKDdlg.cpp(381) : error C2780: 'class std::basic_istream<`template-parameter-1',`template-parameter-2'> &__cdecl std::getline(class std::basic_istream<`template-parameter-1',`template-parameter-2'> &,class std::basic_string<`temp
late-parameter-1',`template-parameter-2',`template-parameter-3'> &,const template-parameter-1)' : expects 3 arguments - 2 provided

Looks like getline is expecting a third argument.

Any clue ?

Thanks,
A.
There are supposed to be 2 versions of std::getline().  the one takes a 3rd argument that indicates the end of line character which the other assumes in \n.  I'm surprised you are havign this problem.  What compiler are you using?  is it an old version?

Anyways, you can just specify '\n' as the 3rd parameter.
This is what I get now:

C:\TADS\TADS\DPRMYLDHKDdlg.cpp(381) : error C2784: 'class std::basic_istream<`template-parameter-1',`template-parameter-2'> &__cdecl std::getline(class std::basic_istream<`template-parameter-1',`template-parameter-2'> &,class std::basic_string<`temp
late-parameter-1',`template-parameter-2',`template-parameter-3'> &,const template-parameter-1)' : could not deduce template argument for 'class std::basic_istream<`template-parameter-1',`template-parameter-2'> &' from 'class fstream'

The compiler I am using is the one used by default by  MS VC++ 5.0

Any idea what's up ?

Thanks,
A.
You've got something weird somewhere.  The code I created was compiled under MS VC 5.

Can you e-mail me your file?  My address is nietod@theshop.net.
I can't seem to e-mail you a reply for some reason, so I'm pasting it here.
*************************************************************************
Arnaud

I had to comment out 99% of your code as I didn't have the include files for the clases, but I still got the problem you are getting and was able to fix it, so the following should work.

Again it is namspace problems.  When you include some of the C++ headers with .h instead of the non.h version it fools with the namepaces.  This causes problems if templates in one header use templates in another header and they have different namepace "properties".   To fix this I removed the .h from the iostream, fstream, and istream files.  Then I moved the using declaration to right after the header files are included.

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <afx.h> //for CString
#include <fstream>
#include <istream>
using namespace::std;


good luck,

todd niec

Todd,

Thank you for your sustained follow-up and help on this question.
It compiles just nice now.

Maybe one last small question: How do I allocate let's say the value of the "From" variable inside the "main" to a dialog box variable outside the main (within the same file).

Thanks,
Arnaud.

I don't know what "From" is or in what sense it has to be provided to the dialog box variable.
But if the dialog box will hold a copy of from as a data, member and can be passed as a prameter to the dialog box's constructor.  If a single member function needs to get the "from" data and it is not stored in the dialog object, then you may need to make the "from" data global, not local to main.

Helps?