im tring to make vhd file and im having problems

hi im having trouble with this part>>>>per.name = williamc;
i cant figure out how to get it to take >>>>per.name = "williamc";
i just started c++ been reading alot ,i was using vb6 ,
what it does or what im tring to get it to is take williamc as char name
but im stuck.
thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <fstream> // for ifstream, ofstream, ios_base
#include <iostream>// for cin, cout, endl
// you can just say `using namespace std;` here instead of all these 'using's
using std::ios_base;
using std::cout;
using std::cin;
using std::endl;

// just creating a constant for the name of the database
static const char *DATABASE_FILE = "pr1.bin";

// the data that will go into the database
struct Person {

   char name[8];
   int Features;
   int FFVersion;  
   int DataOffset;
   int TimeStamp;
   int CreatorApplication;
   int CreatorVersion;  
   int CreatorHostOS;
   int OriginalSize;
   int CurrentSize;
   int DiskGeometry;
   int DiskType;
   int Checksum;  
   int UniqueId;
   int SavedState;
   //int Reserved;

};


// this function takes a pointer to a Person struct
// that will be filled with the data in the file
bool LoadDB(Person *per) {
  // first parameter is the name of the file
  // second is the open flags. 
  //    `in` says you will read from the file
  //    `binary` says you will read binary data instead of simple text
  std::ifstream fin(DATABASE_FILE, ios_base::in | ios_base::binary);
  
  // if we couldn't open the file (it may not exist); fail
  if (!fin.is_open())
    return false;
  
  // the file is open; tell the read function to look at `per` as a
  // char pointer instead of a Person pointer so that `fin` can place
  // one byte at a time into the struct
  // the second parameter tells read() how many bytes to read
  // Note: do not use the ">>" operator to read from a binary file 
  fin.read((char*)per, sizeof(Person));
  fin.close();// close the file - not necessary, but we should be neat

  // success  
  return true;
}

// this function takes a pointer to a Person struct
// that will be written into the file
bool SaveDB(Person *per) {
  // these flags say:
  //    `out` - we will be writing data into the file
  //    `binary` - we will be writing binary data and not simple text
  //    `trunc` - if the file already exists, truncate (wipe out) the existing data
  std::ofstream fout(DATABASE_FILE, ios_base::out | ios_base::binary | ios_base::trunc);
  // couldn't open it (disk error?); fail
  if (!fout.is_open())
    return false;

  // notice this time we cast the Person pointer to a const char* because we aren't
  // altering the structure this time; rather we're just looking at it's contents.  
  fout.write((const char*)per, sizeof(Person));
  fout.close();// close the file - not necessary, but we should be neat

  // success  
  return true;
}

int main() {
  // create an empty person struct
  Person per;
  
  // load the data in the db file
  if (LoadDB(&per)) {
    // print what we loaded into our Person struct
    cout << "Loaded Database...\n";
    cout << "name = " << per.name << endl;
    cout << "Features = " << per.Features << endl;
    cout << "FFVersion = " << per.FFVersion << endl;
    cout << "DataOffset = " << per.DataOffset << endl;
    cout << "TimeStamp = " << per.TimeStamp << endl;
    cout << "CreatorApplication = " << per.CreatorApplication << endl;
    cout << "CreatorVersion = " << per.CreatorVersion << endl;
    cout << "CreatorHostOS = " << per.CreatorHostOS << endl;
    cout << "OriginalSize = " << per.OriginalSize << endl;
    cout << "CurrentSize = " << per.CurrentSize << endl;
    cout << "DiskGeometry = " << per.DiskGeometry << endl;
    cout << "DiskType = " << per.DiskType << endl;
    cout << "Checksum = " << per.Checksum << endl;
    cout << "UniqueId = " << per.UniqueId << endl;
    cout << "SavedState = " << per.SavedState << endl;
    cout << endl;
  }

    
    per.name = williamc;
    per.Features = 0x00000;
    per.FFVersion = 0x00010000;  
    per.DataOffset = 0xFFFFFFFF;
    per.TimeStamp = 101000;
    per.CreatorApplication = 87776732;
    per.CreatorVersion = 0x0100;  
    per.CreatorHostOS = 0x5769326B;
    per.OriginalSize = 5243392;
    per.CurrentSize = 5243392;
    per.DiskGeometry = 00065535;
    per.DiskType = 2;
    per.Checksum = 31;  
    per.UniqueId = 427112341;
    per.SavedState = 1;








  //(cin >> per.salary).get();// note .getline() clears its own newline character

  // save the new user-entered data  
  if (SaveDB(&per))
    cout << "\nDatabase saved!\n";
  else
    cout << "\nUnable to save database!\n";

    system("pause");
 
  // return 0 on success (main() should never be void!)
  return 0;
}
c strings are null terminated so "williamc" is actually 9 characters. If you increase the length of name you can use strcpy to write the "williamc" to per.name.
std::strcpy(per.name, "williamc");

If you don't want per.name to be null terminated you can do
std::memcpy(per.name, "williamc", 8);
but don't do this unless you know what you are doing.
Last edited on
so thats why.
it must ony be 8 characters long .
if i had to write to 85 bytes ,like asm does ,im doing it the wrong way i think.
thanks all
Last edited on
the first program didnt work to make a vhd file even though i followed Virtual Hard Disk Image Format Specification from ms.theres still more work i have to do to figure out how to addjust the file size.this will make a 200 megabyte file.if you see anything i can do better please tell me.



what i was woundering is can i load an array with numbers and other arrays in the array?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main()
{
  BYTE  cur_char;

  ofstream out("test.vhd",ios::out | ios::binary); 
  if (!out.is_open())
    return false;
     for (int n=0; n<3072; n++) {
    cur_char = 0; { out << cur_char;  }
  } 
  out.seekp(0,ofstream::beg); 
  BYTE holdarray[] = {99,111,110,101,99,116,105,120,0,0,0,2,0,1,0,0,0,0,0,0,0,0,2,0,23,69,64,121,118,112,99,32,0,5,0,3,87,105,50,107,0,0,0,0,12,128,0,0,0,0,0,0,12,128,0,0,3,57,16,31,0,0,0,3,255,255,238,229,44,180,124,213,158,221,17,225,130,42,201,29,129,166,153,95};
   //write to outfile
  out.write((char *) &holdarray,sizeof holdarray); 
  out.seekp (512);
  BYTE holdarray2[] = {99,120,115,112,97,114,115,101,255,255,255,255,255,255,255,255,0,0,0,0,0,0,6,0,0,1,0,0,0,0,0,100,0,32,0,0,255,255,244,19};
  out.write ((char*)&holdarray2, sizeof holdarray2);
  out.seekp (1536);
     for (int n=1536; n<1936; n++) {
    cur_char = 255; { out << cur_char;  }
  } 
  out.seekp (2560);
  out.write ((char*)&holdarray, sizeof holdarray);

  out.close();
   return 0;
}
william427 wrote:
the first program didnt work to make a vhd file even though i followed Virtual Hard Disk Image Format Specification from ms.

Make sure that you use the correct size in bytes for the different fields.
virtual pc will run mine but no other program will mount it.
do yall know what im doing wrong ,id like to do it in c++.

because following the specifications does not explain certian details as the second program i have up shows.
thanks all
hey all if i followed Virtual Hard Disk Image Format Specification from ms.
and im running win xp how do i go about mounting my file using c++,is it possible and what topic do i search to find more on the subject?
thanks
Topic archived. No new replies allowed.