Implementing a class to simulate a disk

So this code compiles without any problem but it is not producing the correct output. I know there's a problem in either my getBlock or putBlock functions but I can't see it.

Currently the output is "Should be 32 1s: "
"Should be 32 2s: "

There should be 32 1s and 32 2s and nothing is coming out. Someone please help.

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
#include <iostream>
#include <fstream>

using namespace std;

class Sdisk
{
	public :
		Sdisk(string diskname);
		Sdisk(string diskname, int numberofblocks, int blocksize);
		int getblock(int blocknumber, string buffer);
		int putblock(int blocknumber, string buffer);
	private :
		string diskname;        // file name of software-disk
		int numberofblocks;     // number of blocks on disk
		int blocksize;          // block size in bytes
};

Sdisk::Sdisk(string diskname)
{
	this -> diskname = diskname;
	
	ifstream infile;  
	string spcfile = diskname + ".spc";
	infile.open("spcfile.c_str");
	if(infile.good())
	{
        	infile >> blocksize >> numberofblocks;
	}
	else if(!infile.good())
	{
       		numberofblocks = 128;
		blocksize = 128;
	}

	ifstream ifile;
	string datfile = diskname + ".dat";
	ifile.open("datfile.c_str");
	int counter = 0;
	char x;
	x = ifile.get();
	while(ifile.good())
	{
        	counter++;
        	x = ifile.get();
	}
	if (counter == blocksize * numberofblocks)
	{
        	cout << "Dat File Ok";
	}
	else if(!counter == blocksize * numberofblocks)
	{	
        	cout << "Dat File Size Incorrect";
	}	
}

Sdisk::Sdisk(string diskname, int numberofblocks, int blocksize)
{
	this -> diskname = diskname;
	this -> numberofblocks = numberofblocks;
	this -> blocksize = blocksize;

	ifstream infile;  
	string spcfile = diskname + ".spc";
	infile.open("spcfile.c_str");
	if(infile.good())
	{
        	infile >> blocksize >> numberofblocks;
	}
	else if(!infile.good())
	{
       		numberofblocks = 128;
		blocksize = 128;
	}

	ifstream ifile;
	string datfile = diskname + ".dat";
	ifile.open("datfile.c_str");
	int counter = 0;
	char x;
	x = ifile.get();
	while(ifile.good())
	{
        	counter++;
        	x = ifile.get();
	}
	if (counter == blocksize * numberofblocks)
	{
        	cout << "Dat File Ok";
	}
	else if(!counter == blocksize * numberofblocks)
	{	
        	cout << "Dat File Size Incorrect";
	}
}

int Sdisk::getblock(int blocknumber, string buffer)
{
	ifstream bufferfile;
	bufferfile.open("blocknumber", ios::in | ios::out);
	bufferfile >> buffer;
	bufferfile.close();
}

int Sdisk::putblock(int blocknumber, string buffer)
{
	ifstream blockfile;
	blockfile.open("buffer", ios::in | ios::out);
	blockfile >> blocknumber;
	blockfile.close();
}

int main()
{
  	Sdisk disk1("test1",16,32);
  	string block1, block2, block3, block4;
  	for (int i=1; i<=32; i++) block1=block1+"1";
  	for (int i=1; i<=32; i++) block2=block2+"2";
  	disk1.putblock(4,block1);
  	disk1.getblock(4,block3);
  	cout << "Should be 32 1s : ";
  	cout << block3 << endl;
  	disk1.putblock(8,block2);
  	disk1.getblock(8,block4);
  	cout << "Should be 32 2s : ";
  	cout << block4 << endl;;
}
Why did you create another account and then make a third post? You already have two open topics:
http://www.cplusplus.com/forum/general/112265/ <--Original
http://www.cplusplus.com/forum/beginner/112269/ <--Second
Uh...I didn't create a new account? Whomever posted those must be working on the same type of assignment.
Haha, wow. Forgive me - indeed you both must be working on the exact same assignment.

On line 12, you're not taking the string parameter by reference, so how do you expect to change its value?
Last edited on
Topic archived. No new replies allowed.