Thread: Reading a file!!!!!!!!

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

    Reading a file!!!!!!!!

    I want to read the information from (store.ddf) in a sorted manner, lets just say i want the description of item 11111 stored in file (stores.ddf), how would i do this?

    if the question is to broad and i'm just going to go have a look at fstream function i will do this.





    Code:
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    
     using namespace std;
    
    
    
    void rfile()
    {
    
    char string[100];
    
    ifstream bfile;
    bfile.open("stores.ddf");
    // read function are here
    
    bfile.read(string,ios::end);
    
    cout<< string;
    cin.ignore(1000, '\n' );
    
    
    
    
    };
    
    
    void file(int a, int b, char c[100])
    {
    
    char string[100];
    
    ofstream file;
    file.open("stores.ddf",ios::app);
    file<<" ITEM "<< b <<" Quantity "<< a <<" Description "<< c <<endl;
    file.close();
    
    }
    
    struct product
    {
    int item;
    char des[256];
    int qty;
    };
    
    
    int main()
    {
    
     int itemN, qtyN, *b;
     char ans, items[256];
    
    
    do {
    
    cout<<"\nPlease enter the item description > ";
    cin.getline(items,256);
    cout<<"Please enter the item number >";
    cin>> itemN;
    cout<<"please enter the quanity counted >";
    cin>> qtyN;
    
    product descrip;
    
    
    descrip.item=itemN;
    descrip.qty=qtyN;
    descrip.des=items;
    
    
    cout<<"\nThis is the data you have entered\n";
    cout<<"\nItem description > "<< descrip.des;
    cout<<"\nItem > "<< descrip.item;
    cout<<"\nQuantity > "<< descrip.qty <<"\n";
    cout<<"\nIs this correct (Y/N) > ";
    cin>> ans;
    
    file(descrip.qty,descrip.item,descrip.des);
    
    } while ( ans=='n' );
    
    char q;
    
    
    cout<<"\nWould you like to read the file > ";
    cin>> q;
    
    if (q=='y') {
    rfile();
    } else {
    
    char w;
    
    cout<<"\nWould you like to clear the file > ";
    cin>> w;
    
    if (w=='y'){
    ofstream afile;
    afile.open("store.ddf",ios::trunc);
    afile.close();
    } else {
    return 0;
    };
    
    
    
    
    
    };
    }
    WhAtHA hell Is GoInG ON

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >descrip.des=items;

    That is wrong, C++ forbids an assinment of an array. Just looking over the rest of the code.
    Double Helix STL

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by swgh
    >descrip.des=items;

    That is wrong, C++ forbids an assinment of an array. Just looking over the rest of the code.
    it works ? i was told to use strcpy(); but this worked so i left it, i dunno.
    Last edited by wart101; 12-01-2006 at 11:33 PM.
    WhAtHA hell Is GoInG ON

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    but this worked so i left it, i dunno
    It works while you are still in the function
    when you exit - your local variable is destoyed and you get a dangling pointer...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by vart
    It works while you are still in the function
    when you exit - your local variable is destoyed and you get a dangling pointer...

    Ok, i can fix that but what about my reading a file question?
    WhAtHA hell Is GoInG ON

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Search google for File I/O.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by manutd
    Search google for File I/O.

    HeHeHE, ok, well if you feel like giving me some tip i would appreciate it.
    WhAtHA hell Is GoInG ON

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    http://www.cplusplus.com/doc/tutorial/files.html

    Heres a site to help out with reading a file. Dont know if you ever look at or if this is what your asking for XD ^^; Good luck.

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM