我正在编写一个C模块,我遇到了一个以前从未见过的有趣的问题。
// Many other operations before this point
fseek(samples_file, 0, SEEK_SET);
printf("ftell A1 %llu\n", ftell(samples_file));
count = fwrite(channel_buffer+chan_type.size*set_index, 1, chan_type.size, samples_file);
printf("count %llu\n", count);
printf(
根据C99的说法,ftell的原型是:
long int ftell(FILE *stream);
根据我所理解的,这应该是以下几点:
fpos_t ftell(FILE *stream);
为什么会这样呢?
从第7.19.1条至第2节
fpos_t是一种对象类型,而不是数组类型,它能够记录唯一指定文件中每个位置所需的所有信息。
我知道应该使用fpos_t来记录文件中的位置。因此,在文件中返回一个位置的ftell应该是该类型的。相反,它是:
signed
long类型,它可能太小,也可能太大,无法在某些体系结构上访问文件。
我的问题是,下面的代码在其他平台上工作得很好,但在iOS 64位上就不行了。
以下代码中提供了详细信息:
//FILE* f = fopen( .. ); // f is opened and already be used successfully.
//Size of target file is near 50mb
fseek(f, 0, SEEK_END);
// print ftell(f) -> 53394002
fseek(f, -1024, SEEK_END);
// print ftell(f) -> 53392
我为Packcc解析器生成器做了一个最小的例子。在这里,解析器必须识别浮点数或整数。我尝试打印检测到的数字的位置。为了简单起见,这里没有行/列计数,只有"ftell“中的数字。 %auxil "FILE*" # The type sent to "pcc_create" for access in "ftell".
test <- line+
/
_ EOL+
line <- num _ EOL
num <- [0-9]+'.'[0-9]+ {printf
我正在尝试从二进制文件中读取数据。我得到了一个包含所有变量的结构来提取数据。我正在使用fread函数,并且我需要获取文件的总大小作为fread输入之一。我使用ftell函数来查找大小,但是当我调用ftell函数时,我得到了一个分段错误。
下面是我的代码:
#include<iostream>
#include<fstream>
#include<stdio.h>
using namespace std;
struct header{
int so_on;
int so_forth;
int etc;
};
int main(){
FIL
Glibc说fclose()、/fopen()、/fprintf()、/ftell()是线程安全的。但是,当一个线程正在写入或读取该文件,而另一个线程正在关闭该文件时,会发生什么情况?
假设我有一个像这样的函数
FILE * f; //f is opened when program starts
int log(char * str)
{
fprintf(f, "%s", str);
if (ftell(f) > SIZE_LIMIT) {
pthread_mutex_lock(&mutex);
if (ftell(f) &g
我希望在我使用CreateFile打开的现有文件上使用文件流函数(例如,"ftell") (它返回句柄)。我到处找,没有joy。我想做这样的事:
HANDLE h;
FILE *f;
int pos;
h = CreateFile( "MYFILE.TXT", ... )
f = *convert*( h, &f ); // the function I'm looking for
pos = ftell( f );
有从文件句柄获取文件流的方法吗?
下面是一段简单的代码片段,它从文件中读取行,然后返回到前一个位置并重新读取相同的行: program main
implicit none
integer :: unit, pos, stat
character(128) :: buffer
! Open file as formatted stream
open( NEWUNIT=unit, FILE="data.txt", ACCESS="stream", FORM="formatted", STATUS="old", ACTION=
这是我的C代码:
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"limits.h"
int main(int argc,char **argv){
int Array[2]={100,150};
int *pArray=Array;
int *qArray=pArray;
qArray++;
printf("pArray address:%p\n",pArray);
printf(
假设我们有以下文本文件'badpoem.txt',其中包含以下句子
Oranges and lemons,
Pineapples and tea.
Orangutans and monkeys,
Dragonflys or fleas.
我确定了每个句子的大小(以字节为单位
whos
Name Size Bytes Class Attributes
ans 1x1 8 double
fid 1x1