Optical spectral measurements are crucial for optical sensors and many other applications, but the prevailing methods, such as optical spectrum analysis and tunable laser spectroscopy, often have to make compromises among resolution, speed, and accuracy. Optical frequency combs are widely used for metrology of discrete atomic and molecular spectral lines. However, they are usually generated by optical methods and have large comb spacing, which limits the resolution for direct sampling of continuous spectra. To overcome these problems, this paper presents an original method to digitally generate an ultrafine optical frequency comb (UFOFC) as the frequency ruler for spectral measurements. Each comb line provides one sampling point, and the full spectrum can be captured at the same time using coherent detection. For an experimental demonstration, we adopted the inverse fast Fourier transform to generate a UFOFC with a comb spacing of 1.46 MHz over a 10-GHz range and demonstrated its functions using a Mach-Zehnder refractive index sensor. The UFOFC obtains a spectral resolution of 0.01 pm and response time of 0.7 ms; both represent 100-fold improvements over the state of the art and could be further enhanced by several orders of magnitude. The UFOFC presented here could facilitate new label-free sensor applications that require both high resolution and fast speed, such as measuring binding kinetics and single-molecule dynamics.