473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

reading data from text file

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 :))
Jul 19 '05 #1
7 14621
zot
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" <sl*****@yahoo.com> ha scritto nel messaggio
news:ed**************************@posting.google.c 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 :))

Jul 19 '05 #2
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" <sl*****@yahoo.com> ha scritto nel messaggio
news:ed**************************@posting.google.c 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

Jul 19 '05 #3
Thomas Matthews <Th**********************@sbcglobal.net> wrote in message news:<i%************************@newssvr10.news.pr 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
Jul 19 '05 #4


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
kb******@gascad.at
Jul 19 '05 #5
Santah wrote:
Thomas Matthews <Th**********************@sbcglobal.net> wrote in message news:<i%************************@newssvr10.news.pr 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

Jul 19 '05 #6
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

Jul 19 '05 #7
Thomas Matthews <Th**********************@sbcglobal.net> wrote in message news:<Ol************************@newssvr10.news.pr odigy.com>...
Santah wrote:
Thomas Matthews <Th**********************@sbcglobal.net> wrote in message news:<i%************************@newssvr10.news.pr 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 ...
Jul 19 '05 #8

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

Similar topics

6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.