Question about file seeking position
The offset parameter of lseek
is of type off_t
. In 32-bit compilation environments, this type defaults to a 32-bit signed integer - however, if you compile with this macro defined before all system includes:
#define _FILE_OFFSET_BITS 64
...then off_t
will be a 64-bit signed type.
For fseek
, the fseeko
function is identical except that it uses the off_t
type for the offset, which allows the above solution to work with it too.
Simple question on file seeking in C
You would normally use a binary format. One way would be to allocate a certain amount of space as a header in the file. Here, you put the mesh numbers, vertex and index counts, and an offset into the file where the vertex data begins. You read the header when loading the file, then seek to the appropriate place to read the data you want.
File seeking with SML Basis
This is tricky. Unfortunately, seeking isn't directly supported. Moreover, file positions are only transparent for binary files, i.e., those that you have opened with the BinIO
structure [1]. For this structure, the corresponding type BinIO.StreamIO.pos
is defined to be Position.int
, which is some integer type.
However, in an SML system that supports the complete I/O stack from the standard you should be able to synthesise the following seek function using the lower I/O layers:
(* seekIn : BinIO.instream * Position.int -> unit *)
fun seekIn(instream, pos) =
case BinIO.StreamIO.getReader(BinIO.getInstream instream) of
(reader as BinPrimIO.RD{setPos = SOME f, ...}, _) =>
( f pos;
BinIO.setInstream(instream,
BinIO.StreamIO.mkInstream(reader, Word8Vector.fromList[]))
)
| (BinPrimIO.RD{name, ...}, _) =>
raise IO.Io{
name = name,
function = "seekIn",
cause = IO.RandomAccessNotSupported
}
Use it like:
val file = BinIO.openIn "filename"
val _ = seekIn(file, 200)
val bin = BinIO.inputN(file, 1000)
If you need to convert from Word8Vector to string:
val s = Byte.bytesToString bin
You can do the equivalent for out streams as well.
[1] http://standardml.org/Basis/bin-io.html#BIN_IO:SIG:SPEC
Fast accessing file position in ifs() C++
You can't do this with newline translation enabled (what the Standard calls "text mode"), because seeking back to the position requires the standard library to scan through the entire front of the file to find N characters-not-double-counting-newlines. Translations of variable length encodings (e.g. between UTF-8 and UCS) cause a similar problem.
The solution is to turn off newline translation (what the Standard calls "binary mode") and any other translations that involve variable-length encodings, and handle these yourself. With all translations turned off, the "file position" is the number directly used by the OS to perform file I/O, and therefore has the potential to be very efficient (whether it actually is efficient depends on the standard library implementation details).
Creating a TXT file and seeking a position in Python
You don't have to seek inside the file to print your data:
signal1 = 'speed'
bins1 = [0, 10, 20, 30, 40]
signal2 = 'rpm'
bins2 = [0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500]
hist_result = [ [1, 4, 5, 12],
[-5, 8, 9, 0],
[-6, 7, 11, 19],
[1, 4, 5, 12],
[-5, 8, 9, 0],
[-6, 7, 11, 19],
[1, 4, 5, 12],
[-5, 8, 9, 0],
[-6, 7, 11, 19],
]
with open('data.txt', 'w') as f_out:
print('\t{signal1}>=\t{bins}'.format(signal1=signal1, bins='\t'.join(map(str,bins1[:-1]))), file=f_out)
print('\t{signal1}<\t{bins}'.format(signal1=signal1, bins='\t'.join(map(str,bins1[1:]))), file=f_out)
print('{signal2}>=\t{signal2}<'.format(signal2=signal2))
for a, b, data in zip(bins2[:-1], bins2[1:], hist_result):
print(a, b, *data, sep='\t', file=f_out)
Creates data.txt
:
speed>= 0 10 20 30
speed< 10 20 30 40
rpm>= rpm<
0 500 1 4 5 12
500 1000 -5 8 9 0
1000 1500 -6 7 11 19
1500 2000 1 4 5 12
2000 2500 -5 8 9 0
2500 3000 -6 7 11 19
3000 3500 1 4 5 12
3500 4000 -5 8 9 0
4000 4500 -6 7 11 19
Seeking to a line in a file in g++
You can seek to a position in a file, but that position must be a character offset from the start, end or current position - see for example fseek(). There is no way of seeking to a particular line, unless all the lines are exactly the same length.
fseek, what does it mean to be to the last byte of a file?
-1 appears to be correct. I just tried it, and an fgetc
after fseek(f, -1, SEEK_END)
produced the last character of the file. Seeking to 0 relative to SEEK_END
resulted in fgetc
returning EOF
.
Clearly pointing to the last byte of a file is different from pointing to the end of the file. Furthermore, if there is any ambiguity in terminology, it is a failure of the test, not of the student. If the student has demonstrated the expected knowledge, they should receive credit.
Related Topics
Core Dump Filename Gets Thread Name Instead of Executable Name with Core_Pattern %E.%P.Core
Are Pid-Files Still Flawed When Doing It 'Right'
Why Does If [ !$(Grep -Q) ] Not Work When If Grep -Q Does
What Really Is the "Linger Time" That Can Be Set with So_Linger on Sockets
Provide Password Using Shell Script
How Can a Process Try to Access Other Process's Memory in Linux Virtual Memory System
How Can Linux Ptrace Be Unsafe or Contain a Race Condition
Scp: How to Find Out That Copying Was Finished
Linux Kernel API Changes/Additions
How to Reserve Virtual Memory in Linux
How to Retrieve Advertising Payload from Ibeacon/Ble
Unterminated Address Regex While Using Sed
G++ Conio.H: No Such File or Directory
Looping Through Lines in a File in Bash, Without Using Stdin
Automake Subdir-Objects Is Disabled