Connecting Tech Pros Worldwide Help | Site Map

Code for read a file

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 25th, 2007, 02:15 PM
Hollywood
Guest
 
Posts: n/a
Default 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.000000
21/09/07 13:49:56,MW.SET.GNP_NT,7.000000
.....

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


  #2  
Old September 25th, 2007, 02:25 PM
Hollywood
Guest
 
Posts: n/a
Default Re: Code for read a file

On 25 sep, 16:05, Hollywood <freepos...@gmail.comwrote:
Quote:
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.000000
21/09/07 13:49:56,MW.SET.GNP_NT,7.000000
....
>
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.

  #3  
Old September 25th, 2007, 03:15 PM
Sze
Guest
 
Posts: n/a
Default Re: Code for read a file

On 25 Sep., 16:15, Hollywood <freepos...@gmail.comwrote:
Quote:
On 25 sep, 16:05, Hollywood <freepos...@gmail.comwrote:
>
>
>
Quote:
Hello members of the comp.lang.c++,
>
Quote:
My log file is made of a set of 1000 following lines kind:
>
Quote:
21/09/07 13:49:56,MW.SET.D_IGLS,2.000000
21/09/07 13:49:56,MW.SET.GNP_NT,7.000000
....
>
Quote:
commas ',' are used as field separators (data, name, value).
>
Quote:
I'd need a program that read any field into the following structured
table (without the commas)
>
Quote:
struct
{
char strDateHour[30];
char strName[80];
double value;
} Data[1000];
>
Quote:
Please can you explain a simple code to do so ?
>
Quote:
Thank-You
>
Quote:
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.

  #4  
Old September 25th, 2007, 04:05 PM
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
Default Re: Code for read a file

On 2007-09-25 16:05, Hollywood wrote:
Quote:
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.000000
21/09/07 13:49:56,MW.SET.GNP_NT,7.000000
....
>
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
  #5  
Old September 25th, 2007, 04:25 PM
Hollywood
Guest
 
Posts: n/a
Default Re: Code for read a file

On 25 sep, 17:09, Sze <kamistr...@gmx.dewrote:
Quote:
On 25 Sep., 16:15, Hollywood <freepos...@gmail.comwrote:
>
>
>
>
>
Quote:
On 25 sep, 16:05, Hollywood <freepos...@gmail.comwrote:
>
Quote:
Quote:
Hello members of the comp.lang.c++,
>
Quote:
Quote:
My log file is made of a set of 1000 following lines kind:
>
Quote:
Quote:
21/09/07 13:49:56,MW.SET.D_IGLS,2.000000
21/09/07 13:49:56,MW.SET.GNP_NT,7.000000
....
>
Quote:
Quote:
commas ',' are used as field separators (data, name, value).
>
Quote:
Quote:
I'd need a program that read any field into the following structured
table (without the commas)
>
Quote:
Quote:
struct
{
char strDateHour[30];
char strName[80];
double value;
} Data[1000];
>
Quote:
Quote:
Please can you explain a simple code to do so ?
>
Quote:
Quote:
Thank-You
>
Quote:
Quote:
Octavio
>
Quote:
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::CListElem(void)
{
}

CListElem::~CListElem(void)
{
}
void CListElem::ReadLogFile()
{
// 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

  #6  
Old September 25th, 2007, 05:45 PM
Sze
Guest
 
Posts: n/a
Default Re: Code for read a file

On 25 Sep., 18:15, Hollywood <freepos...@gmail.comwrote:
Quote:
On 25 sep, 17:09, Sze <kamistr...@gmx.dewrote:
>
>
>
Quote:
On 25 Sep., 16:15, Hollywood <freepos...@gmail.comwrote:
>
Quote:
Quote:
On 25 sep, 16:05, Hollywood <freepos...@gmail.comwrote:
>
Quote:
Quote:
Hello members of the comp.lang.c++,
>
Quote:
Quote:
My log file is made of a set of 1000 following lines kind:
>
Quote:
Quote:
21/09/07 13:49:56,MW.SET.D_IGLS,2.000000
21/09/07 13:49:56,MW.SET.GNP_NT,7.000000
....
>
Quote:
Quote:
commas ',' are used as field separators (data, name, value).
>
Quote:
Quote:
I'd need a program that read any field into the following structured
table (without the commas)
>
Quote:
Quote:
struct
{
char strDateHour[30];
char strName[80];
double value;
} Data[1000];
>
Quote:
Quote:
Please can you explain a simple code to do so ?
>
Quote:
Quote:
Thank-You
>
Quote:
Quote:
Octavio
>
Quote:
Quote:
I just forgot to tell you the file object is a CFile class one.
>
Quote:
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 -
>
Quote:
- 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::CListElem(void)
{
>
}
>
CListElem::~CListElem(void)
{}
>
void CListElem::ReadLogFile()
{
// 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?

  #7  
Old September 25th, 2007, 06:05 PM
Sze
Guest
 
Posts: n/a
Default Re: Code for read a file

On 25 Sep., 19:43, Sze <kamistr...@gmx.dewrote:
Quote:
On 25 Sep., 18:15, Hollywood <freepos...@gmail.comwrote:
>
>
>
Quote:
On 25 sep, 17:09, Sze <kamistr...@gmx.dewrote:
>
Quote:
Quote:
On 25 Sep., 16:15, Hollywood <freepos...@gmail.comwrote:
>
Quote:
Quote:
On 25 sep, 16:05, Hollywood <freepos...@gmail.comwrote:
>
Quote:
Quote:
Hello members of the comp.lang.c++,
>
Quote:
Quote:
My log file is made of a set of 1000 following lines kind:
>
Quote:
Quote:
21/09/07 13:49:56,MW.SET.D_IGLS,2.000000
21/09/07 13:49:56,MW.SET.GNP_NT,7.000000
....
>
Quote:
Quote:
commas ',' are used as field separators (data, name, value).
>
Quote:
Quote:
I'd need a program that read any field into the following structured
table (without the commas)
>
Quote:
Quote:
struct
{
char strDateHour[30];
char strName[80];
double value;
} Data[1000];
>
Quote:
Quote:
Please can you explain a simple code to do so ?
>
Quote:
Quote:
Thank-You
>
Quote:
Quote:
Octavio
>
Quote:
Quote:
I just forgot to tell you the file object is a CFile class one.
>
Quote:
Quote:
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 -
>
Quote:
Quote:
- Afficher le texte des messages précédents -
>
Quote:
Ok, thank-You for your remarks.
Here goes what I've got :
>
Quote:
#include "StdAfx.h"
#include "listelem.h"
>
Quote:
CListElem ObjFile;
>
Quote:
CListElem::CListElem(void)
{
>
Quote:
}
>
Quote:
CListElem::~CListElem(void)
{}
>
Quote:
void CListElem::ReadLogFile()
{
// Open the LogFile
>
Quote:
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
}
>
Quote:
// Reading part
>
Quote:
// while not end of file
// read every character of the Log file and put it
into the structure above
>
Quote:
}
>
Quote:
struct
{
char strDateHour[30];
char strName[80];
double value;
} Data[1000];
>
Quote:
Now the question is how to test the end of a CFile file ? ( a kind of
EndOfFile).
>
Quote:
Many Thanks
>
Quote:
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.

  #8  
Old September 25th, 2007, 11:25 PM
Jim Langston
Guest
 
Posts: n/a
Default Re: Code for read a file

"Hollywood" <freeposte1@gmail.comwrote in message
news:1190729123.791178.79070@19g2000hsx.googlegrou ps.com...
Quote:
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.000000
21/09/07 13:49:56,MW.SET.GNP_NT,7.000000
....
>
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.000000" );
std::stringstream 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;
}


  #9  
Old September 26th, 2007, 07:05 AM
Hollywood
Guest
 
Posts: n/a
Default Re: Code for read a file

On 25 sep, 19:55, Sze <kamistr...@gmx.dewrote:
Quote:
On 25 Sep., 19:43, Sze <kamistr...@gmx.dewrote:
>
>
>
>
>
Quote:
On 25 Sep., 18:15, Hollywood <freepos...@gmail.comwrote:
>
Quote:
Quote:
On 25 sep, 17:09, Sze <kamistr...@gmx.dewrote:
>
Quote:
Quote:
On 25 Sep., 16:15, Hollywood <freepos...@gmail.comwrote:
>
Quote:
Quote:
On 25 sep, 16:05, Hollywood <freepos...@gmail.comwrote:
>
Quote:
Quote:
Hello members of the comp.lang.c++,
>
Quote:
Quote:
My log file is made of a set of 1000 following lines kind:
>
Quote:
Quote:
21/09/07 13:49:56,MW.SET.D_IGLS,2.000000
21/09/07 13:49:56,MW.SET.GNP_NT,7.000000
....
>
Quote:
Quote:
commas ',' are used as field separators (data, name, value).
>
Quote:
Quote:
I'd need a program that read any field into the following structured
table (without the commas)
>
Quote:
Quote:
struct
{
char strDateHour[30];
char strName[80];
double value;
} Data[1000];
>
Quote:
Quote:
Please can you explain a simple code to do so ?
>
Quote:
Quote:
Thank-You
>
Quote:
Quote:
Octavio
>
Quote:
Quote:
I just forgot to tell you the file object is a CFile class one.
>
Quote:
Quote:
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 -
>
Quote:
Quote:
- Afficher le texte des messages précédents -
>
Quote:
Quote:
Ok, thank-You for your remarks.
Here goes what I've got :
>
Quote:
Quote:
#include "StdAfx.h"
#include "listelem.h"
>
Quote:
Quote:
CListElem ObjFile;
>
Quote:
Quote:
CListElem::CListElem(void)
{
>
Quote:
Quote:
}
>
Quote:
Quote:
CListElem::~CListElem(void)
{}
>
Quote:
Quote:
void CListElem::ReadLogFile()
{
// Open the LogFile
>
Quote:
Quote:
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
}
>
Quote:
Quote:
// Reading part
>
Quote:
Quote:
// while not end of file
// read every character of the Log file and put it
into the structure above
>
Quote:
Quote:
}
>
Quote:
Quote:
struct
{
char strDateHour[30];
char strName[80];
double value;
} Data[1000];
>
Quote:
Quote:
Now the question is how to test the end of a CFile file ? ( a kind of
EndOfFile).
>
Quote:
Quote:
Many Thanks
>
Quote:
Quote:
Octavio
>
Quote:
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

  #10  
Old September 26th, 2007, 07:55 AM
Hollywood
Guest
 
Posts: n/a
Default Re: Code for read a file

SNIP
Quote:
>
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.000000" );
std::stringstream 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.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.