Thank you Tsjerk, this is indeed the solution I figured out as mentioned in a previous post. The only hick is that this can work only if the compiler supports large files. In that case I can use  a <span style="background-color:rgb(255,255,255);font-family:CourierNew,Courier,monospace;text-align:left">#define _FILE_OFFSET_BITS 64</span><span style="background-color:rgb(255,255,255);font-family:CourierNew,Courier,monospace;text-align:left"> </span><span style="background-color:rgb(255,255,255);font-family:CourierNew,Courier,monospace;text-align:left">and fseeko instead of fseek. I did test it with a 200Gb long file.</span><div>
<div><div style="text-align:left"><font face="CourierNew, Courier, monospace"><br></font></div><br><div class="gmail_quote">On 5 June 2012 09:45, Tsjerk Wassenaar <span dir="ltr">&lt;<a href="mailto:tsjerkw@gmail.com" target="_blank">tsjerkw@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Paolo,<br>
<br>
The python code also gives a hint about the C solution... You still<br>
don&#39;t need to read in the first frame. Bytes 81-84 from the start<br>
contain the size of the frame, excluding 92 bytes used for the header.<br>
Mind that this is only an approximate size for a frame, as the size<br>
per frame in an xtc file is variable. But it&#39;ll probably be close. If<br>
you have the size of one frame, you need the size of the file, for<br>
which you can use the solution at<br>
<a href="http://stackoverflow.com/questions/8236/how-do-you-determine-the-size-of-a-file-in-c" target="_blank">http://stackoverflow.com/questions/8236/how-do-you-determine-the-size-of-a-file-in-c</a><br>
Dividing one by the other should give an indication of the number of<br>
frames. If you have a small C program for calculating the number of<br>
frames, please do post it. It might be interesting for others.<br>
<br>
Hope it helps,<br>
<br>
Tsjerk<br>
<div class="HOEnZb"><div class="h5"><br>
On Tue, Jun 5, 2012 at 12:40 AM, Oliver Stueker &lt;<a href="mailto:ostueker@gmail.com">ostueker@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; As far as I know there is no field at the beginning of the file that would<br>
&gt; give a parser hints how many frames are in it.<br>
&gt; (probably because that makes it easier/more performant to append to the file<br>
&gt; while reducing the risk of corrupting it in case a write goes bad)<br>
&gt;<br>
&gt; On the other hand that makes it hard to implement random-access to frames in<br>
&gt; XTC/TRR files.<br>
&gt;<br>
&gt; Interestingly there is just a discussion on the mailing list of MDAnalysis<br>
&gt; (a python framework that can deal with XTC and other trajectories) on how<br>
&gt; libxdr might be extended to generate a checksum-protected index for XTC<br>
&gt; files, so that a given trajectory has to be read only once from beginning to<br>
&gt; end.<br>
&gt; <a href="https://groups.google.com/group/mdnalysis-discussion/browse_thread/thread/3cae3634c726f1ad" target="_blank">https://groups.google.com/group/mdnalysis-discussion/browse_thread/thread/3cae3634c726f1ad</a><br>

&gt;<br>
&gt;<br>
&gt; a different Oliver<br>
&gt;<br>
&gt;<br>
&gt; On Mon, Jun 4, 2012 at 3:24 PM, Paolo Franz &lt;<a href="mailto:paolo.franz@gmail.com">paolo.franz@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; I am trying to avoid doing it by brute force, that is reading all frames<br>
&gt;&gt; until the last is found. In the origin, what I really need to do is to test<br>
&gt;&gt; if a frame exists in the trajectory. I tried with xtc_seek_frame, but that<br>
&gt;&gt; does not work. Of course, if I know how many frames are they the test<br>
&gt;&gt; becomes trivial.<br>
&gt;&gt;<br>
&gt;&gt; That said, I definitely know what is in the trajectory, how many frames<br>
&gt;&gt; are there: I ran the md myself and I have the output file! What I want to do<br>
&gt;&gt; is to write a code that figure out by itself what to expect and if, by any<br>
&gt;&gt; chance I forget what is inside, it does not go into an infinite loop if I<br>
&gt;&gt; ask to analyse the wrong frame.<br>
&gt;&gt;<br>
&gt;&gt; Cheers<br>
&gt;&gt; Paolo<br>
&gt;&gt;<br>
&gt;&gt; On 4 June 2012 22:59, Justin A. Lemkul &lt;<a href="mailto:jalemkul@vt.edu">jalemkul@vt.edu</a>&gt; wrote:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; If all you need is the number of frames contained in an .xtc file, is<br>
&gt;&gt;&gt; there some reason why running gmxcheck on the .xtc file is insufficient?<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; -Justin<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; On 6/4/12 4:56 PM, Paolo Franz wrote:<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; Hi Tsjerk,<br>
&gt;&gt;&gt;&gt; Thanks, but I don&#39;t really want to use a python script, I am doing this<br>
&gt;&gt;&gt;&gt; from<br>
&gt;&gt;&gt;&gt; some c/c++ code. I think I figured out a way to do it, but I haven&#39;t<br>
&gt;&gt;&gt;&gt; tested it yet:<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; i)    open the file<br>
&gt;&gt;&gt;&gt; ii)   do a read_first_xtc<br>
&gt;&gt;&gt;&gt; iii)  then get the file pointer positon from ftellg, which should be the<br>
&gt;&gt;&gt;&gt; length<br>
&gt;&gt;&gt;&gt; of the frame in bytes;<br>
&gt;&gt;&gt;&gt; iv)  place the file pointer at the end of the file with an fseek, then<br>
&gt;&gt;&gt;&gt; get the<br>
&gt;&gt;&gt;&gt; length with an ftellg<br>
&gt;&gt;&gt;&gt; v)   Divide the total length by the length of a frame and obtain the<br>
&gt;&gt;&gt;&gt; number of<br>
&gt;&gt;&gt;&gt; written frames.<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; I am only wondering what to do when the length in bytes of the file is<br>
&gt;&gt;&gt;&gt; too large<br>
&gt;&gt;&gt;&gt; for a long int!<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; On 4 June 2012 16:11, Tsjerk Wassenaar &lt;<a href="mailto:tsjerkw@gmail.com">tsjerkw@gmail.com</a><br>
&gt;&gt;&gt;&gt; &lt;mailto:<a href="mailto:tsjerkw@gmail.com">tsjerkw@gmail.com</a>&gt;&gt; wrote:<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;    Hey Paolo,<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;    I think I posted a script for extracting a last frame before, but if<br>
&gt;&gt;&gt;&gt; I<br>
&gt;&gt;&gt;&gt;    can&#39;t even find it myself... Here it is:<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;    #!/usr/bin/env python<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;    from struct import unpack<br>
&gt;&gt;&gt;&gt;    import sys<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;    def i(x): return sum([ord(x[j])&lt;&lt;(24-j*8) for j in range(4)])<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;    f = open(sys.argv[1])<br>
&gt;&gt;&gt;&gt;    tag = f.read(8)                   # Tag: magic number and number of<br>
&gt;&gt;&gt;&gt; atoms<br>
&gt;&gt;&gt;&gt;    n = 92 + i(f.read(84)[-4:])       # Size of frame in bytes<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;    f.seek(-5*n/4, 2)                 # This should contain a complete<br>
&gt;&gt;&gt;&gt; frame<br>
&gt;&gt;&gt;&gt;    frame = f.read()                  # Read the remaining part in<br>
&gt;&gt;&gt;&gt;    frame = frame[frame.index(tag):]  # Find the tag<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;    # Open the output file<br>
&gt;&gt;&gt;&gt;    if len(sys.argv) &gt; 2:<br>
&gt;&gt;&gt;&gt;        o = sys.argv[2]<br>
&gt;&gt;&gt;&gt;    else:<br>
&gt;&gt;&gt;&gt;        o = sys.argv[1][:-4]+&quot;-last.xtc&quot;<br>
&gt;&gt;&gt;&gt;    open(o,&quot;w&quot;).write(frame)<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;    ###<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;    Hope it helps. Cheers,<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;    Tsjerk<br>
&gt;&gt;&gt;&gt;    On Mon, Jun 4, 2012 at 12:59 PM, Paolo Franz &lt;<a href="mailto:paolo.franz@gmail.com">paolo.franz@gmail.com</a><br>
&gt;&gt;&gt;&gt;    &lt;mailto:<a href="mailto:paolo.franz@gmail.com">paolo.franz@gmail.com</a>&gt;&gt; wrote:<br>
&gt;&gt;&gt;&gt;     &gt; Hello everybody!<br>
&gt;&gt;&gt;&gt;     &gt;<br>
&gt;&gt;&gt;&gt;     &gt; I am wondering how I can figure out the number of frames contained<br>
&gt;&gt;&gt;&gt; in an<br>
&gt;&gt;&gt;&gt;     &gt; .xtc file. Indeed, I need to read a particular frame of a<br>
&gt;&gt;&gt;&gt; trajectory and I<br>
&gt;&gt;&gt;&gt;     &gt; thought that the function<br>
&gt;&gt;&gt;&gt;     &gt; xtc_seek_frame(FILE * , int *, int *)<br>
&gt;&gt;&gt;&gt;     &gt; would return 0 if the frame was there and 1 when it was not.<br>
&gt;&gt;&gt;&gt; Instead, if I<br>
&gt;&gt;&gt;&gt;     &gt; call it with a frame outside the boundaries it seems to go into an<br>
&gt;&gt;&gt;&gt; infinite<br>
&gt;&gt;&gt;&gt;     &gt; loop. What I am doing wrong? Is there a way to read the last frame<br>
&gt;&gt;&gt;&gt; of an<br>
&gt;&gt;&gt;&gt;     &gt; .xtc file?<br>
&gt;&gt;&gt;&gt;     &gt;<br>
&gt;&gt;&gt;&gt;     &gt; Sincerely<br>
&gt;&gt;&gt;&gt;     &gt; Paolo<br>
&gt;&gt;&gt;&gt;     &gt;<br>
&gt;<br>
&gt;<br>
</div></div><div class="im HOEnZb">&gt; --<br>
&gt; gmx-developers mailing list<br>
&gt; <a href="mailto:gmx-developers@gromacs.org">gmx-developers@gromacs.org</a><br>
&gt; <a href="http://lists.gromacs.org/mailman/listinfo/gmx-developers" target="_blank">http://lists.gromacs.org/mailman/listinfo/gmx-developers</a><br>
&gt; Please don&#39;t post (un)subscribe requests to the list. Use the<br>
&gt; www interface or send it to <a href="mailto:gmx-developers-request@gromacs.org">gmx-developers-request@gromacs.org</a>.<br>
<br>
<br>
<br>
</div><div class="im HOEnZb">--<br>
Tsjerk A. Wassenaar, Ph.D.<br>
<br>
post-doctoral researcher<br>
Molecular Dynamics Group<br>
* Groningen Institute for Biomolecular Research and Biotechnology<br>
* Zernike Institute for Advanced Materials<br>
University of Groningen<br>
The Netherlands<br>
--<br>
gmx-developers mailing list<br>
<a href="mailto:gmx-developers@gromacs.org">gmx-developers@gromacs.org</a><br>
</div><div class="HOEnZb"><div class="h5"><a href="http://lists.gromacs.org/mailman/listinfo/gmx-developers" target="_blank">http://lists.gromacs.org/mailman/listinfo/gmx-developers</a><br>
Please don&#39;t post (un)subscribe requests to the list. Use the<br>
www interface or send it to <a href="mailto:gmx-developers-request@gromacs.org">gmx-developers-request@gromacs.org</a>.<br>
</div></div></blockquote></div><br></div></div>