Reading and Writing from files

This is a discussion on Reading and Writing from files within the C Programming forums, part of the General Programming Boards category; Hi there, I'm a university student studying C, I was wondering if you all could help me. I am writing ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    9

    Reading and Writing from files

    Hi there,

    I'm a university student studying C, I was wondering if you all could help me.

    I am writing a database, just a simple practise one, taking in name, telephone, address. I have defined a structure for it all, but my questions are:

    1. Can a read a .txt file with my program and display it,my desire is that when the program is exited the values the user has inputed previous are stored in a .txt and when they re-run the program they can access already stored values.

    2. Also are there any specific syntax when working with structures and writting to a .txt file.

    If you have any querries just ask and i'll reply, sorry for lack of code but only starting to design the program and weeding out all potential problems.

    Cheers

    Dutch's Finest

  2. #2
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    I can't really help you much with the coding, but there are several formats you could use.

    1) XML format, which would aid in the searching/display of your records.

    2) Delimited text file (eg. comma). This would be easy to parse.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >1. Can a read a .txt file with my program and display it
    Yes. It isn't terribly difficult either.

    >2. Also are there any specific syntax when working with structures and writting to a .txt file.
    Well, everything has a specific syntax. Take a look at our FAQ (this one in particular) for details.
    My best code is written with the delete key.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: Reading and Writing from files

    Originally posted by dutch's finest
    Hi there,

    I'm a university student studying C, I was wondering if you all could help me.

    I am writing a database, just a simple practise one, taking in name, telephone, address. I have defined a structure for it all...
    Look at the fread and fwrite functions. If you open the file in binary mode, you can initially write out yout structure directly to the file as you exit. As the program starts, simply read the file into your structure and voila!
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >Look at the fread and fwrite functions.

    Arguments against this:
    http://groups.google.com/groups?hl=e...3.giganews.com
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    15
    ok if you open a file to a file pointer declaring as

    Code:
    FILE *file
    
    while(i < 10)
    {
         a = fgetc(file);
         i++;
    }
    then you can step through the file using code such as


    or you can as some one said use fwrite and fread but this doesnt let you look at files individually unless you use malloc but this is just difficult and hard to be honest.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by Dave_Sinkula
    >Look at the fread and fwrite functions.

    Arguments against this:
    http://groups.google.com/groups?hl=e...3.giganews.com
    Arguments for this:
    Originally posted by dutch's finest
    1) I'm a university student studying C...

    2) I am writing a database, just a simple practise one...

    3) It gives good practice on using fread() and fwrite()
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by liams7
    or you can as some one said use fwrite and fread but this doesnt let you look at files individually unless you use malloc but this is just difficult and hard to be honest.
    Looks like someone else is new to programming.
    What makes you think you can't look at files individually? I do this all the time and never use malloc for it?
    Where did you get this idea?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    Hey all, thanks for everything really appreciate what you all have said wrote the database and works perfectly

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading and writing files
    By jcafaro10 in forum C Programming
    Replies: 8
    Last Post: 04-08-2009, 10:36 PM
  2. writing and reading files
    By oldie in forum C Programming
    Replies: 1
    Last Post: 07-03-2008, 04:54 PM
  3. reading and writing to files
    By cgod in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2004, 10:00 PM
  4. reading and writing to files in windows
    By Dohojar in forum Windows Programming
    Replies: 8
    Last Post: 06-26-2003, 02:07 AM
  5. Reading binary files and writing as text
    By thenrkst in forum C++ Programming
    Replies: 8
    Last Post: 03-13-2003, 09:47 PM

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