|
-
Jan 31st, 2001, 10:25 PM
#1
Thread Starter
PowerPoster
I know how to do this in VB. But can anyone teach me how to archieve this in VC++?
Code:
Option Explicit
Private Type sData
ID As String
Age As Integer
Name As String
End If
Private sd As sData
Private Sub ReadFile()
Dim Ptr As Long
Open "C:\1.dat" For Binary As #1
Ptr = 0
Do While Not EOF(1)
Get #1, (Ptr * Len(sd)) + 1, sd
Debug.Print sd.ID
Debug.Print sd.Age
Debug.Print sd.Name
Ptr = Ptr + 1
DoEvents
Loop
Close #1
End Sub
-
Feb 1st, 2001, 09:13 AM
#2
Monday Morning Lunatic
Something like this:
Code:
#include <stdio.h>
struct MyType {
int iNum;
bool bX;
};
void main() {
FILE *pFile = fopen("temp.dat", "wb");
MyType X = {5, true};
MyType Y;
fwrite(pFile, &X, sizeof(MyType);
fclose(pFile);
pFile = fopen("temp.dat", "rb");
fread(pFile, &Y, sizeof(MyType);
fclose(pFile);
printf("%d = %d", iNum, bX);
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 1st, 2001, 08:08 PM
#3
Thread Starter
PowerPoster
Parksie, can it be done by using the ReadFile API function? and use pointer to read various section of the file?
-
Feb 1st, 2001, 10:21 PM
#4
Thread Starter
PowerPoster
Parksie, When I run your code, I get some error and later on I modified the here and there and it work now.
Code:
'//Code in MyProject.h
void ReadWrite(HWND);
typedef struct {
int iNum;
bool bX;
}MyType;
MyType X;
MyType Y;
Code:
'//Code in MyProject.cpp
void ReadWrite(HWND hDlg)
{
FILE *pFile = fopen("c:\\1.dat", "wb");
X.bX = true;
X.iNum = 5;
fwrite(&X, sizeof(MyType), sizeof(MyType), pFile);
fclose(pFile);
pFile = fopen("C:\\1.dat", "rb");
fread(&Y, sizeof(MyType), sizeof(MyType), pFile);
fclose(pFile);
printf("%d = %d", Y.iNum, Y.bX);
return;
}
Do you have any idea on how to read the file by a given address location?
-
Feb 1st, 2001, 10:46 PM
#5
Thread Starter
PowerPoster
Parksie, I found that I can use the SetFilePointer API to move the open file pointer to the location I need. But Do you know how to use the OpenFile API to open a Binary file?
Thx
-
Feb 2nd, 2001, 04:51 AM
#6
Thread Starter
PowerPoster
can someone explain this to me?
Code:
typedef struct {
long ID;
float fees;
double pt;
} TEST;
TEST ut;
void ReadWrite(HWND hDlg)
{
FILE *pFile;
//Open the map data file
pFile = fopen("C:\\2.dat", "rb");
//Read the Genral Information abt the Map data file
fread(&ut, sizeof(ut), 1, pFile);
//Close the Map data file
fclose(pFile);
return;
}
if I include the extra element call Name then I will not be able to read the data correctly from a binary file as I read with the above method.
Code:
typedef struct {
char Name[5];
long ID;
float fees;
double pt;
} TEST;
TEST ut;
void ReadWrite(HWND hDlg)
{
FILE *pFile;
//Open the map data file
pFile = fopen("C:\\2.dat", "rb");
//Read the Genral Information abt the Map data file
fread(&ut, sizeof(ut), 1, pFile);
//Close the Map data file
fclose(pFile);
return;
}
But If i resort the structure as below, I'll be able to read the data correctly from a binary file
Code:
typedef struct {
long ID;
float fees;
double pt;
char Name[5];
} TEST;
TEST ut;
void ReadWrite(HWND hDlg)
{
FILE *pFile;
//Open the map data file
pFile = fopen("C:\\2.dat", "rb");
//Read the Genral Information abt the Map data file
fread(&ut, sizeof(ut), 1, pFile);
//Close the Map data file
fclose(pFile);
return;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|