473,396 Members | 2,129 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,396 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 1598
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.