c分段读取文件内容|C读取文件内容

|

⑴ C语言怎样将数字从文件里逐个读取出来

可以通过fscanf,从文件中依次读取数据。当fscanf返回EOF时,表示读到文件结尾,这时停止读专取即可。属以文件中存储的为空格分隔的整型字符为例,可以写作:while(fscanf(fp,"%d",&n) != EOF)//从打开的文件指针fp指向的文件读数据,每次读一个整型,直至文件结尾。 printf("%d ", n);

⑵ delphi7分段读取文本

var strList:TStrings; ReadString:String;begin strList:=TStringList.Create; strList.LoadFromFile('文件路径'); //捞文件内容,即将文件内容赋值给strList Memo1.Text:=Copy(strList.Text,1,Pos('#',strList.Text)-1); // Pos('#',strList.Text)表示#在strList内容中第一次出现的位置;Copy(strList.Text,1,Pos('#',strList.Text)-1)表示截取strList中第一个字符位置到第一次出现'#'的位置(前一个字符位置)之间的内容。 ReadString:=Copy(strList.Text,Pos('#',strList.Text)+1,Length(strList.Text)-Pos('#',strList.Text)); //截取strList(文件)内容中第一次出现#後的内容,并将其值赋给ReadString; Memo2.Text:=Copy(ReadString,1,Pos('#',ReadString)); //原理同上,截取ReadString中第一个字符位置到第一次出现#位置之间的内容,并将值赋给Memo2。 …….end;若还有疑问,请Hi我。

⑶ C语言如何读取txt文本里面的内容

C语言可以使用fopen()函数读取txt文本里。

示例:

#include <stdio.h>

FILE *stream, *stream2;

void main( void )

{

int numclosed;

/* Open for read (will fail if file "data" does not exist) */

if( (stream = fopen( "data", "r" )) == NULL )

printf( "The file 'data' was not opened" );

else

printf( "The file 'data' was opened" );

/* Open for write */

if( (stream2 = fopen( "data2", "w+" )) == NULL )

printf( "The file 'data2' was not opened" );

else

printf( "The file 'data2' was opened" );

/* Close stream */

if(fclose( stream2 ))

printf( "The file 'data2' was not closed" );

/* All other files are closed: */

numclosed = _fcloseall( );

printf( "Number of files closed by _fcloseall: %u", numclosed );

}

(3)c分段读取文件内容扩展阅读

使用fgetc函数

#include <stdio.h>

#include <stdlib.h>

void main( void )

{

FILE *stream;

char buffer[81];

int i, ch;

/* Open file to read line from: */

if( (stream = fopen( "fgetc.c", "r" )) == NULL )

exit( 0 );

/* Read in first 80 characters and place them in "buffer": */

ch = fgetc( stream );

for( i=0; (i < 80 ) && ( feof( stream ) == 0 ); i++ )

{

buffer[i] = (char)ch;

ch = fgetc( stream );

}

/* Add null to end string */

buffer[i] = '';

printf( "%s", buffer );

fclose( stream );

}

⑷ 在C语言中怎么将文件中的数据分次读取,用FILE *XX的形式。

用fgets()就可以 这是可以测试文件 因为测试 所以我保证了 D盘文件存在 没做文件打开检查 你看看 如果符合望采纳#include<stdio.h>void main(){ FILE *fp; char str[3][10]; int i,j; fp=fopen("D:\\1.txt","r"); for(i=0;i<3;i++) fgets(&str[i][0],9,fp); for(i=0;i<3;i++) printf(str[i]);}

⑸ c语言分段读取文本文件

#include"stdio.h"intmain(){FILE*pf=NULL;//文件指针intfilelen=0;inti=0;char*buf;pf=fopen("D:\test.txt","r");//以只读方式打开文件if(pf==NULL){return0;}else{//获得文件长度fseek(pf,0,SEEK_END);//文件指针移到末尾filelen=ftell(pf);//获得文件当前指针位置,即为文件长度rewind(pf);//将文件指针移到开头,准备读取buf=malloc(filelen+1);//新建缓冲区,存储独处的数据//将缓冲区的数据设置为0for(i=0;i<filelen+1;i++)buf[i]=0;//读取文件fread(buf,filelen,1,pf);//关闭文件fclose(pf);//buf中即为要读出的数据printf("%s",buf);//输出一下数据,你可以随便怎么用free(buf);//最后记得要释放}return1;}

⑹ C语言中如何读取以逗号分割的文件中的数据

在C语言中,可以使用格式化输入函数fscnaf来解决上述问题,例如有文本文件的数据保存方式为:12,1334,56……则在fscnaf中可以按如下方法输入:fscanf(fp,"%d,%d\n",&a,&b);

⑺ VC++如何把文件中的数据分段读出来

可以不过要每行有固定的分隔符你是说分行读取还是分分隔符读取?分行读取的话用fgets();分字符段读取用strtok();今天下午刚写了一个这样的程序

⑻ C读取文件内容

C读取文件内容参考代码如下:

#include<stdio.h>#include<stdlib.h>#include<string.h>#defineMAX_LINE1024intmain(){charbuf[MAX_LINE];/*缓冲区*/FILE*fp;/*文件指针*/intlen;/*行字符个版数*/if((fp=fopen("test.txt","r"))==NULL){perror("failtoread");exit(1);}while(fgets(buf,MAX_LINE,fp)!权=NULL){len=strlen(buf);buf[len-1]='';/*去掉换行符*/printf("%s%d",buf,len-1);}return0;}

⑼ c语言 如何只读取文件部分内容

读出来不要就行了啊

假设你的文件是t1.txt,在d:存放,把前面两个字段存到内另一个文件容t2.txt里

#include<stdio.h>intmain(){FILE*fpi,*fpo;chars1[30];chars2[30];charoo[30];fpi=fopen("d:\t1.txt","r");if(!fpi){printf("打开源文件失败");return555;}fpo=fopen("d:\t2.txt","w");if(!fpo){printf("打开目标文件失败");return333;}while(!feof(fpi)){fscanf(fpi,"%s%s%s",s1,s2,oo);fprintf(fpo,"%s%s",s1,s2);//不要oo了}fclose(fpi);fclose(fpo);return0;}

⑽ C语言分行读取txt文件中的数据 要求如下: txt文件中的数据: 11 34 54

#include<ctype.h>/*使用isdigit(),isspace()函数*/#include<float.h>/*使用DBL_MAX定义*/#include<stdio.h>voidread_data(FILE*fp,double*a,double*b,int*c,int*d,int*e){charbuf[256],ch;double*dp=a;int*ip[]={c,d,e,NULL};intpos,pos2=0,isa=1;while(!feof(fp)&&ip[pos2]!=NULL){while(!feof(fp)&&isspace(fgetc(fp)));/*跳过空字符*/fseek(fp,-1,SEEK_CUR);/*文件指针向前移1*/pos=0;do{ch=fgetc(fp);if(isdigit(ch)||ch=='.'||ch=='+'||ch=='-'||ch=='e'||ch=='E'||ch==''||ch==''){buf[pos++]=ch;/*只读取数字,小数点,正负号,科学计数符号*/}}while(!feof(fp)&&buf[pos-1]!=''&&buf[pos-1]!='');/*逐个读取直到遇到空字符或文件尾*/buf[pos]=0;if(dp!=NULL)sscanf(buf,"%lf",dp++);/*读取double浮点数*/elsesscanf(buf,"%d",ip[pos2++]);/*读取整数*/if(buf[pos-1]==''&&*dp!=NULL)/*遇到换行符*/{if(isa==1){*dp=DBL_MAX;dp=b;isa=0;}else{*dp=DBL_MAX;dp=NULL;}}}}#defineN100intmain(intargc,char*argv[]){doublea[N],b[N];intc,d,e,pos;FILE*fp=fopen("data.txt","r");if(fp==NULL)return-1;read_data(fp,a,b,&c,&d,&e);printf("a:");pos=0;while(a[pos]<DBL_MAX&&pos<N)printf("%f",a[pos++]);printf("b:");pos=0;while(b[pos]<DBL_MAX)printf("%f",b[pos++]);printf("c=%d,d=%d,e=%d",c,d,e);return0;}


赞 (0)