Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > File Reading

Reply
Thread Tools

File Reading

 
 
cplusplusquestion@gmail.com
Guest
Posts: n/a
 
      07-07-2008
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?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-07-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.
 
Reply With Quote
 
 
 
 
Brad
Guest
Posts: n/a
 
      07-08-2008
Ian Collins wrote:

> If it's a binary file, how do you know where the next line begins?


You don't... at least not 100%
 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      07-08-2008
On Jul 7, 3:48*pm, Ian Collins <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > 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?
>


I would ask the OP the same question for a text file.
--
Fred Kleinschmidt
 
Reply With Quote
 
Eric Pruneau
Guest
Posts: n/a
 
      07-09-2008

"Ian Collins" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
> (E-Mail Removed) wrote:
>> 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


 
Reply With Quote
 
 
 
Reply

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading of file by next of map file and by next of file descriptor. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 1 07-10-2007 02:46 AM
Reading a file and resuming reading. Karim Ali Python 2 05-25-2007 02:04 PM
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muñoz ASP .Net 3 06-04-2005 10:56 AM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM



Advertisments