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.
decornezAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
If the sample file is small, why not post it here?
decornezAuthor Commented:
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.
nietodCommented:
Just copy it to the comment area.  Or you can e-mail me at nietod@theshop.net.
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

arnondCommented:
hi, my e-amil is :
sarnon@techst02.technion.ac.il
nietodCommented:
here it is.  Let me know if you have questions.

int main()
{

   fstream InpFil("C:\\conapp\\Input.txt",ios::in);
   string CurLin;

   getline(InpFil,CurLin); // Get and ignore date line.
   getline(InpFil,CurLin); // Get and ignore run-by line.
   getline(InpFil,CurLin); // Get and ignore blank line.
   getline(InpFil,CurLin); // Get and ignore blank line.
   getline(InpFil,CurLin); // Get and ignore "yield" line.
   getline(InpFil,CurLin); // Get and ignore blank line.
   getline(InpFil,CurLin); // Get and ignore FX line.
   getline(InpFil,CurLin); // Get and ignore blank line.

   while(!InpFil.eof()) // While not at the end of the file.
   {
      int FirstInt;
      char MOrYLetter;
      string CashOrSwap;
      double From;
      double To;
      string Hyphen;

      InpFil >> FirstInt >> MOrYLetter >> CashOrSwap >> From >> Hyphen >> To;
      getline(InpFil,CurLin); // Get current line.
   }

 return 0;
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
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.
arnondCommented:
neitod, you grabed this one right from under my nose. I was this close to answering. ;-).
decornezAuthor Commented:
Nietod, Wow that was fast !

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

A.
nietodCommented:
arnond, you should post it anyways.  Your answer might be better and might get accepted in the end.
arnondCommented:
na, it uses the same principles as your answer. it's a simple parsing method.
thanks anyway.
decornezAuthor Commented:
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.
nietodCommented:
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?
decornezAuthor Commented:
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.

nietodCommented:
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.
decornezAuthor Commented:
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.
nietodCommented:
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;
decornezAuthor Commented:
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.
nietodCommented:
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.
decornezAuthor Commented:
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.
nietodCommented:
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.
nietodCommented:
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

decornezAuthor Commented:
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.

nietodCommented:
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?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.