Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reading data from text file

Reply
Thread Tools

reading data from text file

 
 
Santah
Guest
Posts: n/a
 
      07-28-2003
hi
I'm new to C++
and I'm currently working on Visual C++ 6.0

I'm trying to open a text file, and read some data from it

part of the text file looks like this:

--------

Stoichiometry C3H7NO
Framework group C1[X(C3H7NO)]
Deg. of freedom 30
Full point group C1 NOp 1
Largest Abelian subgroup C1 NOp 1
Largest concise Abelian subgroup C1 NOp 1
Standard orientation:
---------------------------------------------------------------------
Center Atomic Atomic Coordinates (Angstroms)
Number Number Type X Y Z
---------------------------------------------------------------------
1 7 0 -0.471842 1.385033 -0.179367
2 6 0 -0.441998 0.034984 0.352626
3 1 0 -0.532368 0.028578 1.492181
4 6 0 0.817945 -0.768835 0.128492
5 8 0 1.897598 -0.056037 -0.129721
6 6 0 -1.600140 -0.811575 -0.187269
7 1 0 -1.521476 -1.844231 0.228333
8 1 0 -2.577732 -0.368082 0.117451
9 1 0 -1.563751 -0.866827 -1.300932
10 1 0 0.330252 1.899884 0.122288
11 1 0 -1.306000 1.857479 0.102977
12 1 0 2.638344 -0.681170 -0.232052
---------------------------------------------------------------------
Rotational constants (GHZ): 8.3231162 3.9152127
2.8710745
Isotopes: N-14,C-12,H-1,C-12,O-16,C-12,H-1,H-1,H-1,H-1,H-1,H-1
Standard basis: VSTO-3G (5D, 7F)
There are 27 symmetry adapted basis functions of A symmetry.
Crude estimate of integral set expansion from redundant
integrals=1.000.

--------


what I need, is the values:

1 7 0 -0.471842 1.385033 -0.179367
2 6 0 -0.441998 0.034984 0.352626
3 1 0 -0.532368 0.028578 1.492181
4 6 0 0.817945 -0.768835 0.128492
5 8 0 1.897598 -0.056037 -0.129721
6 6 0 -1.600140 -0.811575 -0.187269
7 1 0 -1.521476 -1.844231 0.228333
8 1 0 -2.577732 -0.368082 0.117451
9 1 0 -1.563751 -0.866827 -1.300932
10 1 0 0.330252 1.899884 0.122288
11 1 0 -1.306000 1.857479 0.102977
12 1 0 2.638344 -0.681170 -0.232052


but I don't know how to fetch them

here's what I've done so far .... (I know that it's not good nor
working )

CFile file1("test.txt", CFile::modeRead );
DWORD dwLen = file1.GetLength();

char *pbuf = new char[dwLen] ;

ffile1.ReadHuge( pbuf, dwLen );

CString sText(pbuf);

delete []pbuf;
// sText.Find("Standard orientation:");


so I have the whole file in a string, and now I need only to get the
needed values from the string but I don't know how

any help is welcomed )
 
Reply With Quote
 
 
 
 
zot
Guest
Posts: n/a
 
      07-28-2003
Has the visual c++
a class to find regular expression similar to QT's regexp?
If so you can use it.
If not:
Another point: probably it's better to fetch the file one line at a time.
You can use the standard funtions : readline.
Now you can write a function that fetches
a floating number startinng from a char* (the ponter to the string),
and stores it in a double var.
This function should return error if it is impossible to parse the number.
Suppose you call it f(char*,double).
If it is impossible to fetch a number of int equal to the
number of columns in your table then you are parsing a bad line ( a line
extraneous to the table):

Now you can write a loop:


for(int i=0; i < NUMBER OF COL; i++)
if ( f(string,number)== error)
continue on next line;

Hope this help.
I have assumed you can write such a function, but if you want I can write it
next time.
Sorry but I haven't so much time at the moment....



"Santah" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed) om...
> hi
> I'm new to C++
> and I'm currently working on Visual C++ 6.0
>
> I'm trying to open a text file, and read some data from it
>
> part of the text file looks like this:
>
> --------
>
> Stoichiometry C3H7NO
> Framework group C1[X(C3H7NO)]
> Deg. of freedom 30
> Full point group C1 NOp 1
> Largest Abelian subgroup C1 NOp 1
> Largest concise Abelian subgroup C1 NOp 1
> Standard orientation:
> ---------------------------------------------------------------------
> Center Atomic Atomic Coordinates (Angstroms)
> Number Number Type X Y Z
> ---------------------------------------------------------------------
> 1 7 0 -0.471842 1.385033 -0.179367
> 2 6 0 -0.441998 0.034984 0.352626
> 3 1 0 -0.532368 0.028578 1.492181
> 4 6 0 0.817945 -0.768835 0.128492
> 5 8 0 1.897598 -0.056037 -0.129721
> 6 6 0 -1.600140 -0.811575 -0.187269
> 7 1 0 -1.521476 -1.844231 0.228333
> 8 1 0 -2.577732 -0.368082 0.117451
> 9 1 0 -1.563751 -0.866827 -1.300932
> 10 1 0 0.330252 1.899884 0.122288
> 11 1 0 -1.306000 1.857479 0.102977
> 12 1 0 2.638344 -0.681170 -0.232052
> ---------------------------------------------------------------------
> Rotational constants (GHZ): 8.3231162 3.9152127
> 2.8710745
> Isotopes: N-14,C-12,H-1,C-12,O-16,C-12,H-1,H-1,H-1,H-1,H-1,H-1
> Standard basis: VSTO-3G (5D, 7F)
> There are 27 symmetry adapted basis functions of A symmetry.
> Crude estimate of integral set expansion from redundant
> integrals=1.000.
>
> --------
>
>
> what I need, is the values:
>
> 1 7 0 -0.471842 1.385033 -0.179367
> 2 6 0 -0.441998 0.034984 0.352626
> 3 1 0 -0.532368 0.028578 1.492181
> 4 6 0 0.817945 -0.768835 0.128492
> 5 8 0 1.897598 -0.056037 -0.129721
> 6 6 0 -1.600140 -0.811575 -0.187269
> 7 1 0 -1.521476 -1.844231 0.228333
> 8 1 0 -2.577732 -0.368082 0.117451
> 9 1 0 -1.563751 -0.866827 -1.300932
> 10 1 0 0.330252 1.899884 0.122288
> 11 1 0 -1.306000 1.857479 0.102977
> 12 1 0 2.638344 -0.681170 -0.232052
>
>
> but I don't know how to fetch them
>
> here's what I've done so far .... (I know that it's not good nor
> working )
>
> CFile file1("test.txt", CFile::modeRead );
> DWORD dwLen = file1.GetLength();
>
> char *pbuf = new char[dwLen] ;
>
> ffile1.ReadHuge( pbuf, dwLen );
>
> CString sText(pbuf);
>
> delete []pbuf;
> // sText.Find("Standard orientation:");
>
>
> so I have the whole file in a string, and now I need only to get the
> needed values from the string but I don't know how
>
> any help is welcomed )



 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      07-28-2003
zot wrote:
1. Don't top-post.
> Has the visual c++
> a class to find regular expression similar to QT's regexp?
> If so you can use it.
> If not:
> Another point: probably it's better to fetch the file one line at a time.
> You can use the standard funtions : readline.

readline is not a standard function. If it is, please quote where
in the C++ specification that you found it.
There is a function, std::getline(), which is part of the Standard
Template Library.


> Now you can write a function that fetches
> a floating number startinng from a char* (the ponter to the string),
> and stores it in a double var.

One could use the stringstream functions too.
Or even sscanf().

> This function should return error if it is impossible to parse the number.
> Suppose you call it f(char*,double).
> If it is impossible to fetch a number of int equal to the
> number of columns in your table then you are parsing a bad line ( a line
> extraneous to the table):

But why write a function when one is available?

>
> Now you can write a loop:
>
>
> for(int i=0; i < NUMBER OF COL; i++)
> if ( f(string,number)== error)
> continue on next line;
>
> Hope this help.
> I have assumed you can write such a function, but if you want I can write it
> next time.
> Sorry but I haven't so much time at the moment....
>
>
>
> "Santah" <(E-Mail Removed)> ha scritto nel messaggio
> news:(E-Mail Removed) om...
>


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
Santah
Guest
Posts: n/a
 
      07-29-2003
Thomas Matthews <(E-Mail Removed)> wrote in message news:<i%bVa.13438$(E-Mail Removed) odigy.com>...
> Santah wrote:
>
> > hi
> > I'm new to C++
> > and I'm currently working on Visual C++ 6.0
> >
> > I'm trying to open a text file, and read some data from it
> >
> > part of the text file looks like this:
> >
> > --------
> >
> > Stoichiometry C3H7NO
> > Framework group C1[X(C3H7NO)]
> > Deg. of freedom 30
> > Full point group C1 NOp 1
> > Largest Abelian subgroup C1 NOp 1
> > Largest concise Abelian subgroup C1 NOp 1
> > Standard orientation:
> > ---------------------------------------------------------------------
> > Center Atomic Atomic Coordinates (Angstroms)
> > Number Number Type X Y Z
> > ---------------------------------------------------------------------
> > 1 7 0 -0.471842 1.385033 -0.179367
> > 2 6 0 -0.441998 0.034984 0.352626
> > 3 1 0 -0.532368 0.028578 1.492181
> > 4 6 0 0.817945 -0.768835 0.128492
> > 5 8 0 1.897598 -0.056037 -0.129721
> > 6 6 0 -1.600140 -0.811575 -0.187269
> > 7 1 0 -1.521476 -1.844231 0.228333
> > 8 1 0 -2.577732 -0.368082 0.117451
> > 9 1 0 -1.563751 -0.866827 -1.300932
> > 10 1 0 0.330252 1.899884 0.122288
> > 11 1 0 -1.306000 1.857479 0.102977
> > 12 1 0 2.638344 -0.681170 -0.232052
> > ---------------------------------------------------------------------
> > Rotational constants (GHZ): 8.3231162 3.9152127
> > 2.8710745
> > Isotopes: N-14,C-12,H-1,C-12,O-16,C-12,H-1,H-1,H-1,H-1,H-1,H-1
> > Standard basis: VSTO-3G (5D, 7F)
> > There are 27 symmetry adapted basis functions of A symmetry.
> > Crude estimate of integral set expansion from redundant
> > integrals=1.000.
> >
> > --------
> >
> >
> > what I need, is the values:
> >
> > 1 7 0 -0.471842 1.385033 -0.179367
> > 2 6 0 -0.441998 0.034984 0.352626
> > 3 1 0 -0.532368 0.028578 1.492181
> > 4 6 0 0.817945 -0.768835 0.128492
> > 5 8 0 1.897598 -0.056037 -0.129721
> > 6 6 0 -1.600140 -0.811575 -0.187269
> > 7 1 0 -1.521476 -1.844231 0.228333
> > 8 1 0 -2.577732 -0.368082 0.117451
> > 9 1 0 -1.563751 -0.866827 -1.300932
> > 10 1 0 0.330252 1.899884 0.122288
> > 11 1 0 -1.306000 1.857479 0.102977
> > 12 1 0 2.638344 -0.681170 -0.232052
> >
> >
> > but I don't know how to fetch them
> >
> > here's what I've done so far .... (I know that it's not good nor
> > working )
> >
> > CFile file1("test.txt", CFile::modeRead );
> > DWORD dwLen = file1.GetLength();
> >
> > char *pbuf = new char[dwLen] ;
> >
> > ffile1.ReadHuge( pbuf, dwLen );
> >
> > CString sText(pbuf);
> >
> > delete []pbuf;
> > // sText.Find("Standard orientation:");
> >
> >
> > so I have the whole file in a string, and now I need only to get the
> > needed values from the string but I don't know how
> >
> > any help is welcomed )

>
> I'm in a good mood, so here is a foundation.
>
> #include <fstream>
> #include <string>
> #include <cstdlib>
> using std::ifstream;
> using std::string;
> using std::getline;
> using std::cerr;
>
> int main(void)
> {
> ifstream inp_data("test.txt"); // ifstream defaults to text files
> if (!inp_data)
> {
> cerr << "Error opening 'test.txt'\n";
> return EXIT_FAILURE;
> }
>
> //------------------------------------------------------------
> // Skip past all the junk.
> //------------------------------------------------------------
> string text_line;
> unsigned int dashed_line_count = 2;
> while (getline(inp_data, text_line, '\n'))
> {
> if ((text_line.length() > 1)
> && (text_line[0] == '-'))
> {
> --dashed_line_count;
> if (dashed_line_count == 0)
> break;
> }
> }
>
> if (!inp_data || (dashed_line_count > 0))
> {
> cerr << "Data table not found.\n";
> return EXIT_FAILURE;
> }
>
> //------------------------------------------------------------
> // At this point, the next text line should be the
> // first row of data.
> //------------------------------------------------------------
> unsigned int center_number;
> unsigned int atomic_number;
> unsigned int atomic_type;
> double x_ord;
> double y_ord;
> double z_ord;
> while (inp_data >> center_number)
> {
> inp_data >> atomic_number >> atomic_type
> >> x_ord >> y_ord >> z_ord;

> Process_The_Data(center_number, atomic_number, atomic_type,
> x_ord, y_ord, z_ord);
> }
> return EXIT_SUCCESS;
> }
>
>
> Look Ma, no $#@!&*# MFC library required.
> And its portable too.
> {The code above has not been compiled nor tested.}
>
> --
> Thomas Matthews
>
> C++ newsgroup welcome message:
> http://www.slack.net/~shiva/welcome.txt
> C++ Faq: http://www.parashift.com/c++-faq-lite
> C Faq: http://www.eskimo.com/~scs/c-faq/top.html
> alt.comp.lang.learn.c-c++ faq:
> http://www.raos.demon.uk/acllc-c++/faq.html
> Other sites:
> http://www.josuttis.com -- C++ STL Library book




thanks that you took the time to try to help me, but....
I can't make your source work ...
because:
1. I haven't seen anywhere in your source the string "Standard
Orientation", and that is the keystring wich I should use to locate
the table, because the text file is not with a fixed rows, and there
can be many things above that table, but when there is the "Standard
Orientation" string, I know that the table follows.....
2. The way you added values to those variables (atomic_number etc.)
are they arrays or single values? because I need all the values from
the table stored....

sorry if I missunderstood something, I'm not that good in programming,
and thanks again for taking the time to read this bye
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-29-2003


Karl Heinz Buchegger wrote:
>

[snip]
> then one could use (referring to Mathews code again)


Apologies to Thomas.
When writing the reply I was under the impression that your
first name is Mathews, while in fact Matthews is your surname
and is spelled differently.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      07-29-2003
Santah wrote:
> Thomas Matthews <(E-Mail Removed)> wrote in message news:<i%bVa.13438$(E-Mail Removed) odigy.com>...
>
>>Santah wrote:
>>
>>
>>>hi
>>>I'm new to C++
>>>and I'm currently working on Visual C++ 6.0
>>>
>>>I'm trying to open a text file, and read some data from it
>>>
>>>part of the text file looks like this:
>>>
>>>--------
>>>
>>>Stoichiometry C3H7NO
>>> Framework group C1[X(C3H7NO)]
>>> Deg. of freedom 30
>>> Full point group C1 NOp 1
>>> Largest Abelian subgroup C1 NOp 1
>>> Largest concise Abelian subgroup C1 NOp 1
>>> Standard orientation:
>>> ---------------------------------------------------------------------
>>> Center Atomic Atomic Coordinates (Angstroms)
>>> Number Number Type X Y Z
>>> ---------------------------------------------------------------------
>>> 1 7 0 -0.471842 1.385033 -0.179367
>>> 2 6 0 -0.441998 0.034984 0.352626
>>> 3 1 0 -0.532368 0.028578 1.492181
>>> 4 6 0 0.817945 -0.768835 0.128492
>>> 5 8 0 1.897598 -0.056037 -0.129721
>>> 6 6 0 -1.600140 -0.811575 -0.187269
>>> 7 1 0 -1.521476 -1.844231 0.228333
>>> 8 1 0 -2.577732 -0.368082 0.117451
>>> 9 1 0 -1.563751 -0.866827 -1.300932
>>> 10 1 0 0.330252 1.899884 0.122288
>>> 11 1 0 -1.306000 1.857479 0.102977
>>> 12 1 0 2.638344 -0.681170 -0.232052
>>> ---------------------------------------------------------------------
>>> Rotational constants (GHZ): 8.3231162 3.9152127
>>>2.8710745
>>> Isotopes: N-14,C-12,H-1,C-12,O-16,C-12,H-1,H-1,H-1,H-1,H-1,H-1
>>> Standard basis: VSTO-3G (5D, 7F)
>>> There are 27 symmetry adapted basis functions of A symmetry.
>>> Crude estimate of integral set expansion from redundant
>>>integrals=1.000.
>>>
>>>--------
>>>
>>>
>>>what I need, is the values:
>>>
>>> 1 7 0 -0.471842 1.385033 -0.179367
>>> 2 6 0 -0.441998 0.034984 0.352626
>>> 3 1 0 -0.532368 0.028578 1.492181
>>> 4 6 0 0.817945 -0.768835 0.128492
>>> 5 8 0 1.897598 -0.056037 -0.129721
>>> 6 6 0 -1.600140 -0.811575 -0.187269
>>> 7 1 0 -1.521476 -1.844231 0.228333
>>> 8 1 0 -2.577732 -0.368082 0.117451
>>> 9 1 0 -1.563751 -0.866827 -1.300932
>>> 10 1 0 0.330252 1.899884 0.122288
>>> 11 1 0 -1.306000 1.857479 0.102977
>>> 12 1 0 2.638344 -0.681170 -0.232052
>>>
>>>
>>>but I don't know how to fetch them
>>>
>>>here's what I've done so far .... (I know that it's not good nor
>>>working )
>>>
>>> CFile file1("test.txt", CFile::modeRead );
>>> DWORD dwLen = file1.GetLength();
>>>
>>> char *pbuf = new char[dwLen] ;
>>>
>>> ffile1.ReadHuge( pbuf, dwLen );
>>>
>>> CString sText(pbuf);
>>>
>>> delete []pbuf;
>>>// sText.Find("Standard orientation:");
>>>
>>>
>>>so I have the whole file in a string, and now I need only to get the
>>>needed values from the string but I don't know how
>>>
>>>any help is welcomed )

>>
>>I'm in a good mood, so here is a foundation.
>>
>>#include <fstream>
>>#include <string>
>>#include <cstdlib>
>>using std::ifstream;
>>using std::string;
>>using std::getline;
>>using std::cerr;
>>
>>int main(void)
>>{
>> ifstream inp_data("test.txt"); // ifstream defaults to text files
>> if (!inp_data)
>> {
>> cerr << "Error opening 'test.txt'\n";
>> return EXIT_FAILURE;
>> }
>>
>>//------------------------------------------------------------
>>// Skip past all the junk.
>>//------------------------------------------------------------
>> string text_line;
>> unsigned int dashed_line_count = 2;
>> while (getline(inp_data, text_line, '\n'))
>> {
>> if ((text_line.length() > 1)
>> && (text_line[0] == '-'))
>> {
>> --dashed_line_count;
>> if (dashed_line_count == 0)
>> break;
>> }
>> }
>>
>> if (!inp_data || (dashed_line_count > 0))
>> {
>> cerr << "Data table not found.\n";
>> return EXIT_FAILURE;
>> }
>>
>>//------------------------------------------------------------
>>// At this point, the next text line should be the
>>// first row of data.
>>//------------------------------------------------------------
>> unsigned int center_number;
>> unsigned int atomic_number;
>> unsigned int atomic_type;
>> double x_ord;
>> double y_ord;
>> double z_ord;
>> while (inp_data >> center_number)
>> {
>> inp_data >> atomic_number >> atomic_type
>> >> x_ord >> y_ord >> z_ord;

>> Process_The_Data(center_number, atomic_number, atomic_type,
>> x_ord, y_ord, z_ord);
>> }
>> return EXIT_SUCCESS;
>>}
>>
>>
>>Look Ma, no $#@!&*# MFC library required.
>>And its portable too.
>>{The code above has not been compiled nor tested.}
>>
>>--
>>Thomas Matthews
>>
>>C++ newsgroup welcome message:
>> http://www.slack.net/~shiva/welcome.txt
>>C++ Faq: http://www.parashift.com/c++-faq-lite
>>C Faq: http://www.eskimo.com/~scs/c-faq/top.html
>>alt.comp.lang.learn.c-c++ faq:
>> http://www.raos.demon.uk/acllc-c++/faq.html
>>Other sites:
>> http://www.josuttis.com -- C++ STL Library book

>
>
>
>
> thanks that you took the time to try to help me, but....
> I can't make your source work ...
> because:
> 1. I haven't seen anywhere in your source the string "Standard
> Orientation", and that is the keystring wich I should use to locate
> the table, because the text file is not with a fixed rows, and there
> can be many things above that table, but when there is the "Standard
> Orientation" string, I know that the table follows.....

Instead of comparing the text line read in to a '-',
use the find() method of std::string to search for "Standard
Orientation". That loop ignores all text until a marker is found.

> 2. The way you added values to those variables (atomic_number etc.)
> are they arrays or single values? because I need all the values from
> the table stored....

If you need _all_ the values stored before processing, then create
a structure and use a vector of the structure:
struct Atomic_Record
{
unsigned int center_number;
unsigned int atomic_number;
//... and so on
};

#include <vector>
using std::vector;

typedef vector<Atomic_Record> Atomic_Vector;
Atomic_Vector atom_info;

//----------------------------
The input fragment will look like:
Atomic_Record inp_record;
while (inp_data >> inp_record.center_number)
{
// Fill in the fields of the structure
inp_data >> inp_record.atomic_number
>> inp_record.atomic_type

// and so on ...
>> inp_record.z_ord;


// Save the record in the array:
atom_info.push_back(inp_record);
}

>
> sorry if I missunderstood something, I'm not that good in programming,
> and thanks again for taking the time to read this bye


A better method would be to overload the operator>> (stream
extraction) for the Atomic_Record structure and use that.
With the overloaded operator, your input loop is simplified as:
while (inp_data >> inp_record)
{
atom_info.push_back(inp_record);
}

Now, I am not writing your program for you, but illustrating some
concepts, such as searching for a key string and reading variables
from a text file. Your challenge is to adapt this information
for your needs.

Read the FAQ and welcome.txt below for some great information.

By the way, do the world a favor and don't prefix your
structure and class names with a 'C' or 'T'. These are
the nomenclature for Microsoft and Borland libraries.
Be more creative.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      07-29-2003
Karl Heinz Buchegger wrote:
>
> Karl Heinz Buchegger wrote:
>
> [snip]
>
>>then one could use (referring to Mathews code again)

>
>
> Apologies to Thomas.
> When writing the reply I was under the impression that your
> first name is Mathews, while in fact Matthews is your surname
> and is spelled differently.
>


Apology accepted.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
Santah
Guest
Posts: n/a
 
      07-31-2003
Thomas Matthews <(E-Mail Removed)> wrote in message news:<OlvVa.13602$(E-Mail Removed) odigy.com>...
> Santah wrote:
> > Thomas Matthews <(E-Mail Removed)> wrote in message news:<i%bVa.13438$(E-Mail Removed) odigy.com>...
> >
> >>Santah wrote:
> >>
> >>
> >>>hi
> >>>I'm new to C++
> >>>and I'm currently working on Visual C++ 6.0
> >>>
> >>>I'm trying to open a text file, and read some data from it
> >>>
> >>>part of the text file looks like this:
> >>>
> >>>--------
> >>>
> >>>Stoichiometry C3H7NO
> >>> Framework group C1[X(C3H7NO)]
> >>> Deg. of freedom 30
> >>> Full point group C1 NOp 1
> >>> Largest Abelian subgroup C1 NOp 1
> >>> Largest concise Abelian subgroup C1 NOp 1
> >>> Standard orientation:
> >>> ---------------------------------------------------------------------
> >>> Center Atomic Atomic Coordinates (Angstroms)
> >>> Number Number Type X Y Z
> >>> ---------------------------------------------------------------------
> >>> 1 7 0 -0.471842 1.385033 -0.179367
> >>> 2 6 0 -0.441998 0.034984 0.352626
> >>> 3 1 0 -0.532368 0.028578 1.492181
> >>> 4 6 0 0.817945 -0.768835 0.128492
> >>> 5 8 0 1.897598 -0.056037 -0.129721
> >>> 6 6 0 -1.600140 -0.811575 -0.187269
> >>> 7 1 0 -1.521476 -1.844231 0.228333
> >>> 8 1 0 -2.577732 -0.368082 0.117451
> >>> 9 1 0 -1.563751 -0.866827 -1.300932
> >>> 10 1 0 0.330252 1.899884 0.122288
> >>> 11 1 0 -1.306000 1.857479 0.102977
> >>> 12 1 0 2.638344 -0.681170 -0.232052
> >>> ---------------------------------------------------------------------
> >>> Rotational constants (GHZ): 8.3231162 3.9152127
> >>>2.8710745
> >>> Isotopes: N-14,C-12,H-1,C-12,O-16,C-12,H-1,H-1,H-1,H-1,H-1,H-1
> >>> Standard basis: VSTO-3G (5D, 7F)
> >>> There are 27 symmetry adapted basis functions of A symmetry.
> >>> Crude estimate of integral set expansion from redundant
> >>>integrals=1.000.
> >>>
> >>>--------
> >>>
> >>>
> >>>what I need, is the values:
> >>>
> >>> 1 7 0 -0.471842 1.385033 -0.179367
> >>> 2 6 0 -0.441998 0.034984 0.352626
> >>> 3 1 0 -0.532368 0.028578 1.492181
> >>> 4 6 0 0.817945 -0.768835 0.128492
> >>> 5 8 0 1.897598 -0.056037 -0.129721
> >>> 6 6 0 -1.600140 -0.811575 -0.187269
> >>> 7 1 0 -1.521476 -1.844231 0.228333
> >>> 8 1 0 -2.577732 -0.368082 0.117451
> >>> 9 1 0 -1.563751 -0.866827 -1.300932
> >>> 10 1 0 0.330252 1.899884 0.122288
> >>> 11 1 0 -1.306000 1.857479 0.102977
> >>> 12 1 0 2.638344 -0.681170 -0.232052
> >>>
> >>>
> >>>but I don't know how to fetch them
> >>>
> >>>here's what I've done so far .... (I know that it's not good nor
> >>>working )
> >>>
> >>> CFile file1("test.txt", CFile::modeRead );
> >>> DWORD dwLen = file1.GetLength();
> >>>
> >>> char *pbuf = new char[dwLen] ;
> >>>
> >>> ffile1.ReadHuge( pbuf, dwLen );
> >>>
> >>> CString sText(pbuf);
> >>>
> >>> delete []pbuf;
> >>>// sText.Find("Standard orientation:");
> >>>
> >>>
> >>>so I have the whole file in a string, and now I need only to get the
> >>>needed values from the string but I don't know how
> >>>
> >>>any help is welcomed )
> >>
> >>I'm in a good mood, so here is a foundation.
> >>
> >>#include <fstream>
> >>#include <string>
> >>#include <cstdlib>
> >>using std::ifstream;
> >>using std::string;
> >>using std::getline;
> >>using std::cerr;
> >>
> >>int main(void)
> >>{
> >> ifstream inp_data("test.txt"); // ifstream defaults to text files
> >> if (!inp_data)
> >> {
> >> cerr << "Error opening 'test.txt'\n";
> >> return EXIT_FAILURE;
> >> }
> >>
> >>//------------------------------------------------------------
> >>// Skip past all the junk.
> >>//------------------------------------------------------------
> >> string text_line;
> >> unsigned int dashed_line_count = 2;
> >> while (getline(inp_data, text_line, '\n'))
> >> {
> >> if ((text_line.length() > 1)
> >> && (text_line[0] == '-'))
> >> {
> >> --dashed_line_count;
> >> if (dashed_line_count == 0)
> >> break;
> >> }
> >> }
> >>
> >> if (!inp_data || (dashed_line_count > 0))
> >> {
> >> cerr << "Data table not found.\n";
> >> return EXIT_FAILURE;
> >> }
> >>
> >>//------------------------------------------------------------
> >>// At this point, the next text line should be the
> >>// first row of data.
> >>//------------------------------------------------------------
> >> unsigned int center_number;
> >> unsigned int atomic_number;
> >> unsigned int atomic_type;
> >> double x_ord;
> >> double y_ord;
> >> double z_ord;
> >> while (inp_data >> center_number)
> >> {
> >> inp_data >> atomic_number >> atomic_type
> >> >> x_ord >> y_ord >> z_ord;
> >> Process_The_Data(center_number, atomic_number, atomic_type,
> >> x_ord, y_ord, z_ord);
> >> }
> >> return EXIT_SUCCESS;
> >>}
> >>
> >>
> >>Look Ma, no $#@!&*# MFC library required.
> >>And its portable too.
> >>{The code above has not been compiled nor tested.}
> >>
> >>--
> >>Thomas Matthews
> >>
> >>C++ newsgroup welcome message:
> >> http://www.slack.net/~shiva/welcome.txt
> >>C++ Faq: http://www.parashift.com/c++-faq-lite
> >>C Faq: http://www.eskimo.com/~scs/c-faq/top.html
> >>alt.comp.lang.learn.c-c++ faq:
> >> http://www.raos.demon.uk/acllc-c++/faq.html
> >>Other sites:
> >> http://www.josuttis.com -- C++ STL Library book

> >
> >
> >
> >
> > thanks that you took the time to try to help me, but....
> > I can't make your source work ...
> > because:
> > 1. I haven't seen anywhere in your source the string "Standard
> > Orientation", and that is the keystring wich I should use to locate
> > the table, because the text file is not with a fixed rows, and there
> > can be many things above that table, but when there is the "Standard
> > Orientation" string, I know that the table follows.....

> Instead of comparing the text line read in to a '-',
> use the find() method of std::string to search for "Standard
> Orientation". That loop ignores all text until a marker is found.
>
> > 2. The way you added values to those variables (atomic_number etc.)
> > are they arrays or single values? because I need all the values from
> > the table stored....

> If you need _all_ the values stored before processing, then create
> a structure and use a vector of the structure:
> struct Atomic_Record
> {
> unsigned int center_number;
> unsigned int atomic_number;
> //... and so on
> };
>
> #include <vector>
> using std::vector;
>
> typedef vector<Atomic_Record> Atomic_Vector;
> Atomic_Vector atom_info;
>
> //----------------------------
> The input fragment will look like:
> Atomic_Record inp_record;
> while (inp_data >> inp_record.center_number)
> {
> // Fill in the fields of the structure
> inp_data >> inp_record.atomic_number
> >> inp_record.atomic_type

> // and so on ...
> >> inp_record.z_ord;

>
> // Save the record in the array:
> atom_info.push_back(inp_record);
> }
>
> >
> > sorry if I missunderstood something, I'm not that good in programming,
> > and thanks again for taking the time to read this bye

>
> A better method would be to overload the operator>> (stream
> extraction) for the Atomic_Record structure and use that.
> With the overloaded operator, your input loop is simplified as:
> while (inp_data >> inp_record)
> {
> atom_info.push_back(inp_record);
> }
>
> Now, I am not writing your program for you, but illustrating some
> concepts, such as searching for a key string and reading variables
> from a text file. Your challenge is to adapt this information
> for your needs.
>
> Read the FAQ and welcome.txt below for some great information.
>
> By the way, do the world a favor and don't prefix your
> structure and class names with a 'C' or 'T'. These are
> the nomenclature for Microsoft and Borland libraries.
> Be more creative.
>
> --
> Thomas Matthews
>
> C++ newsgroup welcome message:
> http://www.slack.net/~shiva/welcome.txt
> C++ Faq: http://www.parashift.com/c++-faq-lite
> C Faq: http://www.eskimo.com/~scs/c-faq/top.html
> alt.comp.lang.learn.c-c++ faq:
> http://www.raos.demon.uk/acllc-c++/faq.html
> Other sites:
> http://www.josuttis.com -- C++ STL Library book




"By the way, do the world a favor and don't prefix your
structure and class names with a 'C' or 'T'. These are
the nomenclature for Microsoft and Borland libraries.
Be more creative."

I'm not doing it, and I'm against it too
If I had pasted some code with class names with "c" or "t" it's
because the code was not mine and I adapted it )

thanks for the help ...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading data from a text file Alex Buell C++ 9 03-22-2006 07:19 AM
Reading data from Text file imran Java 4 03-11-2005 09:03 PM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM
reading output file data as input data Stephen Moon Perl Misc 5 02-29-2004 02:16 PM
runtime error reading data from text file Robocop Java 2 02-29-2004 01:38 PM



Advertisments