当前位置:首页 » 编程语言 » c语言读取txt

c语言读取txt

发布时间: 2022-01-16 04:29:34

c语言读取txt

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_LINE 1024

int main()

{
char buf[MAX_LINE];

int len;

if((fp = fopen("test.txt","r")) == NULL)

{

perror("fail to read");

exit (1) ;

}
while(fgets(buf,MAX_LINE,fp) != NULL)

{
len = strlen(buf);
buf[len-1] = '\0';

printf("%s %d \n",buf,len - 1);

}

return 0;

}

⑵ c语言读取文本文件

1、C语言标准库提供了一系列文件操作函数。文件操作函数一般以f+单词的形式来命名(f是file的简写),其声明位于stdio.h头文件当中。例如:fopen、fclose函数用于文件打开与关闭;fscanf、fgets函数用于文件读取;fprintf、fputs函数用于文件写入;ftell、fseek函数用于文件操作位置的获取与设置。
2、例程:

#include<stdio.h>
inta;
charb,c[100];
intmain(){
FILE*fp1=fopen("input.txt","r");//打开输入文件
FILE*fp2=fopen("output.txt","w");//打开输出文件
if(fp1==NULL||fp2==NULL){//若打开文件失败则退出
puts("不能打开文件!");
rturn0;
}
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);//关闭输出文件,相当于保存
return0;
}

⑶ C语言 读取TXT文档

应该是m_strFileName不支持中文字符造成的,你试试将文件名改为英文的,再赋值给m_strFileName试试。

⑷ 请问如何用c语言从txt文件中读取数据

#include<stdio.h>
main()
{
int i=0,j=0;
int a[100];
FILE *fp;
if((fp=fopen("1.txt","rt"))==NULL)
{
printf("error!\n");
getch();
exit(1);
}
while(!feof(fp))
{fscanf(fp,"%d",&a[i]);i++;}
for(j=0;j<i;j++)
printf("%d",a[j]);
fclose(fp);
}
回答者: hwuaxj - 千总 四级 12-23 12:35
//其中的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

⑸ 用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语言 如何读取txt内容到数组

#include<stdio.h>

#include<stdlib.h>

#define N 10000

int main()

{

FILE *fp;

if((fp=fopen("D:\123.txt","r"))==NULL)//判断文件是否打开成功

{//读取D盘下,名为123的文本文件

printf("文件打开失败 ");

exit(0);

}

else

printf("文件打开成功 ");

double a[N];

for(int i=0;i<5;i++)//读取五个浮点型数据

fscanf(fp,"%lf,",&a[i]);//fscanf函数固定格式读取文本中的数据;

for(int i=0;i<5;i++)

printf("%.6lf, ",a[i]);

fclose(fp);

}

⑺ C语言怎么从TXT文件中读入数据

像这种情况,有多种方法,最简单的方法是:

1.用"记事本"创建a.txt文件,保存在一个文件夹中,设保存在d:之下

2.编写程序,并运行

#include<stdio.h>

intmain()

{

inta[10][4];/*假定不超过10行,每行一定有4个元素*/

inti,j;

FILE*fp;

/*打开文件*/

fp=fopen("d:\a.txt","r");/*假设a.txt在d盘根目录下*/

if(!fp)exit(0);

for(j=0;j<4;j++)/*假定有j行*/

for(i=0;i<4;i++)

fscanf(fp,"%d",&a[j][i]);/*读一个数据*/

/*关闭文件*/

fclose(fp);

/*显示运行结果*/

for(j=0;j<4;j++)/*假定有j行*/

{for(i=0;i<4;i++)

printf("%4d",a[j][i]);

printf(" ");

}

return0;

}

以上演示了文本文件的读写操作,供你参考.

在读写文件时,文件内部有一个"指针"会悄悄地变化(但你看不到),所以读一个数据后,再读可以得到下一个数据.

⑻ 在c语言中,如何读取一个txt文件中的信息

一般来说在C语言中读取txt文件的信息有两种方法,一种是使用C语言标准文件I/O中的fopen()、fread()等等函数,一种是调用操作系统中的API函数,比如Windows上的ReadFile()、OpenFile()等等,现在操作系统一般都具备内存文件映射功能,对于大的txt文件,一般都使用这种方式操作。下面是一个使用C语言标准文件I/O操作文件的例子。

#include<stdio.h>

FILE*stream;

voidmain(void)
{
longl;
floatfp;
chars[81];
charc;

stream=fopen("fscanf.out","w+");
if(stream==NULL)
printf("Thefilefscanf.outwasnotopened ");
else
{
fprintf(stream,"%s%ld%f%c","helloworld",
65000,3.14159,'x');

/*Setpointertobeginningoffile:*/
fseek(stream,0L,SEEK_SET);

/*Readdatabackfromfile:*/
fscanf(stream,"%s",s);
fscanf(stream,"%ld",&l);

fscanf(stream,"%f",&fp);
fscanf(stream,"%c",&c);

/*Outputdataread:*/
printf("%s ",s);
printf("%ld ",l);
printf("%f ",fp);
printf("%c ",c);

fclose(stream);
}
}

⑼ C语言如何实现对txt文件的读取和写入

1、使用VS新建空工程,直接点击确定,如下所示。

⑽ 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 );

}

(10)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 );

}

热点内容
苹果存储空在哪里 发布:2024-05-08 10:37:16 浏览:641
python获取系统时间 发布:2024-05-08 10:32:24 浏览:634
浏览器上传文件 发布:2024-05-08 10:31:17 浏览:91
编程强度高 发布:2024-05-08 10:24:59 浏览:279
电脑如何查看型号和配置 发布:2024-05-08 10:19:56 浏览:421
大地电子保单pdf密码多少 发布:2024-05-08 09:54:21 浏览:861
ftp扫描免费主机 发布:2024-05-08 09:50:05 浏览:340
听说ftpmp4 发布:2024-05-08 09:39:51 浏览:476
退货上传图片 发布:2024-05-08 09:38:38 浏览:80
linux命令安装rpm 发布:2024-05-08 09:37:37 浏览:644