当前位置:首页 » 编程语言 » c语言循环读取txt文件数据
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言循环读取txt文件数据

发布时间: 2022-10-29 05:59:47

A. c语言fgets如何循环读取整个文本文件的内容

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//函数返回fname指定文件的全部内容,如果打不开文件,则返回NULL,并显示打开文件错误
char *getfileall(char *fname){
FILE *fp;
char *str;
char txt[1000];
int filesize;
if ((fp=fopen(fname,"r"))==NULL){
printf("打开文件%s错误\n",fname);
return NULL;
}

fseek(fp,0,SEEK_END);

filesize = ftell(fp);
str=(char *)malloc(filesize);
str[0]=0;

rewind(fp);
while((fgets(txt,1000,fp))!=NULL){
strcat(str,txt);
}
fclose(fp);
return str;
}
int main(){
char *p;
char *fname="D:\\temp.txt";
p=getfileall(fname);
if (p!=NULL) puts(p);
}

B. 用c语言读取一个txt文件

如果预知前面的是英文后面的是中文,即可分开:

#include<stdio.h>

#define N 100

void main() { FILE *fp; char s[256],y[N][20],h[N][20]; int i,n;

if ( fp=fopen("c:\data\text.txt","r") ) {

n=0;

while ( !feof(fp) ) {

fgets(s,256,fp); sscanf("%s%s",y[n],h[n]); n++; if ( n>=N ) break;

}

fclose(fp);

printf("英文: "); for ( i=0;i<n;i++ ) printf("%s ",y[i]); printf(" ");

printf("中文: "); for ( i=0;i<n;i++ ) printf("%s ",h[i]); printf(" ");

} else printf("无法打开文件读取。 ");

}

如果中英文顺序不一定,且不会有中英文混合单词:

#include<stdio.h>

#include<string.h>

#define N 100

void main() { FILE *fp; char s[256],y[N][20],h[N][20]; int i,n;

if ( fp=fopen("c:\data\text.txt","r") ) {

n=0;

while ( !feof(fp) ) {

fgets(s,256,fp); sscanf("%s%s",y[n],h[n]);

if ( y[n][0]<0 ) { strcpy(s,y[n]);strcpy(y[n],h[n]);strcpy(h[n],s); } //汉字字符ASCII码小于0

n++; if ( n>=N ) break;

}

fclose(fp);

printf("英文: "); for ( i=0;i<n;i++ ) printf("%s ",y[i]); printf(" ");

printf("中文: "); for ( i=0;i<n;i++ ) printf("%s ",h[i]); printf(" ");

} else printf("无法打开文件读取。 ");

}

C. 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语言循环读取txt文件数据扩展阅读

使用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 );

}

D. c语言如何多次读同一个txt文件

因为你第一次就把文件里指针移至尾部,需要使用fseek函数进行跳转。比如“起始点”参数设置为SEEK_SET
即可将文件指针挪到文件开头位置再次读取,反复这样操作,即可实现重复读取文件。

E. c语言读取txt文件多行数据

#include<stdio.h>
#defineMAX_LINE1024 //每行最大字节数

intmain()
{
intn=0;
FILE*fp;
charstrLine[MAX_LINE]; //读取缓冲区
if((fp=fopen("result.txt","r"))==NULL) //判断文件是否存在及可读
{
printf("OpenFalied!");
return-1;
}
while(!feof(fp)) //循环读取每一行,直到文件尾
{
fgets(strLine,MAX_LINE,fp); //将fp所指向的文件一行内容读到strLine缓冲区
printf("%s",strLine); //输出所读到的内容
//DOSOMETHINGELSE
}
fclose(fp); //关闭文件
printf(" ");
return0;
}

用一维数组存的,如果需要用二维数组则用制表符‘ ’拆分就好了。

F. c语言读取txt文件内容

用C语言从txt文件中读取数据,可以使用C标准库文件自带的文件接口函数进行操作。
一、打开文件:
FILE *fopen(const char *filename, const char *mode);
因为txt文件为文本文件, 所以打开时选择的mode应为"r"或者"rt"。
二、读取文件:
读取文件应根据文件内容的格式,以及程序要求,选择读取文件的函数。可以使用一种,也可以几种混用。 常用的文件读取函数如下:
1、fgetc, 从文件中读取一个字节并返回。 适用于逐个字节读取。
2、 fgets, 从文件中读取一行。适用于整行读取。
3、fscanf, 格式化读取文件, 在已经清楚文件存储格式下,可以直接用fscanf把文件数据读取到对应类型的变量中。
4、fread, 整块读取文件, 对于txt文件比较少用。
三、关闭文件:
读取结束后,应调用fclose函数关闭文件。

G. VC++中如何循环读取TXT文件的每一行内容

C++用fstream中的getline()函数读取一行文件内容

C语言可用fgets()函数读取一行文件内容

两者有一些区别:

1、fgest()读到回车结束,回车符也会写到接收buf中

2、getline()可以设定读到哪个字符结束,默认是回车符,但指定的这个字符不会写到接收buf中。

3、fgets()读取数据,如果在读到回车符之前,达到了最大可读个数,则也会返回已读到的buf数据

4、getline()在读数据时,遇到指定字符之前,达到了最大可读个数,则会返回读错误

参考代码如下:

C++版本

#include<iostream>
#include<fstream>
usingnamespacestd;
intmain()
{
ifstreamin("test.txt");
charstr[1024]={0};

if(in.fail())
{
cout<<"openfileerror"<<endl;
return-1;
}
while(in.getline(str,sizeof(str),' '))
{
cout<<str<<endl;
}
in.close();
return0;
}

C语言版本

#include<stdio.h>
intmain()
{
FILE*fp;
fp=fopen("test.txt","r");
charstr[1024];
if(fp==NULL)
{
printf("openfileerror ");
return-1;
}
while(fgets(str,sizeof(str),fp))
{
printf("%s",str);
}
fclose(fp);
return0;
}

H. 怎样用C语言写入\读取一个TXT文件

如果预知前面的是英文后面的是中文,即可分开:

#include<stdio.h>

#define N 100

void main() { FILE *fp; char s[256],y[N][20],h[N][20]; int i,n;

if ( fp=fopen("c:\data\text.txt","r") ) {

n=0;

while ( !feof(fp) ) {

fgets(s,256,fp); sscanf("%s%s",y[n],h[n]); n++; if ( n>=N ) break;

}

fclose(fp);

printf("英文: "); for ( i=0;i<n;i++ ) printf("%s ",y[i]); printf(" ");

printf("中文: "); for ( i=0;i<n;i++ ) printf("%s ",h[i]); printf(" ");

} else printf("无法打开文件读取。 ");

}

如果中英文顺序不一定,且不会有中英文混合单词:

#include<stdio.h>

#include<string.h>

#define N 100

void main() { FILE *fp; char s[256],y[N][20],h[N][20]; int i,n;

if ( fp=fopen("c:\data\text.txt","r") ) {

n=0;

while ( !feof(fp) ) {

fgets(s,256,fp); sscanf("%s%s",y[n],h[n]);

if ( y[n][0]<0 ) { strcpy(s,y[n]);strcpy(y[n],h[n]);strcpy(h[n],s); } //汉字字符ASCII码小于0

n++; if ( n>=N ) break;

}

fclose(fp);

printf("英文: "); for ( i=0;i<n;i++ ) printf("%s ",y[i]); printf(" ");

printf("中文: "); for ( i=0;i<n;i++ ) printf("%s ",h[i]); printf(" ");

} else printf("无法打开文件读取。 ");

}

I. C语言读取txt文件中的数据

//其中的in.txt就是你要读取数据的文件,当然把它和程序放在同一目录
-------------------------------------

#include <stdio.h>
int main()
{
int data;
FILE *fp=fopen("in.txt","r");
if(!fp)
{
printf("can't open file\n");
return -1;
}
while(!feof(fp))
{
fscanf(fp,"%d",&data);
printf("%4d",data);
}
printf("\n");
fclose(fp);
return 0;
}