当前位置:首页 » 编程语言 » c语言如何打开一段txt
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言如何打开一段txt

发布时间: 2023-03-08 08:31:26

c语言怎么读取一个txt文本文档

用fopen()可以打开这个文件;
fread();可以去读取这个文件的内容;
fclose()去关掉这个文件;

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

}

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

}

⑶ 在C语言中,举例说明如何打开一个文本文件,并读入文件中的数据

1.首先,使用VS构建一个新的空项目,然后直接单击ok。

⑷ 用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文件内容

用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函数关闭文件。

⑹ 怎么用C语言编一个打开txt文件的程序

你可以使用fopen函数,例子如下:
FILE
*fp;/*定义文件类型的指针,它讲指向你所要打开的文件,以后向文件写入数据或者是从文件中读取数据都需要用到他*/
fp=fopen("文件名以及其路径","打开方式");
建议以参考以下几个c函数,你就能够很随意的完成对文件的处理了:
fopen()
字符读写函数:fgetc()和fputc()
字符串读写函数:fgets()和fputs()
格式化读写函数:fcanf()和fprintf()
数据块读写函数:fread()和fwrite()
这些都是对文件操作的基本函数,其中你最好研究一下fopen()函数,那个相对其他的函数要记忆的东西比较多~~