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

How to acess hard disk using c

satyanagendra
hi
I want to create a small address book using c language which contains name and phone no. If i use files to save that information how can i search for a given record.So i want to save raw data into hard disk and read it back.
if any body know the solution please reply.
Apr 17 '07 #1
9 5008
gpraghuram
1,275 Expert 1GB
HI,
You have to take a look into the C file operation under the header stdio.h
You need to use calls like fopen,fgets,fprintf and string operations.

Thanks
Raghuram
Apr 17 '07 #2
HI,
You have to take a look into the C file operation under the header stdio.h
You need to use calls like fopen,fgets,fprintf and string operations.

Thanks
Raghuram
hi raghuram,
my actual aim is to create a database using c. is it possible
Apr 17 '07 #3
hi raghuram,
my actual aim is to create a database using c. is it possible
Yes it is possible to create a database in C. You will have to learn to use structs, or with a little more work you can learn to use classes and create a database in C++ (C++ is the super-set of C).

By writing your own C++ class you can associate as much data as you require with an individual entry to your database. You could also write a function to search for an entry based on any search parameter.

An excellent introduction to C++ can be found in the following text "Introducing C++ for scientists, enginneers and mathematicians" by Derek Capper.
Apr 17 '07 #4
A user cannot direct access to hard disk memory, paged memory or others memory. The kernel will assist us if we overwrite a block of memory.
Apr 22 '07 #5
AdrianH
1,251 Expert 1GB
Hi Sat,

My first question is, why do you want to make a database; is this for teaching yourself how to write a programme in C, thus giving you a goal to work towards?

If yes, then you should have some knowledge in writing programmes using the stdio.h library. If not, perhaps you should start out smaller. Write a simple programme that writes to a file, and then write another to read from it. It will give you insight on what you are doing, and what you should not be doing.

If no, then I would suggest that you don't waste your time. There are database programmes out there that are much more robust, unless you really don't want to depend on them for some design reason.

Do you care if it Is portable?

If yes, you may want to think about if size matters ;). Binary representation is smaller but less portable. ASCII is larger, but more portable.

Because different machines have different binary representations for the same thing (google little-endian and big-endian for more information), this could make things more difficult when you have written to your database and then try reading from it on another computer. If they have different endianness and you haven't encoded in the information to state what endianness it contains or you haven't ensured that only one type of endianness is store in the database, you could be looking at a lot of time trying to figure out why the programme works on one computer but not another.

To overcome this problem with less hassle, you can write the file as a text file. When you write it as text, it will be transferable to any computer as this is more portable (though nowadays you have Unicode and ASCII, but this is much easier to deal with since the standard libraries work with ASCII).

I hope this helps you,


Adrian
Apr 22 '07 #6
Hi Sat,

My first question is, why do you want to make a database; is this for teaching yourself how to write a programme in C, thus giving you a goal to work towards?

If yes, then you should have some knowledge in writing programmes using the stdio.h library. If not, perhaps you should start out smaller. Write a simple programme that writes to a file, and then write another to read from it. It will give you insight on what you are doing, and what you should not be doing.

If no, then I would suggest that you don't waste your time. There are database programmes out there that are much more robust, unless you really don't want to depend on them for some design reason.

Do you care if it Is portable?

If yes, you may want to think about if size matters ;). Binary representation is smaller but less portable. ASCII is larger, but more portable.

Because different machines have different binary representations for the same thing (google little-endian and big-endian for more information), this could make things more difficult when you have written to your database and then try reading from it on another computer. If they have different endianness and you haven't encoded in the information to state what endianness it contains or you haven't ensured that only one type of endianness is store in the database, you could be looking at a lot of time trying to figure out why the programme works on one computer but not another.

To overcome this problem with less hassle, you can write the file as a text file. When you write it as text, it will be transferable to any computer as this is more portable (though nowadays you have Unicode and ASCII, but this is much easier to deal with since the standard libraries work with ASCII).

I hope this helps you,


Adrian
hi
I want to know how to communicate with peripherals of the pc using c language and my intension for commnicate with hard disk is I have a data and i want to store it to any memory device and this data is accessable only through my application software.
so i want to know the memory management of the hard disk. I am studying "DISK BASICS" in "LET US C" by yaswant kanithkar for that if u know any thing about my problem please reply.
Apr 25 '07 #7
AdrianH
1,251 Expert 1GB
hi
I want to know how to communicate with peripherals of the pc using c language and my intension for commnicate with hard disk is I have a data and i want to store it to any memory device and this data is accessable only through my application software.
so i want to know the memory management of the hard disk. I am studying "DISK BASICS" in "LET US C" by yaswant kanithkar for that if u know any thing about my problem please reply.
So are you saying that you are trying to access the hardware without the need of the Operating System? I hope not.

I’m not familiar with the publication that you are referring to, though I did look it up on the internet. It appears to be fairly recent (2002 for the third edition), but I don’t know anything more about it.

Since it is using C to write to a peripheral, that would indicate using the low level IO functions such as open(), read(), write(), lseek() and close() (there are others that I will not describe here). When I first read this thread, I think I misunderstood what your intention was, which is to talk to the hardware directly. The function set I am referring to goes though the OS.

So all you want to do is “...create a small address book using c language which contains name and phone no.” saving “...raw data into hard disk and read it back.” This is definitely doable using the functions you describe. I am just reminding you again that that the resulting data file would probably not be portable across different CPU platforms, though it would probably be portable across the same CPU family (Intel/Motorola).

That said, manipulating the contents of a file is fairly easy. You go though the same steps each time:
  1. open the file using the open() function
  2. seek to the location you wish to read from/write to using lseek() (this is optional as multiple reads/writes will be done in a sequential fashion).
  3. read from/write to the file using the read()/write() functions
  4. close the file using the close() function when you are done writing to the file.

When you use the read()/write() functions, you have to pass the address of the location in memory to copy to/from. Since all pointers (except pointer chains, i.e. a pointer to a pointer to a type) will autocast to a void pointer, you don’t need to do any explicit casting when passing an address. You will also have to pass the size of the memory size you are going to fill. E.g.:
Expand|Select|Wrap|Line Numbers
  1. struct myStruct {
  2.   int foo;
  3.   char bar[65];
  4. };
  5.  
  6. /* C99 standard */
  7. struct myStruct stuff = {
  8.   .foo = 9,
  9.   .bar = “Hello there”
  10. };
  11.  
  12. /* pre C99 standard */
  13. struct myStruct stuff = {
  14.   9,
  15.   “Hello there”
  16. };
  17.  
  18. int fileDescriptor = open(“datafile.dat”, O_WRONLY);
  19. write(fileDescriptor, stuff, sizeof(stuff));
  20. close(fileDescriptor);
  21.  
Reading from the file is similar and I leave as an exercise for you. If you want to move to some record and everything in your file is all myStruct structs, then use seek to move to the appropriate location. i.e.
Expand|Select|Wrap|Line Numbers
  1. lseek(fileDescriptor, index * sizeof(sturct myStruct), SEEK_SET);
Now, one thing about using this set of functions is that it is unbuffered. This means that if you ask for some data located in and around the data that you have asked for before in a file multiple times, it is not very efficient. This is due to disk IO being a block device. I’m sure that the book you referred to will tell you more on that and what it means. It is generally more efficient to use the buffered stdio functions then the lower level IO functions for this reason.

Hope this helps.


Adrian
Apr 26 '07 #8
So are you saying that you are trying to access the hardware without the need of the Operating System? I hope not.

I’m not familiar with the publication that you are referring to, though I did look it up on the internet. It appears to be fairly recent (2002 for the third edition), but I don’t know anything more about it.

Since it is using C to write to a peripheral, that would indicate using the low level IO functions such as open(), read(), write(), lseek() and close() (there are others that I will not describe here). When I first read this thread, I think I misunderstood what your intention was, which is to talk to the hardware directly. The function set I am referring to goes though the OS.

So all you want to do is “...create a small address book using c language which contains name and phone no.” saving “...raw data into hard disk and read it back.” This is definitely doable using the functions you describe. I am just reminding you again that that the resulting data file would probably not be portable across different CPU platforms, though it would probably be portable across the same CPU family (Intel/Motorola).

That said, manipulating the contents of a file is fairly easy. You go though the same steps each time:
  1. open the file using the open() function
  2. seek to the location you wish to read from/write to using lseek() (this is optional as multiple reads/writes will be done in a sequential fashion).
  3. read from/write to the file using the read()/write() functions
  4. close the file using the close() function when you are done writing to the file.

When you use the read()/write() functions, you have to pass the address of the location in memory to copy to/from. Since all pointers (except pointer chains, i.e. a pointer to a pointer to a type) will autocast to a void pointer, you don’t need to do any explicit casting when passing an address. You will also have to pass the size of the memory size you are going to fill. E.g.:
Expand|Select|Wrap|Line Numbers
  1. struct myStruct {
  2.   int foo;
  3.   char bar[65];
  4. };
  5.  
  6. /* C99 standard */
  7. struct myStruct stuff = {
  8.   .foo = 9,
  9.   .bar = “Hello there”
  10. };
  11.  
  12. /* pre C99 standard */
  13. struct myStruct stuff = {
  14.   9,
  15.   “Hello there”
  16. };
  17.  
  18. int fileDescriptor = open(“datafile.dat”, O_WRONLY);
  19. write(fileDescriptor, stuff, sizeof(stuff));
  20. close(fileDescriptor);
  21.  
Reading from the file is similar and I leave as an exercise for you. If you want to move to some record and everything in your file is all myStruct structs, then use seek to move to the appropriate location. i.e.
Expand|Select|Wrap|Line Numbers
  1. lseek(fileDescriptor, index * sizeof(sturct myStruct), SEEK_SET);
Now, one thing about using this set of functions is that it is unbuffered. This means that if you ask for some data located in and around the data that you have asked for before in a file multiple times, it is not very efficient. This is due to disk IO being a block device. I’m sure that the book you referred to will tell you more on that and what it means. It is generally more efficient to use the buffered stdio functions then the lower level IO functions for this reason.

Hope this helps.


Adrian

Thank u for reply i will follow the example.
May 5 '07 #9
AdrianH
1,251 Expert 1GB
Thank u for reply i will follow the example.
No prob. Good luck.


Adrian
May 5 '07 #10

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

Similar topics

2
by: AlirezaH | last post by:
How to measure hard disk capacity?
3
by: Flix | last post by:
I need to detect the root directories of the installed hard disks (es: C:, D:, E:, etc.). I'm not interested in cd drives. I know that there is a way (a bit slow, if I remeber) to retrive all the...
1
by: Shrirang Ballal | last post by:
I am doing a project which requires to read the raw bytes from hard disk. It rrequires to access the partition table and then the individual partitions. I want some material related to these...
16
by: Otie | last post by:
Hi, Is there a way for VB5 to determine exactly where on a hard drive a .exe file is stored upon the .exe file's first copying to the hard drive? What I need to know is the exact hard drive...
13
by: ragtag99 | last post by:
I posted this on comp.lang.asm.x86, alt.os.development, comp.arch, comp.lang.c++ Im working with windows xp professional, NTFS and programming with MASM, c++ (free compiler) or visual basic 6.0...
6
by: Paul Bromley | last post by:
Ok - I have given up on trying to find the active IP address for a given PC. For licensing purposes I need to retrive a unique identifier from the PC that the program is installed on. The Hard disk...
3
by: ary | last post by:
I try to create a weblog host site! in this case i can't use cache for every page because that cause to be my Server ram full of caching page. but if I can save cache in hard disk my problem...
2
by: =?Utf-8?B?R2VvcmR5?= | last post by:
Hello everyone, I would really appreciate if someone helped me in this matter cause I am going to lose my mind... I am using a Sony Vaio Laptop with Windows XP professional (512MB Ram, 1,7 GHz CPU...
3
by: trahul87 | last post by:
Hi, I have a Toshiba Satellite A25 - S207. The internal hard disk has just crashed. I do not want to change the hard disk since its very expensive. So, is it possible to boot using an external...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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
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
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...

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.