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

Low Level File I/0

emaghero
Greetings and Salutations,

I am currently doing some file I/0 on data that I have and I do not wish to use fstream. I have data stored in its untranslated binary format and I want to read it into another program. I have been using _open and _write to export the data but I have been having problems implementing _read.

I will give a simple example of what I want to do.

The following code creates a file and stores it in some location. The file is in binary format and is open for reading and writing.
Expand|Select|Wrap|Line Numbers
  1.     int data;
  2.     int file=_open("g:\\testfile.rob",_O_BINARY|_O_WRONLY|_O_CREAT|_O_TRUNC,_S_IREAD|_S_IWRITE);
  3.  
  4.     if(file==-1){
  5.         perror("Failed to open file\n");
  6.     }
  7.     else{
  8.         cout<<"file is open\n";
  9.     }
  10.  
  11.     char *header="Test File";
  12.  
  13.     data=_write(file,header,(unsigned int)(strlen(header)));
  14.  
  15.     double arr[20];
  16.  
  17.     srand((unsigned)time(NULL));//Seed the random numbers
  18.  
  19.     //Create an array of random numbers
  20.     for(int i=0;i<20;i++){
  21.         arr[i]=rand()%100;//Create an array of random numbers
  22.     }
  23.  
  24.     //Send these random numbers to the output file
  25.     double val;
  26.     for(int i=0;i<20;i++){
  27.         val=arr[i];
  28.         data=_write(file,(void *)&val,sizeof(double));
  29.     }
  30.  
  31.     _close(file);
  32.  
This works fine once the following have been included
Expand|Select|Wrap|Line Numbers
  1. #include <fcntl.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <io.h>
  5. #include <stdio.h>
  6.  
I have been trying to read the data back from the file using the
Expand|Select|Wrap|Line Numbers
  1. _read("file desciptor", "buffer", "Number of bytes");
  2.  
function. This function is supposed to take a file, similar to the one used above, read in Number of bytes data to the buffer and the return the number of bytes that were read.

Here is what I have attempted to do.

Expand|Select|Wrap|Line Numbers
  1. int data;
  2.     int file=_open("g:\\testfile.rob",_O_BINARY|_O_WRONLY|_O_CREAT|_O_TRUNC,_S_IREAD|_S_IWRITE);
  3.  
  4.     if(file==-1){
  5.         perror("Failed to open file\n");
  6.     }
  7.     else{
  8.         cout<<"file is open\n";
  9.     }
  10.  
  11.     char *header="Test File";
  12.  
  13.     data=_write(file,header,(unsigned int)(strlen(header)));
  14.  
  15.     double arr[20];
  16.  
  17.     srand((unsigned)time(NULL));//Seed the random numbers
  18.  
  19.     //Create an array of random numbers
  20.     for(int i=0;i<20;i++){
  21.         arr[i]=rand()%100;//Create an array of random numbers
  22.     }
  23.  
  24.     //Send these random numbers to the output file
  25.     double val;
  26.     for(int i=0;i<20;i++){
  27.         val=arr[i];
  28.         data=_write(file,(void *)&val,sizeof(double));
  29.     }
  30.  
  31.     char BUF[1024];
  32.  
  33.     int testvar;
  34.  
  35.     unsigned int num=300;
  36.  
  37.     testvar=_read(file,(void *)&BUF,num);
  38.  
  39.     cout<<testvar<<endl;
  40.  
  41.     cout<<BUF<<endl;
  42.  
  43.    _close(file);
  44.  
Upon output the value of testvar is -1, which indicates an error, and the output of BUF is gibberish.

Can anybody suggest a possible remedy.

Any help will be greatly appreciated.
Aug 23 '07 #1
5 2386
I have found out why I was getting a return of -1.

Basically I should not have tried to read from the fiel while it was still open.

The following code will produce error free output.

Expand|Select|Wrap|Line Numbers
  1. int data;
  2.     int file=_open("g:\\testfile.rob",_O_BINARY|_O_WRONLY|_O_CREAT|_O_TRUNC,_S_IREAD|_S_IWRITE);
  3.  
  4.     if(file==-1){
  5.         perror("Failed to open file\n");
  6.     }
  7.     else{
  8.         cout<<"file is open\n";
  9.     }
  10.  
  11.     char *header="Test File";
  12.  
  13.     data=_write(file,header,(unsigned int)(strlen(header)));
  14.  
  15.     double arr[20];
  16.  
  17.     srand((unsigned)time(NULL));
  18.  
  19.     //Create an array of random numbers
  20.     for(int i=0;i<20;i++){
  21.         arr[i]=rand()%100;
  22.     }
  23.     //Send these random numbers to the output file
  24.     double val;
  25.     for(int i=0;i<20;i++){
  26.         val=arr[i];
  27.         data=_write(file,(void *)&val,sizeof(double));
  28.     }
  29.  
  30.     _close(file); //Note the closing of the file here and the opening of a new file below
  31.  
  32.     int newfile=_open("g:\\testfile.rob",_S_IREAD);
  33.  
  34.     char BUF[1024];
  35.  
  36.     int testvar;
  37.  
  38.     unsigned int num=600;
  39.  
  40.     testvar=_read(newfile,(void *)&BUF,num);
  41.  
  42.     cout<<testvar<<endl;
  43.  
  44.     cout<<BUF<<endl;
  45.  
  46.     _close(newfile);
  47.  
The output from this code is 169, the number of bytes read from the file, and "Test Header", the head that was assigned at the start.

Can anybody tell me how to extract the numbers that are stored in the file.
This being fairly important for a numerical application.
Aug 23 '07 #2
xoinki
110 100+
hi,
you can use ascii range for detection..
read everything character by character then check whether it is within the range if it is then it is a number else ignore and continue till EOF.
Regards
Xoinki
Aug 23 '07 #3
RRick
463 Expert 256MB
The ascii technique is not going to work because the numbers are stored as binary. No ascii conversion was made when the numbers were written to the file.

Instead, you're going to have to treat the buffer as a pointer to doubles. If the index where the double data begins is dInd, then the following will allow you to view buffer as doubles.
Expand|Select|Wrap|Line Numbers
  1. double * dPtr = (double *) & buffer[ dInd];
  2. cout << dPtr[0] << etc.
  3.  
Reading and writing binary data this way is very platform dependent. There are issues like big/little endian byte ordering. Also, the format and precision of the double can change between systems and even compilers.

Becareful how you use this.
Aug 23 '07 #4
The ascii technique is not going to work because the numbers are stored as binary. No ascii conversion was made when the numbers were written to the file.

Instead, you're going to have to treat the buffer as a pointer to doubles. If the index where the double data begins is dInd, then the following will allow you to view buffer as doubles.
Expand|Select|Wrap|Line Numbers
  1. double * dPtr = (double *) & buffer[ dInd];
  2. cout << dPtr[0] << etc.
  3.  
Reading and writing binary data this way is very platform dependent. There are issues like big/little endian byte ordering. Also, the format and precision of the double can change between systems and even compilers.

Becareful how you use this.
Hey RRick, thanks very much. That worked quite well. I was thinking of something similar but I couldn't get the syntax right.

With regard to your warning: What is "big/ little endian byte ordering"?

I don't plan on changing compilers so that won't be an issue.
Aug 24 '07 #5
RRick
463 Expert 256MB
Endian has to do with the byte ordering with ints, longs, doubles, etc. I'll let you google that subject because I always get is backwards. A diagram really helps.

It appears you are doing this for speed and size, but be careful with the low level reads and writes. They are not necessarily buffered which can be expensive when you read/write small chunks of data. The C and C++ routines do support buffering and also support binary data.

I use the low level routines when I want to do a single read or write. I had one job that needed to read and write images. Since I knew how big the largest image was, I could do all my calculations in memory and then write it out with one write. Same thing for reading.
Aug 24 '07 #6

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

Similar topics

2
by: campbell.shaun | last post by:
The application I am writing uses XML files for validation. I've been able to set up my xml files so that I can share validation for common fields using external entities. eg <?xml...
3
by: Ais | last post by:
Hi.. Presently i'm trying to install IBM Db2 Software on my Windows XP Operating systems. I'm using IBM(R) DB2(R) Universal Database Enterprise Server Edition, Version 8.1. After...
1
by: raydelex | last post by:
I am new to securing a database with logins. My questions is: I want only one database to use a new Workgroup file that I have created, not all the Access databases that I bring up under my...
5
by: rdcpro | last post by:
In reading MSDN docs on creating custom Configuration sections, I found this page: ...
0
by: accessman2 | last post by:
I have a question. I want to open the MS Access file with user-level Security. I know that if I do NOT setup user-level Security in the MS Access file, and create the table for login in the MS...
2
by: RanjitSingh.RS | last post by:
I have several Databases in a Instance . I want to a set particular Isolation Level for Only one read Only Database . Is it Possible to set a specific Isloation Level at a Database Level
2
by: FP | last post by:
I have the following files; \A.php \F1\B.php \F1\F1_1\C.php File A has the lines "echo 'A'; include_once('F1/B.php');" File B has the lines "echo 'B'; include_once('F2/C.php');" File C has...
6
by: GrispernMix | last post by:
//ques and and level order traversal file name: lab6_build_leaf_up.cpp Instructions:
7
by: GrispernMix | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <iostream> #include <string.h> using namespace std; #define TRUE 1 #define FALSE 0
10
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I had a program and it always works fine and suddenly it gives me the following message when a pass a xml file to our server program: error code: -1072896680 reason: XML document must...
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?
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
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...
1
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.