Reading a file!!!!!!!!

This is a discussion on Reading a file!!!!!!!! within the C++ Programming forums, part of the General Programming Boards category; I want to read the information from (store.ddf) in a sorted manner, lets just say i want the description of ...

  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,475
    >descrip.des=items;

    That is wrong, C++ forbids an assinment of an array. Just looking over the rest of the code.
    I'm just trying to be a better person - My Name Is Earl

  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 10:33 PM.
    WhAtHA hell Is GoInG ON

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    adr
    adr is offline
    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, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21