Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 7th, 2008, 11:45 PM
cplusplusquestion@gmail.com
Guest
 
Posts: n/a
Default File Reading

I have a binary file like:

a bla bla bla bla
b 12 23 34 80 38

a bla bla bla bla
b 23 45 89 30 99

When I find first character of the line is 'a', I would like to skip
reading this line, then go to next line. Any suggestion?
  #2  
Old July 7th, 2008, 11:55 PM
Ian Collins
Guest
 
Posts: n/a
Default Re: File Reading

cplusplusquestion@gmail.com wrote:
Quote:
I have a binary file like:
>
a bla bla bla bla
b 12 23 34 80 38
>
a bla bla bla bla
b 23 45 89 30 99
>
When I find first character of the line is 'a', I would like to skip
reading this line, then go to next line. Any suggestion?
If it's a binary file, how do you know where the next line begins?

--
Ian Collins.
  #3  
Old July 8th, 2008, 01:15 AM
Brad
Guest
 
Posts: n/a
Default Re: File Reading

Ian Collins wrote:
Quote:
If it's a binary file, how do you know where the next line begins?
You don't... at least not 100% :)
  #4  
Old July 8th, 2008, 07:26 PM
Fred
Guest
 
Posts: n/a
Default Re: File Reading

On Jul 7, 3:48*pm, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
cplusplusquest...@gmail.com wrote:
Quote:
I have a binary file like:
>
Quote:
a bla bla bla bla
b 12 23 34 80 38
>
Quote:
a bla bla bla bla
b 23 45 89 30 99
>
Quote:
When I find first character of the line is 'a', I would like to skip
reading this line, then go to next line. *Any suggestion?
>
If it's a binary file, how do you know where the next line begins?
>
I would ask the OP the same question for a text file.
--
Fred Kleinschmidt
  #5  
Old July 9th, 2008, 04:05 AM
Eric Pruneau
Guest
 
Posts: n/a
Default Re: File Reading


"Ian Collins" <ian-news@hotmail.coma écrit dans le message de news:
6dfkr9F22jb8U5@mid.individual.net...
Quote:
cplusplusquestion@gmail.com wrote:
Quote:
>I have a binary file like:
>>
>a bla bla bla bla
>b 12 23 34 80 38
>>
>a bla bla bla bla
>b 23 45 89 30 99
>>
>When I find first character of the line is 'a', I would like to skip
>reading this line, then go to next line. Any suggestion?
>
If it's a binary file, how do you know where the next line begins?
>
--
Ian Collins.
From what I see, you can do:

1. read a char until you get a number
2.read a char until you get a letter

do this steps util the end of file

here a way to do this (with little error checking), you can test it with a
bin file like this one
a bla bla bla blab 12 23 34 80 38a bla bla bla blab 23 45 89 30 99

it can be on a single line, it doesn't matter.



bool isNumeric(char tmp)
{
return tmp >= 48 && tmp <= 57; // from ascii table
}

bool isLetter(char tmp)
{
// from ascii table
return (tmp >= 65 && tmp <= 90) || // A-Z
(tmp >= 97 && tmp <= 122); // a-z
}

int main()
{
std::ifstream ifs("FileName.bin", ios_base::in | ios_base::binary);

string linea , lineb;
while(ifs)
{
// now read a line and b line
char tmp=0;

while(!isNumeric(tmp))
{
ifs.get(tmp);
if(ifs)
linea.push_back(tmp);
else
break;
}
int bpos = linea.rfind('b');
lineb.assign(linea,bpos,linea.size()-bpos);
linea.erase(linea.begin()+bpos, linea.end());
// now you do your stuff with linea

while(!isLetter(tmp))
{
ifs.get(tmp);
if(ifs)
lineb.push_back(tmp);
else
break;
}
// ok I assume the data in linea has been extracted and stored elsewhere
so I can clear the string
linea.clear();
size_t apos = lineb.rfind('a');
if(ifs)
{
linea.push_back('a');
lineb.erase(lineb.begin()+apos, lineb.end());
}
// now extract info from lineb ....
lineb.clear();
}

return 0;
}


--------------------

Eric Pruneau


 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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 network members.
Post your question now . . .
It's fast and it's free

Popular Articles