473,406 Members | 2,371 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,406 software developers and data experts.

Writing raw data on disk.

78
Hi guys.

I am messing around with virtual file systems lately, but i have a problem.
I want to save a structure on the virtual disk as raw data. How can i do this? Should i convert each "member" inside the struct to char?
like: "encrypt" and "decript" thingy?
Note: I'm writing char[] buffer to the disk!

Thanks!
Jun 6 '07 #1
13 5853
weaknessforcats
9,208 Expert Mod 8TB
Yes, the file stream is a char file stream. You may also want field delimiters to you can read the data back in.

Expand|Select|Wrap|Line Numbers
  1. struct Date
  2.         {
  3.             int month;
  4.             int day;
  5.             int year;
  6.         };
  7.         Date dt;
  8.         dt.month = 7;
  9.         dt.day = 4;
  10.         dt.year = 1776;
  11.         fstream output("c:\\scratch\\instructor\\testdata.txt", ios::out);
  12.         stringstream  ss;
  13.         ss << dt.month << '|' << dt.day << '|' << dt.year << '\n';
  14.         string buffer;
  15.         ss >> buffer;
  16.         output.write(buffer.c_str(), buffer.size());
  17.  
Jun 6 '07 #2
Savage
1,764 Expert 1GB
Yes, the file stream is a char file stream. You may also want field delimiters to you can read the data back in.

Expand|Select|Wrap|Line Numbers
  1. struct Date
  2.         {
  3.             int month;
  4.             int day;
  5.             int year;
  6.         };
  7.         Date dt;
  8.         dt.month = 7;
  9.         dt.day = 4;
  10.         dt.year = 1776;
  11.         fstream output("c:\\scratch\\instructor\\testdata.txt", ios::out);
  12.         stringstream  ss;
  13.         ss << dt.month << '|' << dt.day << '|' << dt.year << '\n';
  14.         string buffer;
  15.         ss >> buffer;
  16.         output.write(buffer.c_str(), buffer.size());
  17.  
If he will be using >> to extract data,I think that whitespace can serve as a delimiter.
Jun 6 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
If he will be using >> to extract data,I think that whitespace can serve as a delimiter.
That's an if.

And it's another if that the data does not contain whitespace.

However, depending upon the answers to those two if's, then you may be correct.
Jun 6 '07 #4
Sebouh
78
I'm really sorry, but i forgot to mention a key factor. I'm working in C.

P.S. The problem i want to avoid is, if the data structure is 512 bytes, i want to write 512 bytes to disk. Converting short types to characters will occupy more space!
Jun 7 '07 #5
Sebouh
78
Ok, i've been thinking really hard (it hurts too). I can solve this if i find a method to convert a 2 byte short int to a 2 byte char.
Any chance i can read a byte from the short?
Thanks!
Jun 7 '07 #6
Savage
1,764 Expert 1GB
I'm really sorry, but i forgot to mention a key factor. I'm working in C.

P.S. The problem i want to avoid is, if the data structure is 512 bytes, i want to write 512 bytes to disk. Converting short types to characters will occupy more space!
use itoa.It converts numbers to string with using bases,like 10 for decimal and so on.

Savage
Jun 7 '07 #7
AdrianH
1,251 Expert 1GB
Ok, i've been thinking really hard (it hurts too). I can solve this if i find a method to convert a 2 byte short int to a 2 byte char.
Any chance i can read a byte from the short?
Thanks!
What you just asked is impossible. A char is one byte long and a short is two.

You want to write binary data to a file? That's easy. Cast the struct instance's address to a void*.

Actually, in C you don't even need to cast since it uses a weak type system you can assign any pointer type to void * and void * to any pointer type. Since fread and fwrite take a void * for the buffer, you just plop the address of the struct and use sizeof(youStruct) as the size.

BUT BE WARNED, this file you create will not be portable across different CPU platforms. If you don't care, go right ahead.


Adrian
Jun 7 '07 #8
Sebouh
78
Well my implementation writes and reads blocks of data, so i use char* buffers to read and write. Making itvoid* wouldn't work would it?

But, i've found a way to do it. I've created two functions, struct_to_string and string_to_struct. The first converts the structure to a char*. The structure only contains characters and shorts, so the problem is with shorts. What i did is store the first 8 bits of short in 1 char and the last 8 bits inthe other.Then the string_to_struct reforms the structure, and in case of shorts, it recreates them using binary operations.

It cost me precious time (because i have a deadline...) butit works like charm!

Thanks for the help anyway guys!:)
Jun 7 '07 #9
Savage
1,764 Expert 1GB
Well my implementation writes and reads blocks of data, so i use char* buffers to read and write. Making itvoid* wouldn't work would it?

But, i've found a way to do it. I've created two functions, struct_to_string and string_to_struct. The first converts the structure to a char*. The structure only contains characters and shorts, so the problem is with shorts. What i did is store the first 8 bits of short in 1 char and the last 8 bits inthe other.Then the string_to_struct reforms the structure, and in case of shorts, it recreates them using binary operations.

It cost me precious time (because i have a deadline...) butit works like charm!

Thanks for the help anyway guys!:)
Intresting,we are glad that u have solved it.

Savage
Jun 7 '07 #10
AdrianH
1,251 Expert 1GB
Well my implementation writes and reads blocks of data, so i use char* buffers to read and write. Making itvoid* wouldn't work would it?

But, i've found a way to do it. I've created two functions, struct_to_string and string_to_struct. The first converts the structure to a char*. The structure only contains characters and shorts, so the problem is with shorts. What i did is store the first 8 bits of short in 1 char and the last 8 bits inthe other.Then the string_to_struct reforms the structure, and in case of shorts, it recreates them using binary operations.

It cost me precious time (because i have a deadline...) butit works like charm!

Thanks for the help anyway guys!:)
Expand|Select|Wrap|Line Numbers
  1. struct A
  2. {
  3.   char aString[6];
  4.   short aShort;
  5. };
  6.  
  7. struct A a = { "Hello", 4 };
  8. fwrite(&a, sizeof(struct A), 1, outfile);
  9.  
  10. struct A a;
  11. fread(&a, sizeof(struct A), 1, infile);
I didn't include opening the respective files. I'll leave that to you.

Basicly, what you did is the same thing, with one major difference. Yours is done so that you can change the implementation of how it reads and write it in the appropriate functions. With mine your stuck esp if you are reading/writing in several locations, unless you wrap these up in appropriate functions that take a FILE* parameter.

Yours is more flexable, which is definatly good as you can potentially make it endian compatiable across CPUs.


Adrian
Jun 7 '07 #11
Sebouh
78
Hmm, interesting. I didn't know you could write a void* using fwrite.
Glad you showed me :)
Jun 8 '07 #12
AdrianH
1,251 Expert 1GB
Hmm, interesting. I didn't know you could write a void* using fwrite.
I'm not writing a void*, I'm writing a struct A instance.
Glad you showed me :)
No problem.


Adrian
Jun 8 '07 #13
AdrianH
1,251 Expert 1GB
Actually, in C you don't even need to cast since it uses a weak type system you can assign any pointer type to void * and void * to any pointer type. Since fread and fwrite take a void * for the buffer, you just plop the address of the struct and use sizeof(youStruct) as the size.
In C++ this goes only one way, almost anything to a void *. When going from a void * to anything, you require a reinterpret cast.


Adrian
Jun 8 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
8
by: Lu | last post by:
Hi there, I got a program to write data to a randomly accessed file (the program moves file pointer to a certain position of the file according the current "keyword" and then writes data). It...
4
by: Igor Shulgin | last post by:
Hi! What is standard and direct way (within Oracle 9.2 stored procedures) for writing binary data from Oracle to external file on disk? We have tried to use UTL_FILE package, but it operates on...
5
by: Ben Jeurissen | last post by:
Hello, I have to deal with the following issue in C++: Two threads are started from the main thread, both capturing images from a different firewire camera. Both threads take shots of 460800...
3
by: ishekar | last post by:
Hi, I have an application where i want to write data to a file, the data is being sent from an external source. I know the total size of the data and then i retrieve the data in small segments...
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
16
by: Ali | last post by:
Hi I want to write (or read) to a stream, but the data is not byte array I converted the data to byte array manually, but it is very slow, (becuse the data is very large) Is another way for this...
12
by: Chris Springer | last post by:
I'd like to get some feedback on the issue of storing data out to disk and where to store it. I've never been in a production environment in programming so you'll have to bear with me... My...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...
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,...

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.