Thread: how can I read file by colums?

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    how can I read file by colums?

    file
    Code:
    ABCD
    EFGD
    FFDG
    I want to read file by colums,
    AEF BFF CGD DDG

    how to do it ?
    which function can do it ?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The function you write will do it - hopefully.

    When you read a file, you read it SEQUENTIALLY. So there is no way to ignore the parts of a row that you have no interest in, atm.

    What you need to do is:

    1) read and store the first char

    2) read but ignore the number of char's equal to the width of the row (picturing this as a table with rows and columns, as your post describes it).

    3) read and store the next char. Do #1 thru #3, until you've read the number of rows you wanted.

    4) move the file pointer backward, by the number of char's in the row times the number of rows you got.

    5) read and store the next char - which will be the first char in the second column.

    As you can see, it's pretty inefficient, because you're constantly moving the read head back and forth. Probably better to read the data you want, into an array with rows and columns, and then you can access the data directly, instead of sequentially. Much faster that way, and easier to program, as well.

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Adak
    When you read a file, you read it SEQUENTIALLY.
    Not necessarily; there's fseek() and friends that lets you jump around the file. Although, that does not make your suggestion any less valid -- reading everything in seems to be the way to go in this case.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) allocate enought memory to hold the entire file.
    2) load the entire file.
    3) close the file
    4) use strtok() or similar indexing tool to index the file in memory.
    5) in each token, simply read the character you're interested in zz = mytoken[mycol]
    6) free memory
    7) print results
    8) go get a cold one...
    Last edited by CommonTater; 10-12-2010 at 02:05 AM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by msh View Post
    Not necessarily; there's fseek() and friends that lets you jump around the file. Although, that does not make your suggestion any less valid -- reading everything in seems to be the way to go in this case.
    The read and write heads on traditional hard drives (even using fseek()), always move through the disk, in a sequential order - there is no random reading/writing. The head can position itself w/o reading or writing, to any absolute position (sector, etc.), on the disk, but it still has to get from point A to point C, by moving the head through point B.

  6. #6
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Adak View Post
    The read and write heads on traditional hard drives (even using fseek()), always move through the disk, in a sequential order - there is no random reading/writing. The head can position itself w/o reading or writing, to any absolute position (sector, etc.), on the disk, but it still has to get from point A to point C, by moving the head through point B.
    You know what I was getting at, and it had nothing to do with how the data is actually read from the physical disk -- something we don't care about in general application programming. My post was more for accuracy and completeness then anything else, really.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No, I did not know what you were getting at, apparently. I don't try and bait people, or run in ego competitions. Not my way.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by CommonTater View Post
    1) allocate enought memory to hold the entire file.
    2) load the entire file.
    3) close the file
    4) use strtok() or similar indexing tool to index the file in memory.
    5) in each token, simply read the character you're interested in zz = mytoken[mycol]
    6) free memory
    7) print results
    8) go get a cold one...
    I think it's not viable, because my input file is very very big

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    How big is it?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I only see three ways to do this:

    1) Use the "read a column, then fseek() forward (or read and ignore), and read the next column, continuing until you need to return to the first column + 1 position (one data type width, not one byte).

    Do that until you have processed enough of the file, that the remainder will all fit into an array. Do the rest of the processing, from the array.

    2) Load up all you can of the data, into an array. Process it as needed, until you're down to the very last row or two. Then move these last row/s to the lowest indeces, and re-load more data. Repeat until the data is exhausted.

    This has the advantage of working only from the array, and being a good combo of fast to process, and simple to code up. I should have suggested it, the first time.

    3) Re-arrange the data by re-shuffling it, so the parts that would benefit from being adjacent, are made adjacent. Disadvantage: time needed to do the extra work. Advantage: any subsequent searching or processing that needed this kind of data layout, would be hugely faster.

    For one time processing, I'd use method #2. For work that involved lots of searches or processing later on that would really benefit from having the data organized this way, I'd use method #3.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 02:01 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM