Thread: Line oriented text file access

  1. #1
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45

    Line oriented text file access

    I have been programming in C for a while and can do the usual file access magix, but im wondering if there is an efficient way to access a line of a file, i.e, a wordlist for s sentence generator. I would assume pointer manipulation prior to fscanf would do it but if anyone has any strokes of genius they would be much appreciated.
    Thankyou

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Text files must be read sequentially. You can jump around in them with varying success, but how would you propose knowing where to jump to? Keep reading until you hit the line you're looking for.

    If the file is formatted to give you a clue where the desired line is, that's a bonus, but otherwise, there's not much else you can do.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You mean access a specific line of a file for random read access or just simply reading through a file line by line?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you had lines that were always the same length (padded with blanks at the end), you could easily seek to a particular line, but it would waste a lot of space if most lines aren't up to the max line length, and if you want a longer line you'll have to reformat the whole file...

  5. #5
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45

    Delimiting

    could the carriage return character be used to delimit the lines and then like you say just step through each line, not actually storing the strings and pick a random line to stop at and read that line?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Unless this is a huge text file with many 10's of MB of data, just reading it all into memory would be a more efficient way of working.
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by andrew.bolster View Post
    could the carriage return character be used to delimit the lines and then like you say just step through each line, not actually storing the strings and pick a random line to stop at and read that line?
    Yeah. Just read characters (and throw them away) until you see '\n'. Each time you see that is a new line. Keep going until you are at the line number you want.

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. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. printing out a line from text file..
    By loso44x in forum C Programming
    Replies: 3
    Last Post: 10-22-2005, 01:14 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM