Thread: Making files, opening them, and writing to them

  1. #1
    Unregistered
    Guest

    Making files, opening them, and writing to them

    i am attempting to make a sud (sigle user domain) and i want to make it possible to save and open your account. First I need to know how to make a data file called whatever is stored in char name[10]. I now need to know how to test to see if a file with that name exists and how to acsess if it does exist. I then need to know how to acsess information from that and writ to it preferably in the following format:

    Age =
    Life =
    max life =
    mana =
    max mana =

    and so on. Please help

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    To open a file to write to you can do this:
    Code:
    FILE *outfile = fopen("FileName.Ext", "w");
    //fprintf is similiar to printf except it takes a file pointer first
    fprintf(outfile, "Age = %d", Age);
    //More fprintf's
    fclose(outfile);
    To open a file for reading do this:
    Code:
    FILE *infile = fopen("FileName.Ext", "r");
    if(infile==NULL){
        //File open error or file does not exist
    }
    //Later on 
    fclose(infile);
    To read from the file take a look at functions like getc and fgets.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Unregistered
    Guest
    um... i dont understand what you mean by "r" and "w".
    P.S. please include the headers needed because with normal iostream it doesnt recognize it

  4. #4
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    This will comile with stdio.h, stdlib.h, and strings.h. This works for C, I do not know about C++. Sorry.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  5. #5
    Unregistered
    Guest
    this is great! thanks a lot, but i still cannot extract becuase the getf command istn lists in the commands, but my compiler recognizes it, so could you please inform me of how to extract these lines out of a file called sheep?

    fuzzy
    100
    200
    0

    i want to put all of these into wariables: "feel_like", "wieght", and "dullness" do you know how do do that?

  6. #6
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    here

    or

    here

    either one will be able to help you out.

  7. #7
    Unregistered
    Guest

    Talking

    i FINALY got it! thanx for you help!

Popular pages Recent additions subscribe to a feed