Code for read a file

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

    #1

    Code for read a file

    Hello members of the comp.lang.c++,

    My log file is made of a set of 1000 following lines kind:

    21/09/07 13:49:56,MW.SET .D_IGLS,2.00000 0
    21/09/07 13:49:56,MW.SET .GNP_NT,7.00000 0
    .....

    commas ',' are used as field separators (data, name, value).

    I'd need a program that read any field into the following structured
    table (without the commas)

    struct
    {
    char strDateHour[30];
    char strName[80];
    double value;
    } Data[1000];

    Please can you explain a simple code to do so ?

    Thank-You

    Octavio

  • Hollywood

    #2
    Re: Code for read a file

    On 25 sep, 16:05, Hollywood <freepos...@gma il.comwrote:
    Hello members of the comp.lang.c++,
    >
    My log file is made of a set of 1000 following lines kind:
    >
    21/09/07 13:49:56,MW.SET .D_IGLS,2.00000 0
    21/09/07 13:49:56,MW.SET .GNP_NT,7.00000 0
    ....
    >
    commas ',' are used as field separators (data, name, value).
    >
    I'd need a program that read any field into the following structured
    table (without the commas)
    >
    struct
    {
    char strDateHour[30];
    char strName[80];
    double value;
    } Data[1000];
    >
    Please can you explain a simple code to do so ?
    >
    Thank-You
    >
    Octavio
    I just forgot to tell you the file object is a CFile class one.

    Comment

    • Sze

      #3
      Re: Code for read a file

      On 25 Sep., 16:15, Hollywood <freepos...@gma il.comwrote:
      On 25 sep, 16:05, Hollywood <freepos...@gma il.comwrote:
      >
      >
      >
      Hello members of the comp.lang.c++,
      >
      My log file is made of a set of 1000 following lines kind:
      >
      21/09/07 13:49:56,MW.SET .D_IGLS,2.00000 0
      21/09/07 13:49:56,MW.SET .GNP_NT,7.00000 0
      ....
      >
      commas ',' are used as field separators (data, name, value).
      >
      I'd need a program that read any field into the following structured
      table (without the commas)
      >
      struct
      {
      char strDateHour[30];
      char strName[80];
      double value;
      } Data[1000];
      >
      Please can you explain a simple code to do so ?
      >
      Thank-You
      >
      Octavio
      >
      I just forgot to tell you the file object is a CFile class one.
      Perhaps you should, paste your code or a link to it, because else it
      looks like
      you are searching for a programming slave, instead of someone who is
      in a beneficial mood,
      so he want to use his time to help you.
      Nobody likes it to help somebody with something, who doesn`t seem like
      he is trying it by himself.

      Comment

      • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

        #4
        Re: Code for read a file

        On 2007-09-25 16:05, Hollywood wrote:
        Hello members of the comp.lang.c++,
        >
        My log file is made of a set of 1000 following lines kind:
        >
        21/09/07 13:49:56,MW.SET .D_IGLS,2.00000 0
        21/09/07 13:49:56,MW.SET .GNP_NT,7.00000 0
        ....
        >
        commas ',' are used as field separators (data, name, value).
        >
        I'd need a program that read any field into the following structured
        table (without the commas)
        >
        struct
        {
        char strDateHour[30];
        char strName[80];
        double value;
        } Data[1000];
        >
        Please can you explain a simple code to do so ?
        Look into overloading the >operator for you struct, perhaps by using
        getline() to read a line and then parsing each line for the values.

        --
        Erik Wikström

        Comment

        • Hollywood

          #5
          Re: Code for read a file

          On 25 sep, 17:09, Sze <kamistr...@gmx .dewrote:
          On 25 Sep., 16:15, Hollywood <freepos...@gma il.comwrote:
          >
          >
          >
          >
          >
          On 25 sep, 16:05, Hollywood <freepos...@gma il.comwrote:
          >
          Hello members of the comp.lang.c++,
          >
          My log file is made of a set of 1000 following lines kind:
          >
          21/09/07 13:49:56,MW.SET .D_IGLS,2.00000 0
          21/09/07 13:49:56,MW.SET .GNP_NT,7.00000 0
          ....
          >
          commas ',' are used as field separators (data, name, value).
          >
          I'd need a program that read any field into the following structured
          table (without the commas)
          >
          struct
          {
          char strDateHour[30];
          char strName[80];
          double value;
          } Data[1000];
          >
          Please can you explain a simple code to do so ?
          >
          Thank-You
          >
          Octavio
          >
          I just forgot to tell you the file object is a CFile class one.
          >
          Perhaps you should, paste your code or a link to it, because else it
          looks like
          you are searching for a programming slave, instead of someone who is
          in a beneficial mood,
          so he want to use his time to help you.
          Nobody likes it to help somebody with something, who doesn`t seem like
          he is trying it by himself.- Masquer le texte des messages précédents-
          >
          - Afficher le texte des messages précédents -
          Ok, thank-You for your remarks.
          Here goes what I've got :

          #include "StdAfx.h"
          #include "listelem.h "

          CListElem ObjFile;

          CListElem::CLis tElem(void)
          {
          }

          CListElem::~CLi stElem(void)
          {
          }
          void CListElem::Read LogFile()
          {
          // Open the LogFile

          CFile LogFile;
          CFileException e;
          char* pLogFileName = "Log.log";
          if( !LogFile.Open( pLogFileName, CFile::modeRead , &e ) )
          {
          #ifdef _DEBUG
          afxDump << "The file is not open " << e.m_cause << "\n";
          #endif
          }

          // Reading part

          // while not end of file
          // read every character of the Log file and put it
          into the structure above


          }

          struct
          {
          char strDateHour[30];
          char strName[80];
          double value;
          } Data[1000];

          Now the question is how to test the end of a CFile file ? ( a kind of
          EndOfFile).

          Many Thanks

          Octavio

          Comment

          • Sze

            #6
            Re: Code for read a file

            On 25 Sep., 18:15, Hollywood <freepos...@gma il.comwrote:
            On 25 sep, 17:09, Sze <kamistr...@gmx .dewrote:
            >
            >
            >
            On 25 Sep., 16:15, Hollywood <freepos...@gma il.comwrote:
            >
            On 25 sep, 16:05, Hollywood <freepos...@gma il.comwrote:
            >
            Hello members of the comp.lang.c++,
            >
            My log file is made of a set of 1000 following lines kind:
            >
            21/09/07 13:49:56,MW.SET .D_IGLS,2.00000 0
            21/09/07 13:49:56,MW.SET .GNP_NT,7.00000 0
            ....
            >
            commas ',' are used as field separators (data, name, value).
            >
            I'd need a program that read any field into the following structured
            table (without the commas)
            >
            struct
            {
            char strDateHour[30];
            char strName[80];
            double value;
            } Data[1000];
            >
            Please can you explain a simple code to do so ?
            >
            Thank-You
            >
            Octavio
            >
            I just forgot to tell you the file object is a CFile class one.
            >
            Perhaps you should, paste your code or a link to it, because else it
            looks like
            you are searching for a programming slave, instead of someone who is
            in a beneficial mood,
            so he want to use his time to help you.
            Nobody likes it to help somebody with something, who doesn`t seem like
            he is trying it by himself.- Masquer le texte des messages précédents -
            >
            - Afficher le texte des messages précédents -
            >
            Ok, thank-You for your remarks.
            Here goes what I've got :
            >
            #include "StdAfx.h"
            #include "listelem.h "
            >
            CListElem ObjFile;
            >
            CListElem::CLis tElem(void)
            {
            >
            }
            >
            CListElem::~CLi stElem(void)
            {}
            >
            void CListElem::Read LogFile()
            {
            // Open the LogFile
            >
            CFile LogFile;
            CFileException e;
            char* pLogFileName = "Log.log";
            if( !LogFile.Open( pLogFileName, CFile::modeRead , &e ) )
            {
            #ifdef _DEBUG
            afxDump << "The file is not open " << e.m_cause << "\n";
            #endif
            }
            >
            // Reading part
            >
            // while not end of file
            // read every character of the Log file and put it
            into the structure above
            >
            }
            >
            struct
            {
            char strDateHour[30];
            char strName[80];
            double value;
            } Data[1000];
            >
            Now the question is how to test the end of a CFile file ? ( a kind of
            EndOfFile).
            >
            Many Thanks
            >
            Octavio
            I am not sure where this CFile class belongs to, could you please
            enlighten me, so I could help you, hopefully?

            Comment

            • Sze

              #7
              Re: Code for read a file

              On 25 Sep., 19:43, Sze <kamistr...@gmx .dewrote:
              On 25 Sep., 18:15, Hollywood <freepos...@gma il.comwrote:
              >
              >
              >
              On 25 sep, 17:09, Sze <kamistr...@gmx .dewrote:
              >
              On 25 Sep., 16:15, Hollywood <freepos...@gma il.comwrote:
              >
              On 25 sep, 16:05, Hollywood <freepos...@gma il.comwrote:
              >
              Hello members of the comp.lang.c++,
              >
              My log file is made of a set of 1000 following lines kind:
              >
              21/09/07 13:49:56,MW.SET .D_IGLS,2.00000 0
              21/09/07 13:49:56,MW.SET .GNP_NT,7.00000 0
              ....
              >
              commas ',' are used as field separators (data, name, value).
              >
              I'd need a program that read any field into the following structured
              table (without the commas)
              >
              struct
              {
              char strDateHour[30];
              char strName[80];
              double value;
              } Data[1000];
              >
              Please can you explain a simple code to do so ?
              >
              Thank-You
              >
              Octavio
              >
              I just forgot to tell you the file object is a CFile class one.
              >
              Perhaps you should, paste your code or a link to it, because else it
              looks like
              you are searching for a programming slave, instead of someone who is
              in a beneficial mood,
              so he want to use his time to help you.
              Nobody likes it to help somebody with something, who doesn`t seem like
              he is trying it by himself.- Masquer le texte des messages précédents -
              >
              - Afficher le texte des messages précédents -
              >
              Ok, thank-You for your remarks.
              Here goes what I've got :
              >
              #include "StdAfx.h"
              #include "listelem.h "
              >
              CListElem ObjFile;
              >
              CListElem::CLis tElem(void)
              {
              >
              }
              >
              CListElem::~CLi stElem(void)
              {}
              >
              void CListElem::Read LogFile()
              {
              // Open the LogFile
              >
              CFile LogFile;
              CFileException e;
              char* pLogFileName = "Log.log";
              if( !LogFile.Open( pLogFileName, CFile::modeRead , &e ))
              {
              #ifdef _DEBUG
              afxDump << "The file is not open " << e.m_cause<< "\n";
              #endif
              }
              >
              // Reading part
              >
              // while not end of file
              // read every character of the Log file and put it
              into the structure above
              >
              }
              >
              struct
              {
              char strDateHour[30];
              char strName[80];
              double value;
              } Data[1000];
              >
              Now the question is how to test the end of a CFile file ? ( a kind of
              EndOfFile).
              >
              Many Thanks
              >
              Octavio
              >
              I am not sure where this CFile class belongs to, could you please
              enlighten me, so I could help you, hopefully?
              Ah, I have got it, it`s an MFC Class, aren`t it?
              Well if you are using the mfc class, you could compare wheather the
              read methode, returned the passed amount of bytes, else it should get
              to the end of the file. I do not found a special methode to querry
              this.

              Comment

              • Jim Langston

                #8
                Re: Code for read a file

                "Hollywood" <freeposte1@gma il.comwrote in message
                news:1190729123 .791178.79070@1 9g2000hsx.googl egroups.com...
                Hello members of the comp.lang.c++,
                >
                My log file is made of a set of 1000 following lines kind:
                >
                21/09/07 13:49:56,MW.SET .D_IGLS,2.00000 0
                21/09/07 13:49:56,MW.SET .GNP_NT,7.00000 0
                ....
                >
                commas ',' are used as field separators (data, name, value).
                >
                I'd need a program that read any field into the following structured
                table (without the commas)
                >
                struct
                {
                char strDateHour[30];
                char strName[80];
                double value;
                } Data[1000];
                >
                Please can you explain a simple code to do so ?
                Output of the following program is:

                Date/Time: 21/09/07 13:49:56
                Name: MW.SET.D_IGLS
                Value: 2

                To get the std::string into a c-style array use strcpy such as:
                strcpy( strDateHour, DateHour.c_str( ) );

                Since this is a CFile forget about it, Windows specific, but you just want
                to read an entire line and parse it.

                #include <sstream>
                #include <iostream>
                #include <string>

                int main()
                {
                std::string Foo("21/09/07 13:49:56,MW.SET .D_IGLS,2.00000 0" );
                std::stringstre am Bar( Foo );

                // Read date from foo
                std::string Date;
                Bar >Date;
                std::string Hour;
                std::getline( Bar, Hour, ',' );

                std::string DateHour = Date + " " + Hour;

                std::string Name;
                std::getline( Bar, Name, ',' );

                double Value;
                Bar >Value;
                std::cout << "Date/Time: " << DateHour <<
                "\nName: " << Name <<
                "\nValue: " << Value << std::endl;
                }


                Comment

                • Hollywood

                  #9
                  Re: Code for read a file

                  On 25 sep, 19:55, Sze <kamistr...@gmx .dewrote:
                  On 25 Sep., 19:43, Sze <kamistr...@gmx .dewrote:
                  >
                  >
                  >
                  >
                  >
                  On 25 Sep., 18:15, Hollywood <freepos...@gma il.comwrote:
                  >
                  On 25 sep, 17:09, Sze <kamistr...@gmx .dewrote:
                  >
                  On 25 Sep., 16:15, Hollywood <freepos...@gma il.comwrote:
                  >
                  On 25 sep, 16:05, Hollywood <freepos...@gma il.comwrote:
                  >
                  Hello members of the comp.lang.c++,
                  >
                  My log file is made of a set of 1000 following lines kind:
                  >
                  21/09/07 13:49:56,MW.SET .D_IGLS,2.00000 0
                  21/09/07 13:49:56,MW.SET .GNP_NT,7.00000 0
                  ....
                  >
                  commas ',' are used as field separators (data, name, value).
                  >
                  I'd need a program that read any field into the following structured
                  table (without the commas)
                  >
                  struct
                  {
                  char strDateHour[30];
                  char strName[80];
                  double value;
                  } Data[1000];
                  >
                  Please can you explain a simple code to do so ?
                  >
                  Thank-You
                  >
                  Octavio
                  >
                  I just forgot to tell you the file object is a CFile class one.
                  >
                  Perhaps you should, paste your code or a link to it, because else it
                  looks like
                  you are searching for a programming slave, instead of someone who is
                  in a beneficial mood,
                  so he want to use his time to help you.
                  Nobody likes it to help somebody with something, who doesn`t seem like
                  he is trying it by himself.- Masquer le texte des messages précédents -
                  >
                  - Afficher le texte des messages précédents -
                  >
                  Ok, thank-You for your remarks.
                  Here goes what I've got :
                  >
                  #include "StdAfx.h"
                  #include "listelem.h "
                  >
                  CListElem ObjFile;
                  >
                  CListElem::CLis tElem(void)
                  {
                  >
                  }
                  >
                  CListElem::~CLi stElem(void)
                  {}
                  >
                  void CListElem::Read LogFile()
                  {
                  // Open the LogFile
                  >
                  CFile LogFile;
                  CFileException e;
                  char* pLogFileName = "Log.log";
                  if( !LogFile.Open( pLogFileName, CFile::modeRead , &e) )
                  {
                  #ifdef _DEBUG
                  afxDump << "The file is not open " << e.m_cause << "\n";
                  #endif
                  }
                  >
                  // Reading part
                  >
                  // while not end of file
                  // read every character of the Log file and put it
                  into the structure above
                  >
                  }
                  >
                  struct
                  {
                  char strDateHour[30];
                  char strName[80];
                  double value;
                  } Data[1000];
                  >
                  Now the question is how to test the end of a CFile file ? ( a kind of
                  EndOfFile).
                  >
                  Many Thanks
                  >
                  Octavio
                  >
                  I am not sure where this CFile class belongs to, could you please
                  enlighten me, so I could help you, hopefully?
                  >
                  Ah, I have got it, it`s an MFC Class, aren`t it?
                  Well if you are using the mfc class, you could compare wheather the
                  read methode, returned the passed amount of bytes, else it should get
                  to the end of the file. I do not found a special methode to querry
                  this.- Masquer le texte des messages précédents -
                  >
                  - Afficher le texte des messages précédents -
                  That's right, CFile is a MFC class. What I'm searching is a CFile
                  function that allow to read caracter by caracter in a file and put it
                  into my structure.

                  The Microsoft Visual C++ help show me the following :

                  char pbuf[100];
                  UINT nBytesRead = LogFile.Read( pbuf, 100 );

                  Once the pbuf loaded with the lines I'll find a way to parse it
                  searching for commas and placing the text in between into the corrects
                  fields of my structure.

                  Many thanks

                  Comment

                  • Hollywood

                    #10
                    Re: Code for read a file

                    SNIP
                    >
                    Output of the following program is:
                    >
                    Date/Time: 21/09/07 13:49:56
                    Name: MW.SET.D_IGLS
                    Value: 2
                    >
                    To get the std::string into a c-style array use strcpy such as:
                    strcpy( strDateHour, DateHour.c_str( ) );
                    >
                    Since this is a CFile forget about it, Windows specific, but you just want
                    to read an entire line and parse it.
                    >
                    #include <sstream>
                    #include <iostream>
                    #include <string>
                    >
                    int main()
                    {
                    std::string Foo("21/09/07 13:49:56,MW.SET .D_IGLS,2.00000 0" );
                    std::stringstre am Bar( Foo );
                    >
                    // Read date from foo
                    std::string Date;
                    Bar >Date;
                    std::string Hour;
                    std::getline( Bar, Hour, ',' );
                    >
                    std::string DateHour = Date + " " + Hour;
                    >
                    std::string Name;
                    std::getline( Bar, Name, ',' );
                    >
                    double Value;
                    Bar >Value;
                    std::cout << "Date/Time: " << DateHour <<
                    "\nName: " << Name <<
                    "\nValue: " << Value << std::endl;
                    >
                    >
                    >
                    }
                    Thank-you Jim for your code, I'm trying to adapt it to my classes.
                    Another question please , is there any CFile function that can read a
                    line (from start to the new line character) into your string Foo. The
                    length of the lines in my Log files are not the same.

                    Comment

                    Working...