473,763 Members | 9,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1616
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.co mwrote:
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******@hotma il.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.b in", ios_base::in | ios_base::binar y);

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

while(!isNumeri c(tmp))
{
ifs.get(tmp);
if(ifs)
linea.push_back (tmp);
else
break;
}
int bpos = linea.rfind('b' );
lineb.assign(li nea,bpos,linea. size()-bpos);
linea.erase(lin ea.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(lin eb.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
3066
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
10373
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 text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly find the number of characters available by: |
4
9842
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 = tmpfile(); /* write into tmpFile */ ...
0
3940
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
7
6063
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
1
64189
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 article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
6
248991
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 System; using System.IO; using System.Diagnostics;
2
3702
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? In the Makefile can I just add a "-g" flag to have the binary produced with debugging symbols? The source is written in ANSI C. This is what I have now: "CC = gcc" The client binary is 433680 and the core file produced when it crashed
1
2672
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
10448
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 set of integers stored in an array.It would be a great help if you could provide some code for it.I tried the function fscanf but by that I am able to read only the first integer of the text file.Please help me.
0
9387
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9823
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.