reading data from text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Santah

    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("Sta ndard 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 :))
  • zot

    #2
    Re: reading data from text file

    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" <slice17@yahoo. com> ha scritto nel messaggio
    news:ed9bdbcb.0 307272339.3d78e d64@posting.goo gle.com...[color=blue]
    > 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("Sta ndard 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 :))[/color]


    Comment

    • Thomas Matthews

      #3
      Re: reading data from text file

      zot wrote:
      1. Don't top-post.[color=blue]
      > 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.[/color]
      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.

      [color=blue]
      > 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.[/color]
      One could use the stringstream functions too.
      Or even sscanf().
      [color=blue]
      > 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):[/color]
      But why write a function when one is available?
      [color=blue]
      >
      > 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" <slice17@yahoo. com> ha scritto nel messaggio
      > news:ed9bdbcb.0 307272339.3d78e d64@posting.goo gle.com...
      >[/color]

      --
      Thomas Matthews

      C++ newsgroup welcome message:

      C++ Faq: http://www.parashift.com/c++-faq-lite
      C Faq: http://www.eskimo.com/~scs/c-faq/top.html
      alt.comp.lang.l earn.c-c++ faq:

      Other sites:
      http://www.josuttis.com -- C++ STL Library book

      Comment

      • Santah

        #4
        Re: reading data from text file

        Thomas Matthews <Thomas_Matthew sHatesSpam@sbcg lobal.net> wrote in message news:<i%bVa.134 38$9M4.37360047 51@newssvr10.ne ws.prodigy.com> ...[color=blue]
        > Santah wrote:
        >[color=green]
        > > 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("Sta ndard 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 :))[/color]
        >
        > 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_cou nt = 2;
        > while (getline(inp_da ta, text_line, '\n'))
        > {
        > if ((text_line.len gth() > 1)
        > && (text_line[0] == '-'))
        > {
        > --dashed_line_cou nt;
        > if (dashed_line_co unt == 0)
        > break;
        > }
        > }
        >
        > if (!inp_data || (dashed_line_co unt > 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[color=green][color=darkred]
        > >> x_ord >> y_ord >> z_ord;[/color][/color]
        > Process_The_Dat a(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.l earn.c-c++ faq:
        > http://www.raos.demon.uk/acllc-c++/faq.html
        > Other sites:
        > http://www.josuttis.com -- C++ STL Library book[/color]



        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

        Comment

        • Karl Heinz Buchegger

          #5
          Re: reading data from text file



          Karl Heinz Buchegger wrote:[color=blue]
          >[/color]
          [snip][color=blue]
          > then one could use (referring to Mathews code again)[/color]

          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
          kbuchegg@gascad .at

          Comment

          • Thomas Matthews

            #6
            Re: reading data from text file

            Santah wrote:[color=blue]
            > Thomas Matthews <Thomas_Matthew sHatesSpam@sbcg lobal.net> wrote in message news:<i%bVa.134 38$9M4.37360047 51@newssvr10.ne ws.prodigy.com> ...
            >[color=green]
            >>Santah wrote:
            >>
            >>[color=darkred]
            >>>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:
            >>>
            >>>--------
            >>>
            >>>Stoichiometr y 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("Sta ndard 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 :))[/color]
            >>
            >>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_cou nt = 2;
            >> while (getline(inp_da ta, text_line, '\n'))
            >> {
            >> if ((text_line.len gth() > 1)
            >> && (text_line[0] == '-'))
            >> {
            >> --dashed_line_cou nt;
            >> if (dashed_line_co unt == 0)
            >> break;
            >> }
            >> }
            >>
            >> if (!inp_data || (dashed_line_co unt > 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[color=darkred]
            >> >> x_ord >> y_ord >> z_ord;[/color]
            >> Process_The_Dat a(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[/color]
            >
            >
            >
            >
            > 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.....[/color]
            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.
            [color=blue]
            > 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....[/color]
            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_R ecord> Atomic_Vector;
            Atomic_Vector atom_info;

            //----------------------------
            The input fragment will look like:
            Atomic_Record inp_record;
            while (inp_data >> inp_record.cent er_number)
            {
            // Fill in the fields of the structure
            inp_data >> inp_record.atom ic_number[color=blue][color=green]
            >> inp_record.atom ic_type[/color][/color]
            // and so on ...[color=blue][color=green]
            >> inp_record.z_or d;[/color][/color]

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

            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:

            C++ Faq: http://www.parashift.com/c++-faq-lite
            C Faq: http://www.eskimo.com/~scs/c-faq/top.html
            alt.comp.lang.l earn.c-c++ faq:

            Other sites:
            http://www.josuttis.com -- C++ STL Library book

            Comment

            • Thomas Matthews

              #7
              Re: reading data from text file

              Karl Heinz Buchegger wrote:[color=blue]
              >
              > Karl Heinz Buchegger wrote:
              >
              > [snip]
              >[color=green]
              >>then one could use (referring to Mathews code again)[/color]
              >
              >
              > 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.
              >[/color]

              Apology accepted.

              --
              Thomas Matthews

              C++ newsgroup welcome message:

              C++ Faq: http://www.parashift.com/c++-faq-lite
              C Faq: http://www.eskimo.com/~scs/c-faq/top.html
              alt.comp.lang.l earn.c-c++ faq:

              Other sites:
              http://www.josuttis.com -- C++ STL Library book

              Comment

              • Santah

                #8
                Re: reading data from text file

                Thomas Matthews <Thomas_Matthew sHatesSpam@sbcg lobal.net> wrote in message news:<OlvVa.136 02$hZ3.37974713 48@newssvr10.ne ws.prodigy.com> ...[color=blue]
                > Santah wrote:[color=green]
                > > Thomas Matthews <Thomas_Matthew sHatesSpam@sbcg lobal.net> wrote in message news:<i%bVa.134 38$9M4.37360047 51@newssvr10.ne ws.prodigy.com> ...
                > >[color=darkred]
                > >>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:
                > >>>
                > >>>--------
                > >>>
                > >>>Stoichiometr y 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("Sta ndard 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_cou nt = 2;
                > >> while (getline(inp_da ta, text_line, '\n'))
                > >> {
                > >> if ((text_line.len gth() > 1)
                > >> && (text_line[0] == '-'))
                > >> {
                > >> --dashed_line_cou nt;
                > >> if (dashed_line_co unt == 0)
                > >> break;
                > >> }
                > >> }
                > >>
                > >> if (!inp_data || (dashed_line_co unt > 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_Dat a(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[/color]
                > >
                > >
                > >
                > >
                > > 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.....[/color]
                > 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.
                >[color=green]
                > > 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....[/color]
                > 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_R ecord> Atomic_Vector;
                > Atomic_Vector atom_info;
                >
                > //----------------------------
                > The input fragment will look like:
                > Atomic_Record inp_record;
                > while (inp_data >> inp_record.cent er_number)
                > {
                > // Fill in the fields of the structure
                > inp_data >> inp_record.atom ic_number[color=green][color=darkred]
                > >> inp_record.atom ic_type[/color][/color]
                > // and so on ...[color=green][color=darkred]
                > >> inp_record.z_or d;[/color][/color]
                >
                > // Save the record in the array:
                > atom_info.push_ back(inp_record );
                > }
                >[color=green]
                > >
                > > sorry if I missunderstood something, I'm not that good in programming,
                > > and thanks again for taking the time to read this :) bye[/color]
                >
                > 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.l earn.c-c++ faq:
                > http://www.raos.demon.uk/acllc-c++/faq.html
                > Other sites:
                > http://www.josuttis.com -- C++ STL Library book[/color]



                "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 ...

                Comment

                Working...