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

c语言如何读取txt文件|请问如何用c语言从txt文件中读取数据的第1张示图

⑴ 如何用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语言读取文本文件

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文件中读入数据

1 以fopen打开文件,使用"r"方式。

2 通过fscanf,按照文件中的数据格式,读入数据。

3 关闭文件并使用数据。

如文件in.txt中存在三个以空格分隔的数据,依次为整型,字符串,以及浮点型,则读取数据的代码可以写作:

intmain(){FILE*fp;inta;chars[100];floatf;fp=fopen("in.txt","r");if(fp==NULL)return-1;//打开文件失败,结束程序。fscanf(fp,"%d%s%f",&a,s,&f);fclose(fp);printf("readvalue:%d,%s,%f",a,s,f);}

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

}

(4)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语言读取txt文件

以下程序实现输来入文件名源, 按行读文件, 并输出.

intmain(){FILE*fp;charname[100];charbuf[1024];scanf("%s",name);fp=fopen(name,"r");if(fp==NULL)printf("openfilefailed");else{while(fgets(buf,fp)!=NULL)printf("%s",buf);}fclose(fp);return0;}

除了按行读取外, 还可以单个字符读取 fgetc, 格式化读取fscanf.

用法类似于getchar和scanf

⑹ C语言中读取txt文件内容

楼主朋友,你的程序中的问题出在分配空间不足上。比如当你想让a[j]指向某段内存时,用的是 a[j]=(char *)malloc(sizeof(char)); 而到了后面你是要在这段内存中存入一个字符串的,所以就发生了越界。下面是我根据你的代码片段写的一个测试程序,经过修改应该没问题了。#include <stdio.h>#include <stdlib.h>int main (void){ char path[]="12345.txt"; FILE *create; if((create=fopen(path,"r"))!=NULL) { int j; char **a; a=(char **)malloc(100*sizeof(char*)); //此处如果只申请一个char *大小的空间时, //你以后的a[j]往哪里放?此处的100是假设你的文件中有100行信息。如果超过100还得多分配 for (j=0;;j++) { a[j]=(char *)malloc(10000*sizeof(char)); //此处只申请一个字符的空间,后面读取 //长度为10000的字符串就没地方存放了 fgets(a[j],10000,create); printf("%s",a[j]); //测试读取是否成功,将文件中信息显示到屏幕上 if(feof(create)!=0) { for(;j=0;j–) free(a[j]); break; } } free(a); } else printf("Fail to open the file.\n"); fclose(create); printf("\n");}

⑺ 请问如何用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;}

⑻ c语言 如何读取本地文本文件

这个就太简单拉!!如下:回#include <stdio.h>#include <stdlib.h>int main(void){ int ch = 0; FILE *fp = NULL; fp = fopen("d:\\a.txt", "r"); if (fp == NULL) { printf("\nCann't open the file!答"); exit(1); } else { while ( ( ch = fgetc(fp) ) != EOF) { putchar(ch);//或者printf("%c", ch); } fclose(fp); } return 0;}

⑼ 怎么用C语言读取 TXT文件中的字符串

可以使用输入输出重定向来将TXT文本中的字符内容导入程序中,或者使用标准C库函数:fopen()和fgetc();先使用fopen()函数打开TXT文本文件,然后使用fgetc读取文本文件中的字符。读取全部文本中全部字符可以使用一个while循环加判断是否读取到文件结尾来实现:char ch;while((ch= fgetc(fp)) != EOF)这样当读取到文件结尾时,while循环就会终止。

C语文编写编译如下:

#include<stdio.h>#include<stdlib.h>#include<string.h>#defineMAXLINE100000#defineBUFLEN1024intmain(){FILE*file;charbuf[BUFLEN];intlen=0,i=0;char*array[MAXLINE];file=fopen("test.txt","r");//打开TXST.TxT文件if(!file)return-1;while(fgets(buf,BUFLEN,file))//读取TXT中字符{len=strlen(buf);array[i]=(char*)malloc(len+1);if(!array[i])break;strcpy(array[i++],buf);}fclose(file);i–;while(i>=0&&array[i]){printf("%s",array[i]);//打印test文档的字符free(array[i–]);}}

⑽ C语言中如何调用文本文件

1、首先使用VS新建空工程,直接点击确定。

未经允许不得转载:山九号 » c语言如何读取txt文件|请问如何用c语言从txt文件中读取数据

赞 (0)