Solved

extraction from text file

Posted on 1998-10-25
23
228 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:decornez
  • 11
  • 8
  • 3
  • +1
23 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1176005
If the sample file is small, why not post it here?
0
 

Author Comment

by:decornez
ID: 1176006
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176007
Just copy it to the comment area.  Or you can e-mail me at nietod@theshop.net.
0
 
LVL 3

Expert Comment

by:arnond
ID: 1176008
hi, my e-amil is :
sarnon@techst02.technion.ac.il
0
 
LVL 22

Accepted Solution

by:
nietod earned 180 total points
ID: 1176009
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;
}

0
 
LVL 22

Expert Comment

by:nietod
ID: 1176010
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.
0
 
LVL 3

Expert Comment

by:arnond
ID: 1176011
neitod, you grabed this one right from under my nose. I was this close to answering. ;-).
0
 

Author Comment

by:decornez
ID: 1176012
Nietod, Wow that was fast !

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

A.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176013
arnond, you should post it anyways.  Your answer might be better and might get accepted in the end.
0
 
LVL 3

Expert Comment

by:arnond
ID: 1176014
na, it uses the same principles as your answer. it's a simple parsing method.
thanks anyway.
0
 

Author Comment

by:decornez
ID: 1176015
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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 22

Expert Comment

by:nietod
ID: 1176016
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?
0
 

Author Comment

by:decornez
ID: 1176017
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.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1176018
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.
0
 

Author Comment

by:decornez
ID: 1176019
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176020
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;
0
 

Author Comment

by:decornez
ID: 1176021
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176022
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.
0
 

Author Comment

by:decornez
ID: 1176023
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176024
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176025
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

0
 

Author Comment

by:decornez
ID: 1176026
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.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1176027
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?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now