Code for read a file

H

Hollywood

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
 
H

Hollywood

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

Sze

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 ?

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

Guest

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

Hollywood

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.

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
 
S

Sze

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

I am not sure where this CFile class belongs to, could you please
enlighten me, so I could help you, hopefully?
 
S

Sze

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



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

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

Jim Langston

Hollywood said:
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;
}
 
H

Hollywood

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

Hollywood

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

Tim Slattery

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.

This question is off-topic here, since this group discusses standard
C++, not vendor-specific libraries like MFC. You'll find lots of
people who know exactly what you're talking about at
microsoft.public.vc.mfc.

FWIW, CFile is fully documented at
http://msdn2.microsoft.com/en-us/library/60fh2b6f(VS.71).aspx

It looks to me like you could tell CFile::Read to read one byte at a
time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,052
Latest member
EvaW192252

Latest Threads

Top