Thread: storing data

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    142

    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.
    WhAtHA hell Is GoInG ON

  2. #2
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    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

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    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?
    WhAtHA hell Is GoInG ON

  4. #4
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    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
    Programming is a form of art.

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    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".
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Push_back not storing data to vector
    By csonx_p in forum C++ Programming
    Replies: 13
    Last Post: 07-27-2008, 04:38 AM
  2. Robust method for storing data outside of a program
    By goatslayer in forum C++ Programming
    Replies: 17
    Last Post: 09-19-2007, 03:08 PM
  3. Storing data from a file into a single char string
    By tuxinator in forum C Programming
    Replies: 16
    Last Post: 05-01-2006, 10:21 PM
  4. storing client data in server app
    By codec in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2003, 10:34 PM
  5. Storing Data in a Structure
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2001, 06:43 AM