Reading data from a tab delimited text
Hi,
i would like to read data from a tab delimited txt file:in this file there are 7 columns,each representing a physical quantity.
After this reading operation i want to fill some arrays in order to plot the various quantities.
Can you give me some advice?
Thanks in advance!
Re: Reading data from a tab delimited text
You can read the file line by line and then tokenize the line on a tab-character to split it.
Re: Reading data from a tab delimited text
if your file is tab delimited all the way, then you can read it like that:
Code:
FILE *fp = ::fopen( "c:\\file.txt" ,"r");
if (!fp)
return;
while ( fp && feof(fp) == 0 )
{
int a=0,b=0,c=0,d=0,e=0,f=0,g=0;
::fscanf(fp,"%d\t%d\t%d\t%d\t%d\t%d\t%d\n",&a,&b,&c,&d,&e,&f,&g);
}
::fclose(fp);
just make sure how the data types are stored...in my code i assume that all file is contains int values! if you have different data types you should change the fscanf.
Cheers
Re: Reading data from a tab delimited text
I'd go for Marc's solution as it is more flexible. and, to make lifemuch easier I would take boost lib and use the tokenizer class there.
Re: Reading data from a tab delimited text
Quote:
Originally Posted by Marc G
You can read the file line by line and then tokenize the line on a tab-character to split it.
How can i read the file line by line?
What do you mean with "tokenize"?
Re: Reading data from a tab delimited text
1) If there are always 7 fields, you can read as follows (fields
are separated by any whitespaces). Sample below has fields
of different types:
Code:
#include <fstream>
int main()
{
int i1,i2,i3,i4;
float f1,f2;
double d1;
std::ifstream in("your_file.txt");
while (in >> i1 >> i2 >> f1 >> d1 >> i3 >> i4 >> f2)
{
// do whatever you want
}
return 0;
}
2) if number of fields is unknown, but all of the same type, you can read
as follows (again, separated by any whitespaces):
Code:
#include <fstream>
#include <sstream>
#include <vector>
int main()
{
std::ifstream in("your_file.txt");
std::string line;
while (std::getline(in,line))
{
std::vector<int> v;
std::stringstream ss(line);
int ival;
while (in >> ival)
v.push_back(ival);
// do whatever you want with v[0] ... v[v.size()-1]
}
return 0;
}
Re: Reading data from a tab delimited text
Quote:
Originally Posted by Fraben
How can i read the file line by line?
Use std::getline, see the second example by Philip Nicoletti above.
Quote:
Originally Posted by Fraben
What do you mean with "tokenize"?
Tokenize means to split a string up by splitting it on a certain character, in your case a tab-character. In your case you can easily tokenize it like Philip Nicoletti said by using the >> operator. Note: this will split your string on any white-space, not just a tab-character.
Re: Reading data from a tab delimited text
Thanks for your precious help!
2 more questions:
1)The text file i'm reading contains three initial lines:an empty line,an header (which explains which data is represented below),and a "delimiter" realized with some "-" (i.e. somewhat like ----------------).
How can i exclude these lines?They force the while to stop immediately!
2)In the code you've sent to me i've seen you have used std::ifstream.Can i use CFile instead?
Re: Reading data from a tab delimited text
Quote:
Originally Posted by Fraben
1)The text file i'm reading contains three initial lines:an empty line,an header (which explains which data is represented below),and a "delimiter" realized with some "-" (i.e. somewhat like ----------------).
How can i exclude these lines?They force the while to stop immediately!
If it are exactly 3 lines, you can call std::getline 3 times to skip those lines.
Re: Reading data from a tab delimited text
Thanks Marc,it works perfectly!
But what can i do to check the line before reading it?
I mean:suppose that there's a line with only one string of text,which i want to ignore,and i don't know where it is.
Is there a way to exclude it,in order to let the "while" instruction work correctly,reading till the end of the file?
Re: Reading data from a tab delimited text
Quote:
Originally Posted by Fraben
But what can i do to check the line before reading it?
I mean:suppose that there's a line with only one string of text,which i want to ignore,and i don't know where it is.
Is there a way to exclude it,in order to let the "while" instruction work correctly,reading till the end of the file?
well you dont need to check line before reading it. you should read the line check if you want to ignore the data if yes continue else process the data.
look at this pesudo code:
Code:
while ( !END_OF_FILE)
{
ReadLine( Line );
// check the Line data
if ( dont want Line )
continue;
// here process the Line and do whatever you want
}
Cheers
Re: Reading data from a tab delimited text
golanshahar: i agree with you,but how can i do what you've suggested?
I don't know much about file handling(well,i don't know much about visual c++...,i'm a rookie!),and i'm trying to use the code sent by Philip Nicoletti.
Re: Reading data from a tab delimited text
Quote:
Originally Posted by Fraben
golanshahar: i agree with you,but how can i do what you've suggested?
I don't know much about file handling(well,i don't know much about visual c++...,i'm a rookie!),and i'm trying to use the code sent by Philip Nicoletti.
well if you using Philip code then its simple:
Code:
std::ifstream in("your_file.txt");
std::string line;
while (std::getline(in,line))
{
// here you have line
// check the line if it something that you want or not i dont know
// what is your cases to "Want" a line or not.
if ( line == "something i dont want")
continue;
std::vector<int> v;
std::stringstream ss(line);
int ival;
while (in >> ival)
v.push_back(ival);
// do whatever you want with v[0] ... v[v.size()-1]
}
Philip i hope its ok i used your code ;)
hope its help...
Cheers
Re: Reading data from a tab delimited text
Hi,
i've used this code
Code:
void CApiarioDlg::OnLoadData()
{
char data[8],ora[8];
double TempMis,TempSet,Umid,Press,Weight;
CString strHour,strMinute,strDate,strForTitle;
long lHour,lMinute;
if(!g_pLoadGraph)
g_pLoadGraph=new CLoadGraph(this);
char strFilter[]={"TXT Files (*.txt)|*.txt|All Files (*.*)|*.*||"};
CFileDialog fdlgChoose(TRUE,".txt",NULL,OFN_READONLY,strFilter);
// CFileDialog filedlgFileChoose(TRUE,".wav");
if(fdlgChoose.DoModal()==IDOK)
{
std::ifstream in(fdlgChoose.GetPathName());
std::string line;
for(int i=1;i<=5;i++)
std::getline(in,line);
while(in>>data>>ora>>TempMis>>TempSet>>Umid
>>Press>>Weight)
{
g_pLoadGraph->m_LoadMultiGraph.SetLoadingData(TRUE);
strHour=ora;
strHour=strHour.Left(2);
lHour=atol(strHour);
strMinute=ora;
strMinute=strMinute.Mid(3,2);
lMinute=atol(strMinute);
g_pLoadGraph->m_LoadMultiGraph.InsertData5(TempMis,TempSet,Umid,Press,Weight,
lHour,lMinute);
g_pLoadGraph->m_LoadMultiGraph.DrawNow();
}
strDate=data;
}
}
But during the debugging, i've noticed that the data variable,which i know is "11/03/06" in the file i open,is read as:
Code:
- data 0x00120f6c ""
[0] 0 ''
[1] 49 '1'
[2] 47 '/'
[3] 48 '0'
[4] 51 '3'
[5] 47 '/'
[6] 48 '0'
[7] 54 '6'
As you can see,the first character read is a null character.Why?How can i fix it?
Re: Reading data from a tab delimited text
change ur for loop like this
Code:
for(int i=0;i<=5;i++)
{
}