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

How to read and write data in C++ (random access)?

151 100+
Hi,

I have one input data.

1200 3
1234 4
1235 6
1236 2

I need write this in binary file in such a way that i can easily write and read the data.

I know using fread and fwrite it is possible to do it. we can access data randomly. But how can i implement it? Any working example will help me.
Nov 9 '12 #1
3 2559
weaknessforcats
9,208 Expert Mod 8TB
Use a struct:

Expand|Select|Wrap|Line Numbers
  1. struct
  2. {
  3.   int data;
  4.   int data1;
  5. };
Load the members and then write the struct in binary to your file. You on't need the separating space on the output.

Read the input using fscanf into an int, a char, and an int. Use the ints to load yout struct.
Nov 9 '12 #2
Man4ish
151 100+
But i want to write data in such a way that 3 should be stored at 1200 loction and like others and i can easily read the data from there also.

Regards
Nov 9 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
In that case use a text file instead of a binary file and be sure to write each record in the file in exactly the same format.

A struct and a binary file will still work since the file would contain nothing but ints that are all the same size. The int with 1200 would be followed by an int with 3 followed by an int with 1234.

Remember, after the file is written there is no way to tell howit was written just from the data in the file. It is up to you to position the data so you can access it randomly later. In this case, do you see the data as 4 characters 1234 or an integer of 1234. The answer to that will determine how to format the file.
Nov 9 '12 #4

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

Similar topics

3
by: Cybertof | last post by:
Hello, Is there a simple way to read a random access file that has been created with VB6 using direct writing to disk of Type....End Type structures ? I have not found this possibility in C#. ...
2
by: carlos | last post by:
How can we read / write numbers in a excel data sheet.
2
by: Eric | last post by:
Hi, I need to send data from a file into a structure. In Vb6 this was done like so. public Type Struct1 v1 as string v2 as string end type
3
by: Bruce | last post by:
I am building a WinForms app that uses Web Services access to a server for most of its data input/output, but I also need to persist some of its data to the local disk (basically as a cache of some...
4
by: Kc-Mass | last post by:
Does anyone have experience reading Act! (contact management software) data into Access. Someone suggested to me that the data is in Foxpro tables, which would seemingly be easy but I don't know...
5
by: kevin | last post by:
Hi, Hope you can help - I can't see the wood for the trees any more. I currently have a local MS Access file. I'd like to do this in VB.NET: connect to file (select * from table) for...
0
by: kalichakradhar | last post by:
hi, I am new to VB programming.. I am writing a automated Tool in Vb which could read/write cell value present in the MPP (project plan 2003).I am successful in opening the project but now i dont...
2
by: shankar2 | last post by:
Hello All, This is my first try at posting a query in this forum, your patience is appreciated. App Details: Access 2000 on Win 2000, Slightly better than a Novice, comfortable with various...
0
by: nallsindian | last post by:
Hi Good morning to all of you, i have a doubt in visual basic and excel connectivity, how to read or write the values in excel, Regards Nallasamy.VP
4
by: knuckels23 | last post by:
Hi All, I have a Random access file which is written using VB 6.0. I need to read this file using C#. The record used in VB to write the Random access file is as follows Type AA aa1 As...
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: 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:
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
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
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,...

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.