file
I want to read file by colums,Code:ABCD EFGD FFDG
AEF BFF CGD DDG
how to do it ?
which function can do it ?
file
I want to read file by colums,Code:ABCD EFGD FFDG
AEF BFF CGD DDG
how to do it ?
which function can do it ?
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.
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.Originally Posted by Adak
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.
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.
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.
How big is it?
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.