Thread: Opening ASCII files in C?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    98

    Opening ASCII files in C?

    How do I open, read and write ASCII files?

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    An ASCII file is only a text file with a specific character encoding. You can apply the C File I/O tutorial and read the file as a normal text file.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    fopen()
    fgets()
    fclose()
    Easy money.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    Is there any other way to read/write ASCII files without using fopen and fclose?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure, use open and close, but I don't see why you'd want to. fopen and fclose are your standard methods of using files in C.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    fopen and fclose open and close files.
    fread / fwrite / fgets / fputs / fgetc / fputc do the actual reading and writing.

    Anything else is slipping into non portability.

    I suppose you could open "/dev/hda" if you want to be extreme.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Anything else is slipping into non portability.
    Well, besides getc() and putc(), of course.

    getc() and putc() are equivalent to fgetc() and fputc(), but are usually implemented as macros instead of functions; so the might be slightly faster, but don't pass them an argument like "c++", because it could be evaluated several times.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opening files.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-25-2008, 02:05 PM
  2. Need help opening a series of files
    By ramparts in forum C Programming
    Replies: 9
    Last Post: 11-14-2006, 05:49 PM
  3. Opening files with UNICODE file names
    By decohk in forum Linux Programming
    Replies: 2
    Last Post: 11-09-2006, 05:25 AM
  4. opening files
    By angelic79 in forum C Programming
    Replies: 3
    Last Post: 10-19-2004, 06:52 AM
  5. Opening files - Giving options?
    By wwwGazUKcom in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2001, 07:06 AM