Thread: Reading in a text file.

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    44

    Reading in a text file.

    Hey again people. For my latest program the first step is for me to read a text file into a program and into a linked list. I haven't been taught this, and i'm wondering if you guys could give a few tips or point me in the direction of some useful websites to help me deal with the problem.

    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Discussed many times before on this forum - do a search.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    I have done and its helped thanks =]

    Just a quick question how would i go about working out the length of the text file??

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Read it into a linked list
    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.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    Ah thats my goal my currently i want to read it into an array so i can format all the characters and remove certain things before it goes into the list, so it's easier to search. So any ideas? =]

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's discussed even more often on this forum then reading things into a linked list.

    It's also amazingly unnecessary, as you don't need (or, for that matter, want) the whole file in memory to format characters et cetera. I would be surprised if you needed much more than a few lines at any given time.

  7. #7
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    I am only a newbie myself, but here are some things which might give you some extra insight to solve the problem.

    Every open file has a position indicator associated with it. A text file will have the EOF character at the end of the file.

    1. You can try counting characters(including whitespaces) until the end of the file.

    2. ftell() is a function that gives the current file position in bytes from the start of the file.

    You can open a file for reading(while the EOF isn't reached), then use the ftell() to tell you the position once EOF is reached.
    OS: Linux Mint 13(Maya) LTS 64 bit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM