Using char as record

Hello,
I would like to be able to use a char instead of an int to store the record (variable n) for this program:
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
// Random_Access_Write.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <Windows.h>
using namespace std;

	int get_int(int default_value);
        #define COL_WIDTH 80

int _tmain() {
	char filename[MAX_PATH + 1];
	int n = 0;
	char model[20];
	char make[20];
	int year = 0;
	int mileage = 0;
	int recsize = sizeof(model) + sizeof(int);

	cout << "Enter file name: ";
	cin.getline(filename, MAX_PATH);
	
	// Open file for binary write
	fstream  fbin(filename, ios::binary | ios::out);
	if (! fbin) {
		cout << "Could not open " << filename << endl;
		system("PAUSE");
		return -1;

	}
	while(1) {
		// Get record number to write to.

	cout << "Enter file record number: ";
	n = get_int(0);
	if (n == -1)
		break;



	// Get data from end user.

	cout << "Enter model ";
	cin.getline(model, sizeof(model) - 1);
	cout << "Enter brand: ";
	cin.getline(make, sizeof(make) - 1 );
	cout << "Enter year: ";
	year = get_int(0);
	cout << "Enter mileage: ";
	mileage = get_int(0);


	// Write data to the file.

	fbin.seekp(n * recsize);
	fbin.write(model, sizeof(model) - 1);
	fbin.write(make, sizeof(make) - 1);
	fbin.write((char*) (&year), sizeof(int));
	fbin.write((char*) (&mileage), sizeof(int));
	}

	fbin.close();
	system("PAUSE");
	return 0;


}



int get_int(int default_value) {
	char s[COL_WIDTH+1];
	cin.getline(s, COL_WIDTH);
	if (strlen(s) == 0)
		return default_value;
	return atoi(s);

	//Write data 



}




I tried:

1
2
3
4
5
6
        char n1[COL_WIDTH + 1];
        cout << "Enter file record word: ";
	cin.getline(n1, COL_WIDTH);
	int n = int(n1);
	if (n == -1)
		break;


But when I used the program to read the file and typed in the word that I had previously typed to store the record, nothing came up.

edit: When I write the binary file to store the data it takes up LOTS of space! (30+ Megabytes sometimes, and when I open the file with Notepad it has thousands of blank spaces. Is there a more efficient way to do it?


Thanks in advance.
Last edited on
I believe your main issue is line 57. The stream position moves automatically as you write, so you don't need to make sure you're at the end of the file. Why recsize is the size it is, I do not know.

A method of making proper binary files is to include amounts within the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char word[] = "Hello";
unsigned int length = strlen(word);

//write
binaryOut.write((char*)(&length), sizeof(length)); // sizeof(length), not unsigned int.  Makes changing length's type easier
binaryOut.write(word, strlen(word);

// read

char *newWord;
unsigned int newLength;
binaryIn.read((char*)(&newLength), sizeof(newLength));
newWord = new char[newLength + 1];
binaryIn.read(newWord, newLength);
newWord[newLength] = '\0';


not sure why you have windows.h in there. If your compiler warns you that atoi is dangerous, rub it gently and say "I know where my null terminators are".
Last edited on
I tried that new code, when I ran it I got a message box that said:
"Run-Time Check Failure #2 - The variable 'newWord' is being used without being initialized

here's my code:

1
2
3
4
5
6
7
8
9
10
11
		cout << "Enter record name: ";
		cin.getline(newWord, COL_WIDTH);
		unsigned int newlength;

		// Read data from the file.
		db.read((char*)(&newlength), sizeof(newlength));
		name = new char[newlength + 1];
		db.read(newWord, newlength);
		newWord[newlength] = '\0';
		db.read(model, sizeof(model) - 1);
		db.read((char*) (&year), sizeof(int));
Last edited on
May someone please help? I have until tomorrow to finish this. I tried very hard but I can't find out why I'm getting these errors (I've known C++ only for a month)
Topic archived. No new replies allowed.