-
storing data
i'm working on a project that requires me to store information and then monipulated it accordingly,
the problem is i have been working with fileIO for some time now and it seems i am getting no where, if anyone has any tips for sorting data retrieved from a file used to store information such as adresses and phone numbers can you please let me know.
-
Why don't you post your code so far and tell us what the specific problem you are having is? Here's a tutorial on file i/o that might help.
http://www.cprogramming.com/tutorial/lesson10.html
-
cheers for the toot, i know i should post my code but i keep changing it and i don't have one that is a good example of what i want, i'm mostly playing with fucntion till i find what i need so it's a bit of a mess,
here a small example of what i mean there might be mistakes as it's just to give you an idea so please don't pick on the small things just try to get the idea of what i am doing THX.
Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
struct data {
int num;
char des[20];
};
int main(int argc, char *argv[])
{
int num1, len;
char des1[20], * buffer;
cout<<"Please enter item number > ";
cin>> num1;
cout<<"\nplease enter desciption > ";
cin.ignore(1,'\n');
cin.getline(des1,20);
data item;
item.num=num1;
item.des[20]=des1[20];
//opening a file stream
ofstream ofile;
ofile.open("whattha.ddf",ios::app);
ofile<< des1 <<" "<< num1 <<endl;
ofile.close();
//reading the file to a buffer!!!!!!!!!!!!!!!
ifstream ifile;
ifile.open("whattha.ddf");
ifile.seekg(0,ios::end);
len=ifile.tellg();
ifile.seekg(0,ios::beg);
buffer = new char [len];
ifile.read(buffer,len);
//display contents of buffer (or file)
cout.write(buffer,len);
cin.get();
what i want to do is once i have sent information to the file i want to be able to retrieve it in a sorted manner ,
say if i input item number 12345 with the description "cheese"
i want to be able to search the item number and find the description?
i am stumped and have been for a while, where can i find the answer or has anyone got some valuable tips?
-
I think you should parse the input. that means, you have to extract the data that you can put in the 'data' structure. then you should write a sorting function by hand or use the sort() from STL(if you have heard of it.)
http://www.sgi.com/tech/stl/sort.html
trying to read from a file in a "sorted mode" is time-consuming and quite hard for a beginner to code, thus I recommend data extraction :)
-
Sounds like you want a Random Access File structure. In this file structure you have a record structure with fields that have a specified size. You can then access a specific record using a record number. I would suggest googling for "Random Access File C++". You will find examples that explain it much better then I. If you truly need to read in the file and sort it and then write it back out to the file sequentially, then go with Hardi and use "data extraction".