Thread: Thick n00b needs help with File i/o and structs

  1. #1

    Thick n00b needs help with File i/o and structs

    OK folks im being as dense as a very dense thing here. Ive searched for tutorials etc and i cant seem to figure it out in the way i want to use it.

    I want a struct to hold data on a supplier, namely and ID for the supplier and a supplier name.

    im currently using :

    struct Suppliertype
    	char *cName;
    	int iSuppId;
    I then want the user to enter the new data and append this data to a text file (supplier.txt).

    im currently trying it like this:

    void NewSupplier(void)
    	char sNewSupplier[50];
    	cout << "Enter the name of the new supplier: ";
    	cin >> sNewSupplier;
    	ofstream SuppFile("supplier.txt", ios::app);
    	SuppFile << sNewSupplier << "," << "/n";
    i know i aint using the struct in this coz this is the problem - i dunno how to.

    After ive appended the file i want to have a separate function to read the text file and display the list of suppliers on the screen.

    This is for part of my Uni degree (only 1st year tho) so i aint gonna be nicking peeps code for commercial use - i just want to know how to do this.

    Thanks in advance

  2. #2
    Declare an object of the structure in the second function. Load the data into the structure using cin and the dot operator. You are opening the file correctly, but you need to cast the object to a char* then write the sizeof the structure to the file. If you still need some help try doing a search. You should have no problem finding some examples in previous threads because this topic has been discussed many times. Have a great day.

  3. #3
    Registered User
    Join Date
    May 2002
    Ok as per structures you implement your strucure like so:
    Suppliertype Supplier1; //creates a variable of your struture type
    Supplier1.iSuppId = (whatever); //you use variables like this
    fstream SuppFile("supplier.txt", ios::app | ios :: out);
    SuppFile << sNewSupplier << "," << "/n";
    SuppFile << "Supplier Id: "<<Supplier1.iSuppId
    Hope this helps

  4. #4
    Registered User Catif's Avatar
    Join Date
    May 2002
    Cheers folks - much appreciated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file help plz
    By dutrachr in forum C Programming
    Replies: 4
    Last Post: 04-18-2006, 11:41 AM
  2. Help with file I/O
    By cantore in forum C Programming
    Replies: 5
    Last Post: 04-02-2006, 02:05 PM
  3. Replies: 5
    Last Post: 10-02-2005, 12:15 AM
  4. File I/O and structs
    By Evandb in forum C Programming
    Replies: 6
    Last Post: 10-13-2004, 09:40 PM
  5. Reading from file into structs..
    By dankas in forum C Programming
    Replies: 14
    Last Post: 10-16-2002, 10:33 PM