473,322 Members | 1,379 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,322 software developers and data experts.

string I/O in binary file

Hi there,

I have an NxK matrix of strings (C++).
I'd like to write it to a binary file
and later read it back to C++ (since the
dimension N is huge, I'd like to save
some time reading it every time).
What's the best way (neat and fast) to do this without converting to C
character
array or create extra storage for the size
of each string?

Thank you for your help.
Jul 22 '05 #1
6 1894
jack wrote:
Hi there,

I have an NxK matrix of strings (C++).
I'd like to write it to a binary file
and later read it back to C++ (since the
dimension N is huge, I'd like to save
some time reading it every time).
What's the best way (neat and fast) to do this without converting to C
character
array or create extra storage for the size
of each string?


Is there something wrong with the line wrap on your news client, or are
you inserting your own line breaks? If so, why? Just let your client
handle it. It will look much nicer (assuming you set a reasonable line
length, like maybe 70 characters).

Basically, your problem is not well-defined. You need to indicate the
bounds of the strings in some way if you expect to be able to find where
one ends and the next begins. Usually this is done with a delimiter
(like in C) or a size. There's no simple way to do it without either of
these.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #2
jack wrote:
Hi there,

I have an NxK matrix of strings (C++).
I'd like to write it to a binary file
and later read it back to C++ (since the
dimension N is huge, I'd like to save
some time reading it every time).
What's the best way (neat and fast) to do this without converting to C
character
array or create extra storage for the size
of each string?


Is there something wrong with the line wrap on your news client, or are
you inserting your own line breaks? If so, why? Just let your client
handle it. It will look much nicer (assuming you set a reasonable line
length, like maybe 70 characters).

Basically, your problem is not well-defined. You need to indicate the
bounds of the strings in some way if you expect to be able to find where
one ends and the next begins. Usually this is done with a delimiter
(like in C) or a size. There's no simple way to do it without either of
these.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #3

"jack" <id***@yahoo.com> wrote in message
news:40**********************@news.rcn.com...
Hi there,

I have an NxK matrix of strings (C++).
I'd like to write it to a binary file
and later read it back to C++ (since the
dimension N is huge, I'd like to save
some time reading it every time).
What's the best way (neat and fast) to do this without converting to C
character
array or create extra storage for the size
of each string?

Thank you for your help.


I don't see what advantage you think a C character array will give you. You
still have the same problem that you are trying to write parts of a large
file where each part is a different size. One way around this is to assume
some fixed maximum size, it certainly makes things simpler.

Another issue that greatly affects how you should do this is whether the
parts of the files might change size, so you read some strings of one size
but then you change the sizes and want to write different size strings to
the same place in the file.

Undoubtedly the fastest and neatest way to do this is to find some third
party library that has already solved the problem for you. This sort of
thing has been done countless times before. You could for instance look at a
B-Tree implementation. A B-Tree is a kind of indexed data structure
specialised to be stored in a file. In your case the index would presumably
be the first index of your matrix.

It's hard to give more specific advice because your problem is only vaguely
specified. Why not post back explaining *what* you are trying to achieve,
not *how* you are trying to achieve it.

john
Jul 22 '05 #4

"jack" <id***@yahoo.com> wrote in message
news:40**********************@news.rcn.com...
Hi there,

I have an NxK matrix of strings (C++).
I'd like to write it to a binary file
and later read it back to C++ (since the
dimension N is huge, I'd like to save
some time reading it every time).
What's the best way (neat and fast) to do this without converting to C
character
array or create extra storage for the size
of each string?

Thank you for your help.


I don't see what advantage you think a C character array will give you. You
still have the same problem that you are trying to write parts of a large
file where each part is a different size. One way around this is to assume
some fixed maximum size, it certainly makes things simpler.

Another issue that greatly affects how you should do this is whether the
parts of the files might change size, so you read some strings of one size
but then you change the sizes and want to write different size strings to
the same place in the file.

Undoubtedly the fastest and neatest way to do this is to find some third
party library that has already solved the problem for you. This sort of
thing has been done countless times before. You could for instance look at a
B-Tree implementation. A B-Tree is a kind of indexed data structure
specialised to be stored in a file. In your case the index would presumably
be the first index of your matrix.

It's hard to give more specific advice because your problem is only vaguely
specified. Why not post back explaining *what* you are trying to achieve,
not *how* you are trying to achieve it.

john
Jul 22 '05 #5
On Tue, 6 Apr 2004 20:20:35 -0400, "jack" <id***@yahoo.com> wrote:
Hi there,

I have an NxK matrix of strings (C++).
I'd like to write it to a binary file
and later read it back to C++ (since the
dimension N is huge, I'd like to save
some time reading it every time).
What's the best way (neat and fast) to do this without converting to C
character
array or create extra storage for the size
of each string?

Thank you for your help.


I have done this several times for different reasons.

Bye the way that some strings may be empty i used seperators like a
csv-file and you nead a simple parser to read this back.
stringvalue-fieldseperator-stringvalue-fieldseperator-stringvalue-rowseperator;
stringvalue-fieldseperator-stringvalue-fieldseperator-fieldseperator-fieldseperator-stringvalue-rowseperator;
stringvalue-fieldseperator-stringvalue-fieldseperator-stringvalue-rowseperator;

Another way is to write the string length bevor the stringvalue. Set
the length of -1 at the end of each row. So you read first the length
and you knows the length of the next string or you must shift in the
next row.

Howie

Jul 22 '05 #6
On Tue, 6 Apr 2004 20:20:35 -0400, "jack" <id***@yahoo.com> wrote:
Hi there,

I have an NxK matrix of strings (C++).
I'd like to write it to a binary file
and later read it back to C++ (since the
dimension N is huge, I'd like to save
some time reading it every time).
What's the best way (neat and fast) to do this without converting to C
character
array or create extra storage for the size
of each string?

Thank you for your help.


I have done this several times for different reasons.

Bye the way that some strings may be empty i used seperators like a
csv-file and you nead a simple parser to read this back.
stringvalue-fieldseperator-stringvalue-fieldseperator-stringvalue-rowseperator;
stringvalue-fieldseperator-stringvalue-fieldseperator-fieldseperator-fieldseperator-stringvalue-rowseperator;
stringvalue-fieldseperator-stringvalue-fieldseperator-stringvalue-rowseperator;

Another way is to write the string length bevor the stringvalue. Set
the length of -1 at the end of each row. So you read first the length
and you knows the length of the next string or you must shift in the
next row.

Howie

Jul 22 '05 #7

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

Similar topics

8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
6
by: Tarun | last post by:
Hi All, I need to find a particular substring in a binary string(some text appended & prepended to binary data). I cant use strstr since it terminates on receiving '\0'which can be there in...
9
by: zjut | last post by:
I want to add a string to the file and the file is sort by letter! for examply: the follow file is a big file ////////////////////// abort black cabbage dog egg fly
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
2
by: Edvin | last post by:
Why is binary array written to a file different than when converting the binary array to string and then writing it to file! For example: // --- Allocate byte array byte arrByte = new byte;...
14
by: Josh Baltzell | last post by:
I am having a lot more trouble with this than I thought I would. Here is what I want to do in pseudocode. Open c:\some.pdf Replace "Replace this" with "Replaced!" Save c:\some_edited.pdf I...
1
by: vinothg | last post by:
Hi , I have a binary file which contains 30,000 strings of 20 bytes each.I need to search for a string in the file to see whether the particular string exists. The sample code which i wrote...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
6
by: fnoppie | last post by:
Hi, I am near to desperation as I have a million things to get a solution for my problem. I have to post a multipart message to a url that consists of a xml file and an binary file (pdf)....
13
by: liujiaping | last post by:
Hi, all. I have a dictionary-like file which has the following format: first 4 column 7 is 9 a 23 word 134 .... Every line has two columns....
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.