當前位置:首頁 » 編程語言 » c語言文件長度

c語言文件長度

發布時間: 2022-08-18 09:58:10

c語言讀寫文件,文件名長度有限制嗎

有限制的,c庫函數里有一種結構體_finddata_t 專門存儲文件的各種信息,具體定義如下:
struct _finddata_t
{ unsigned attrib;
time_t time_access;
time_t time_write;
_fsize_t size;
char name[MAX_FNAME];
name[MAX_FNAME]就是存儲的文件名。 }
其中_MAX_FNAME是一個常量宏,在stdlib.h文件中被定義,表示的是文件名的最大長度!!

都是原創的哦,不是復制粘貼過來的!!

② c語言如何計算文件大小

#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE*fp;
int a;
if((fp=fopen("1.txt","rb"))==NULL)
{
printf("此文件無法打開");
exit(0);
}
fseek(fp,0,2);
a=ftell(fp);
printf("%d\n",a);
fclose(fp);
}
望採納!

③ C語言 文本文件長度

long get_file_size( FILE* fp )
{
if (fp==NULL) return -1;
fseek( fp, 0L, SEEK_END );
return ftell(fp);
}

④ 如何用C語言獲取文件的大小

intfile_size(char*filename)

{

FILE*fp=fopen(filename,"r");

if(!fp)return-1;

fseek(fp,0L,SEEK_END);

intsize=ftell(fp);

fclose(fp);

returnsize;

}

(4)c語言文件長度擴展閱讀

C語言獲取文件長度及全部內容

FILE*fp;

fp=fopen("localfile","rb");//localfile文件名

fseek(fp,0L,SEEK_END);/*定位到文件末尾*/

flen=ftell(fp);/*得到文件大小*/

p=(char*)malloc(flen+1);/*根據文件大小動態分配內存空間*/

if(p==NULL)

{

fclose(fp);

return0;

}

fseek(fp,0L,SEEK_SET);/*定位到文件開頭*/

fread(p,flen,1,fp);/*一次性讀取全部文件內容*/

p[flen]=0;/*字元串結束標志*/

⑤ C語言將文件長度截斷為0是什麼意思

將一個現有文件的長度截斷為len。如果以前文件長度大於len,超過len的部分將不能再訪問
長度截斷為0相當於將文件內的數據全部刪除。

⑥ 【C語言】計算文件長度,把文件內容讀取到內存中

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
main ()
{
string str;
ifstream f1 ( "1.txt" );
if (f1.fail())
{
cerr<<"open failure on 1.txt"<<endl;
return 1;
}
ifstream f2 ( "2.txt" );
if (f2.fail())
{
cerr<<"open failure on 2.txt"<<endl;
return 1;
}
ofstream out ( "3.txt" );
if (out.fail())
{
cerr<<"open failure on 3.txt"<<endl;
return 1;
}
while ( getline (f1, str))
{
out<<str<<endl;
}
while ( getline (f2,str))
{
out<<str<<endl;
}
f1.close();
f2.close();
out.close ();
return 0;
}
可以參照這個試試

⑦ c語言求文件長度,ftell得到文件長度為-1

C語言獲取文件長度及全部內容,參考代碼如下:


FILE*fp;
fp=fopen("localfile","rb");//localfile文件名
fseek(fp,0L,SEEK_END);/*定位到文件末尾*/
flen=ftell(fp);/*得到文件大小*/
p=(char*)malloc(flen+1);/*根據文件大小動態分配內存空間*/
if(p==NULL)
{
fclose(fp);
return0;
}
fseek(fp,0L,SEEK_SET);/*定位到文件開頭*/
fread(p,flen,1,fp);/*一次性讀取全部文件內容*/
p[flen]=0;/*字元串結束標志*/


all:strchange.o
gcc-ostrchange.o-cstrchange.c
strchange.o:strchange.c
gcc-ostrchangestrchange.o
clean:
rm-rfstrchange*.o

⑧ 用純C語言取得文件長度

第一種方法: 也可以讀取一個不定長的文件。
FILE *pFile = fopen( pFilePath, \"r\" );
if ( pFile == NULL )
{
return 0;
}
fseek( pFile, 0, SEEK_END );
iFileLen = ftell( pFile );
rewind( pFile );
m_pFileText = new char[iFileLen+1];
fread( m_pFileText, 1, iFileLen, pFile );
m_pFileText[iFileLen] = 0;
fclose( pFile );

第二種方法:
// 計算字元個數
FILE *pFile = fopen( pFilePath, \"r\" );
char ch;
int num = 0;
while ( ch = getc( pFile ) != EOF )
{
num++ ;
}
fclose( pFile );

⑨ C語言獲取txt文件大小兩種方法的差異

我測試了你的代碼:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
voidmain()
{
FILE*p=fopen("d:\jiuibf.txt","rt");
intlength=0;
for(;fgetc(p)!=EOF;length++);
fclose(p);
printf("第一種方式,文件長度=%d ",length);
p=fopen("d:\jiuibf.txt","rb");
fseek(p,0,2);
length=ftell(p);
fclose(p);
printf("第二種方式,文件長度=%d ",length);
}

文本文件的內容是:

FILE *p=("jiuibf.txt","rt");

int length;

for(;fgetc(p)!=EOF;length);

FILE *p=(jiuibf.txt","rb");

int length;

fseek(p,0,2);

length=ftell(p);

程序的輸出是:

FILE*fp;
fp=fopen("localfile","rb");//localfile文件名
fseek(fp,0,SEEK_SET);
fseek(fp,0,SEEK_END);
longlongBytes=ftell(fp);//longBytes就是文件的長度

⑩ C語言中如何方便地得到文件長度

告訴你一個最方便的函數:stat,例:
struct stat fileData;
if (0 == stat("C:\log.txt", &fileData))
{
printf("file size %u.", fileData.st_size);
}

熱點內容
美國雲伺服器主機 發布:2024-04-19 22:28:54 瀏覽:139
抗生素資料庫 發布:2024-04-19 22:13:03 瀏覽:495
晚晚教編程 發布:2024-04-19 21:56:23 瀏覽:712
安卓換蘋果語音留言怎麼看 發布:2024-04-19 21:56:21 瀏覽:627
解壓神經 發布:2024-04-19 21:47:03 瀏覽:894
c語言字元轉義字元 發布:2024-04-19 21:43:51 瀏覽:727
mysql存儲過程語法 發布:2024-04-19 21:00:04 瀏覽:245
修復損壞的壓縮文件 發布:2024-04-19 20:53:32 瀏覽:423
編程發型 發布:2024-04-19 20:53:28 瀏覽:500
去除空格sql 發布:2024-04-19 20:43:30 瀏覽:785