jumping lines in a file using c
hello i am trying to sort a textual file using c programming language, in order to sort the file i am using a unique key, i need to be able to jump from line to line in order to sort the file , the problem is that i do not know if there is a command in c which let me jump from the first line to lets say the 20 line for example the only solution which i know for it is to use each time fscanf with a loop but this solution is not very effective EDIT:
one more problem which i am having is that i c开发者_如何转开发annot read the all file to the memory its some kind of an assignment given in files management course i can only read 2 records at a time when doint the actual sortthing is this is not a binary file, options like functions as
fseek(filepointer,number of bits,SEEK_SET)
cannot help me here cause i need to jump lines
thanks in advance for your time.
The C library doesn't have a function to jump to a specific line in a file.
What you could do (short of reading the whole file into memory) is read through the file once, and store the start position of each line into an array. Using that array, you can then seek directly to the start of each line.
But you can take this further: since you sort the file based on a key, there is no need to read the full lines all the time. You could read the file once, store the line's keys plus their file position in memory and sort that.
And finally it may be useful to explore other sorting algorithms which do not require random file access (at the price of IO-bound performance) - Mergesort comes to mind.
Read the file into an array (a char *[]
) line-by-line, then sort that. Jumping around in a text file is very hard; shuffling lines around in-place is even harder.
If that takes too much memory, read the file from beginning to end, storing only the start offset of each line in an array of off_t
instead. This doesn't solve the problem of in-place modification/sorting, though, and it may become very slow when you do the actual sorting.
A heaping helping of ftell, fseek, and fsetpos might be in order to keep track of each line's position in the file.
Also, if the line are not fixed width then swapping lines in place will be extremely difficult.
Good luck.
Sequential read of text line can be done with fgets()
, fseek is of no help for text excpept if structured in fixed length.
Is mmap()
allowed ? because if so, you can parse memory and store line offsets in an array (maybe growing using realloc). Then you'll
be able to use qsort() function, working in O(n log n) instead of O(n^2) if re-reading all lines for each line.
Good luck !
The line skipping part is actually easy. To read a specific line, just seek to the beginning of the file and use a loop with fgets to get each line until you reach the one you are interested in. This is the brute force approach, and might offend your sense of aesthetics, but computers were invented to do this type of work.
There is a more elegant way to do the line skipping, but that might also be one of the problems your professor wants you to solve, so I won't mention it here.
精彩评论