473,412 Members | 5,361 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

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?
Jul 7 '08 #1
4 1599
cp***************@gmail.com 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.
Jul 7 '08 #2
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% :)
Jul 8 '08 #3
On Jul 7, 3:48*pm, Ian Collins <ian-n...@hotmail.comwrote:
cplusplusquest...@gmail.com 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
Jul 8 '08 #4

"Ian Collins" <ia******@hotmail.coma écrit dans le message de news:
6d************@mid.individual.net...
cp***************@gmail.com 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
Jul 9 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
6
Atran
by: Atran | last post by:
Hello: In this article: You will learn to Write or Read A Text File. Let's Begin: First Create a new project (ConsoleApp or WinApp). And Make sure your program uses these namespaces: using...
2
by: Zach | last post by:
I compiled a game client and it crashed (segmentation fault) resulting in a core file being generated. I'm trying to find out exactly what caused it to crash. Any ideas how I can do this with gdb?...
1
by: dwaterpolo | last post by:
Hi Everyone, I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt and compare them, the first text file has a bunch of values listed like: y y y y y y y
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.