// Random_Access_Write.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <Windows.h>
usingnamespace 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?
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:
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".
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);
unsignedint newlength;
// Read data from the file.
db.read((char*)(&newlength), sizeof(newlength));
name = newchar[newlength + 1];
db.read(newWord, newlength);
newWord[newlength] = '\0';
db.read(model, sizeof(model) - 1);
db.read((char*) (&year), sizeof(int));
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)