ftell,C语言如何读取文件中指定的某一段
ftell,C语言如何读取文件中指定的某一段详细介绍
本文目录一览: ftell 的用法
ftell
函数名: ftell
功 能: 返回当前文件指针
用 法: long ftell(FILE *stream);
程序例:
#include
int main(void)
{
FILE *stream;
stream = fopen("MYFILE.TXT", "w+");
fprintf(stream, "This is a test");
printf("The file pointer is at byte \
%ld\n", ftell(stream));
fclose(stream);
return 0;
}
ftell() 和 fseek() 用长整型表示文件内的偏移 (位置), 因此, 偏移量被限制在 20 亿 (231-1) 以内
C语言图片里画线的,length=ftell(fp)的值怎么算的,怎么的算法
ftell:是获取当前文件指针位置。
在例题中先使用fseek移动到文件的末尾了,所以ftell函数返回的值的大小是根据
你传入文件大小而定的。在例题首先gets(filename);
意思:如果你传入的一个文件大小1000字节的文件名,该例题打印的值就是1000,
fseek(fp, 0L,SEEK_END); 将文件的当前读写位置指针移到文件的末尾。
length=ftell(fp); ftell()函数得到文件当前读写位置指针相对于文件首的偏移字节数,该位移值等于文件所含字节数。
函数 ftell 用于得到文件位置指针当前位置相对于文件首的偏移字节数。
用 fseek把文件指针移到文件末尾,再用 ftell得到从头到尾的字节数,就是文件的总字节数。
函数ftellfp的作用是
函数ftellfp的作用是指向文件的当前读写位置。函数ftellfp的作用是得到fp所指向文件的当前读写位置,即位置指针的当前值,因此函数ftellfp的作用是指向文件的当前读写位置。函数是指将一组语句的集合通过一个函数名封装起来,想要执行这个函数,只需调用其函数名即可。
C语言中frewind, fseek, ftell怎么用?
我感觉如果是二进制模式打开二进制文件的话可能会不一样吧,如果文件中存储的是二进制的整数,在文本模式下打开的时候每个字节只对应这个整数十进制中的一位,这样一个5位的十进制数就可能占5个字节,而二进制下只用4个字节来存储,也就只占4个字节。
rewind就是把当前文件指针移动到文件开始,fseek文件定位,ftell返回文件当前指针。
如rewind(fp);fp为文件指针意思是把指针回到文件开始;fseek(fp,nL,1)这个有三种情况0表示文件开始,1表示当前位置,2表示文件末尾,nL表示指针位移量可以为负;ftell(fp)返回文件当前的指针
C++读文件时,ftell显示从0开始读,但是读完后为什么ftell返回文件大小FILESIZE,而不是FILESIZE - 1呢?
ftell 输出当前文件指针位置,
例如 读前 ftell 输出 0,1,2,3,4,读后ftell 输出1,2,3,4,5
最前 读前 ftell 输出 0,文件指针位置 在 SEEK_SET
最后 读后ftell 输出的5 正好是 文件大小 FILESIZE,文件指针位置 在 SEEK_END
你可以自己用循环语句读前读后 输出一下就清楚了。
因为当前指针已经移动到文件后面去了啊,并没有在最后一个字节停下来
使用ftell获取filesize仅仅是根据ftell本身的特性,进行的一种特殊应用,ftell的主要功能是在随机读写的时候帮助确定文件句柄中当前位置到文件首部的偏移量,使用ftell获取filesize的方式,本身就需要结合fseek(fp, 0L,SEEK_END);这种方式,那么EOF这个位置到0一定是filesize,而不是filesize-1
C语言获得文件大小时ftell始终返回-1
long GetFileLength(FILE* fileptr) {
if (fileptr == NULL) return 0; /*fileptr=NULL是赋值语句,应为==*/
long fOffset = ftell(fileptr);
if (fseek(fileptr, 0, SEEK_END)<0) return 0; /*fseek函数失败时返回负值*/
long file_size = ftell(fileptr);
fseek(fileptr, fOffset, SEEK_SET);
return file_size;
}
C语言(谭浩强)里面文件那块ftell()函数具体什么时候会出现返回值为-1L的情况???请高手指教!!
如果文件fp不存在则会返回-1,这是正常的,函数里面进行处理了,比如
int ftell(fp)
{
if( fileExist(fp) )//假定 fileExist 函数可以检测文件是否存在
return (-1);
else
return (0);
........
}
如果不进行文件检测处理,那么指定一个不存在的文件名,并对该文件进行读写操作就会出错,可以看看c++里的异常处理,可以捕获程序异常,并进行相应处理,不至于程序崩溃
C语言如何读取文件中指定的某一段
只单纯读文件某一段,首先要知道这段内容开头的位置并定位文件指针,还要知道内容长度,这个除非你读取前就知道,否则不如直接读取全文或者一段一段读取,然后用遍历字符串判断的方法把你题目中说的B开头ENDB结尾的内容。
#include
#include
#define TBEGIN "B"
#define TEND "END B"
void main() { FILE *fp; char buffer[256];
if ( fp=fopen("c:\\test.txt","r") ) {
while ( !feof(fp) ) { fgets(buffer,255,fp); if ( strcmp(buffer,TBEGIN)==0 ) break; }
while ( !feof(fp) ) {
fgets(buffer,255,fp); if ( strcmp(buffer,TEND)==0 ) break; else printf("%s\n",buffer);
}
fclose(fp);
} else printf("无法打开文本文件.\n");
}
#include
//从文件p中接收第n行字串,保存在str中
//假设一行的文本内容不超过1000字符,如果估计超过,请自行修改函数中的1000
//如果打不开文件,返回NULL
//如果文件不足n行,返回NULL
//如果正获得正常数据,返回str的地址
char *getfileline(char *p,int n, char *str){
FILE *fp;
int i;
if ((fp=fopen(p,"r"))==NULL){
printf("打开文件错误\n");
return NULL;
}
for(i=1;i
<n;i++)
if ((fgets(str,1000,fp))==NULL) {
fclose(fp);
return NULL;
}
fgets(str,1000,fp);
fclose(fp);
return str;
}
int main(){
char p[1000];
getfileline("d:\\temp.txt", 10, p);
puts(p);
}
你可以使用条件调用指令,使用某个条件直接调用所需要的文件段就可以了
用.ini文件来存储。
比如内容是:
[UserInfo]
UserName=admin
读取方式:
char achUserName[20];
GetPrivateProfileString("UserInfo","UserName",NULL,achUserName,20,"c:\\userInfo.ini");
使用fread函数读取指定长度的字符串,即使包含\n也会被读取,可以首先使用fseek定位到文件结尾,然后ftell函数返回的值就是文件的大小,这样就可以用循环多次读取文件,直到读取所有内容
FILE *file = NULL;
char szFile[1025] = {0};
int nHadRead = 0;
file = fopen( "file.txt", "r+");
if ( file == NULL )
return;
fseek( file, 0, SEEK_END ); //定位到文件尾
int nLen = ftell( file ); //获取当前位置,即文件长度
fseek( file 0, SEEK_SET ); //重新定位到文件开头,准备开始读
while ( nHadRead < nLen )
{
int nRead = nLen - nHadRead >1024 ? 1024 : nLen - nHadRead; //如果剩余小于1024字节,则读剩余字节,否则每次读取1024字节。
int nTmp = fread( szFile, 1, nRead , file );
nHadRead += nTmp;
printf( "%s", szFile );
memset( szFile, 0x0, sizeof(szFile) );
}
fclose(file);
大致过程就是这样,纯手打,没有调试过,可能有错
</n;i++)
fread读取与ftell显示字节数不一致的问题
应该是这样。0x0a等于字符\n,ftell把它当成字符处理了.
StackOverFlow上有人碰到过:
'\n' can be represented with two characters, so there is the skew you are getting.
If you don't want that to happen, open the finaly in binary mode.
ftell的说明:
or binary streams, the value returned corresponds to the number of bytes from the beginning of the file. For text streams, the value is not guaranteed to be the exact number of bytes from the beginning of the file, but the value returned can still be used to restore the position indicator to this position using fseek.
c语言 那个输入文件放在哪里啊
要放在本工程的目录下面,也就是说你保存C原文件的那个地方,你可以点另存为,找到那个地方。
应该放在你安装软件的默认位置下,里面有个lib或者是Include文件夹,放在那就可以了
改成这样:
#include
#include
typedef struct student
{
char name[10];
int age;
char sex;
int ID;
float GPA;
} student;
student stu1[5];
void main()
{
FILE *fp1,*fp2;
int i=0;
char tname[10],tsex;
int tage,tGPA,tID;
fp1=fopen("1.txt","r");
if(fp1==NULL)
{
printf("Fail to open the infile\n");
}
fp2=fopen("2.txt","wb");
if(fp2==NULL)
{
printf("fail to open the outfile\n");
}
while(1==1)
{
if (fscanf(fp1,"%s %d %s %d %f",
tname,&tage,&tsex,&tID,&tGPA)==EOF) break;
strcpy(stu1[i].name,tname);
stu1[i].age=tage;
stu1[i].sex=tsex;
stu1[i].ID=tID;
stu1[i].GPA=tGPA;
if(stu1[i].age>1987)
fwrite(&stu1[i],sizeof(student),1,fp2);
i++;
}
fclose(fp1);
fclose(fp2);
}
改成这样:
#include
#include
typedef struct student
{
char name[10];
int age;
char sex;
int ID;
float GPA;
} student;
student stu1[5];
void main()
{
FILE *fp1,*fp2;
int i=0;
char tname[10],tsex;
int tage,tGPA,tID;
fp1=fopen("1.txt","r");
if(fp1==NULL)
{
printf("Fail to open the infile\n");
}
fp2=fopen("2.txt","wb");
if(fp2==NULL)
{
printf("fail to open the outfile\n");
}
while(1==1)
{
if (fscanf(fp1,"%s %d %s %d %f",
tname,&tage,&tsex,&tID,&tGPA)==EOF) break;
strcpy(stu1[i].name,tname);
stu1[i].age=tage;
stu1[i].sex=tsex;
stu1[i].ID=tID;
stu1[i].GPA=tGPA;
if(stu1[i].age>1987)
fwrite(&stu1[i],sizeof(student),1,fp2);
i++;
}
fclose(fp1);
fclose(fp2);
}
希望对你有帮助
使用C语言的文件操作函数可以读写txt文件,如果使用相对路径,文件必须放在程序相同的文件夹内。
1、C语言标准库提供了一系列文件操作函数。文件操作函数一般以f+单词的形式来命名(f是file的简写),其声明位于stdio.h头文件当中。例如:fopen、fclose函数用于文件打开与关闭;fscanf、fgets函数用于文件读取;fprintf、fputs函数用于文件写入;ftell、fseek函数用于文件操作位置的获取与设置。
2、例程:
#include
int a;char b,c[100];int main(){ FILE * fp1 = fopen("input.txt", "r");//打开输入文件 FILE * fp2 = fopen("output.txt", "w");//打开输出文件 if (fp1==NULL || fp2==NULL) {//若打开文件失败则退出 puts("不能打开文件!"); rturn 0; } fscanf(fp1,"%d",&a);//从输入文件读取一个整数 b=fgetc(fp1);//从输入文件读取一个字符 fgets(c,100,fp1);//从输入文件读取一行字符串 printf("%ld",ftell(fp1));//输出fp1指针当前位置相对于文件首的偏移字节数 fputs(c,fp2);//向输出文件写入一行字符串 fputc(b,fp2);//向输出文件写入一个字符 fprintf(fp2,"%d",a);//向输出文件写入一个整数 fclose(fp1);//关闭输入文件 fclose(fp2);//关闭输出文件,相当于保存 return 0;}