Hello I'm new to C++ and Im trying to practice simple random-access techniques. I tried to write this simple program where i enter information about a car at determined locations with in a text file. The code is below...
#include <iostream>
#include <fstream>
using namespace std;
int get_int(int default_value);
char model[20];
char make[20];
char year[5];
int main() {
char filename[81] = "test.txt";
int n;
int mileage;
int recsize = sizeof(model) + sizeof(make)
+ sizeof(year) + sizeof(int);
fstream fbin(filename, ios::binary | ios::in | ios::out);
if (!fbin) {
cout << "Could not open file " << filename;
return 0;
}
while(1){
cout << "Enter file record number (Enter -1 to exit): ";
n = get_int(0);
if(n==-1)
break;
cout << "Enter car model: ";
cin.getline(model, 19);
cout << "Enter car make: ";
cin.getline(make, 19);
It seems when i enter information in consecutive locations the data seems to over lap, so when i try to read the information from the file it does not return what i want. I cant find the bug in the code.
Can someone help me because it's frustrating me. I've looking at this for hours and figure it out.
Why are you opening the file in binary mode if it is a text file? Also please format this code correctly. It's incomprehensible as you posted it and I'm also unsure of what you mean by "random access".
also, I'm not sure if you can use seekp to make a file bigger. You are opening a file that is presumably empty (although I have no idea what is on your hard drive). You seem to think that you can seek wherever you want regardless of the actual file size and then start writing data. I'm not sure that you can do that but I'm unfamiliar with the std. You might want to take a look at the c++ std or do a google search on this topic.
One more thing is that you are writing the entire char arrays regardless of the string length which means that you might be writing garbage to the file. If make of car is "ford" there are only 4 characters plus the NULL. What is in the other 15 bytes of the array and why would you want to write that to the file?
I just read in my STL book that seeking to locations before the beginning or past the end is undefined behavior. You need to make sure that the file is big enough to handle what you are doing. The program never determines the actual size of the file. You might have to perform a test and write a bunch of fill characters to make the file big enough before attempting to write. You can not just open an empty file and just start seeking to wherever you want. The stream class will not automatically write fill characters and expand the file dynamically for you.
It does not seem that the size of the file is an issue, it seems that the file changes its size according to what i enter. The only issue i really have is the fact that the entries keep over lapping.... for example if i choose two record locations that are not consecutive, i don't have an issue.
Well i am trying to teach my self how to code and I'm using this book. The chapter is on reading and writing to files, and this section is on using random access with binary information. Random access, as in one could choose to read and write data to a file where ever they so choose. The book did go over text files and writing to them earlier on the chapter, but it tries to touch on binary files too. Should i not use a *.txt file? This code above is something i tried to put together from some of the examples and what not that were provided. I assume the book is trying to give me some sort of look at a some simplified data base type thing. Where each category has an allotted space and allowing you to easily store and retrieve information... I don't know. Is this kind of thing unnecessary? Should i just move on? Or should this somehow work? Because this is pretty much what most of the examples and answers to the exercise look like.
Also i apologize about the formatting. I don't know how to put the code into the forum and keep the correct formatting, it seems the text editor auto formates it every time i try to correct it.