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

c语言中txt的读写

发布时间: 2022-12-25 21:41:22

c语言 txt文件的读写方法

例子一个,比较简单:

#include<stdio.h>
#include<string.h>

void main()
{
char line[50];
char name[20], ps[8];

FILE *fp = fopen("User.txt","r");
if(!fp)
{
return;
}

while(!feof(fp))
{
memset(line,0,50);
memset(name,0,20);
memset(ps,0,8);
fgets(line,50,fp);

sscanf(line, "%s%s", name, ps);
printf("Name:[%s], Pass:[%s]\n",name,ps);
}
}

❷ 怎样用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<string.h>

#defineMAXSIZE4000000

structpassword{
charpsw[12];//密码名称
intcounter;//出现次数计数器
};

intAppend(structpassworda[],int*n,charpsw[]){
inti;
for(i=0;i<*n;++i){
if(strcmp(a[i].psw,psw)==0){
++a[i].counter;
return2;
}
}
if(*n<MAXSIZE){
strcpy(a[*n].psw,psw);
a[*n].counter=1;
++(*n);
return1;
}
return0;
}

intmain(){
structpassworda[MAXSIZE];
charpsw[12];
inti,n=0,id;
charinfilename[]="indata.txt";
charoutfilename[]="outdata.txt";
FILE*inf,*outf;
if((inf=fopen(infilename,"rt"))==NULL){
printf("不能打开数据文件:%s。 ",infilename);
return1;
}
while(fscanf(inf,"%d%11s",&id,psw)==2){
if(Append(a,&n,psw)==0)break;
}
fclose(inf);
if((outf=fopen(outfilename,"wt"))==NULL){
printf("不能打开数据文件:%s。 ",outfilename);
return2;
}
for(i=0;i<n;++i)
fprintf(outf,"%s%d ",a[i].psw,a[i].counter);
fclose(outf);
return0;
}

❹ 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的r方式可以实现读取,用w+方式可以实现写入。
1.fopen的函数原型:FILE
*
fopen(const
char
*
path,const
char
*
mode);

fopen函数的第一个参数是文件路径,第二个参数是打开方式,有以下几种方式:
r
以只读方式打开文件,该文件必须存在。
r+
以可读写方式打开文件,该文件必须存在。
rb+
读写打开一个二进制文件,允许读数据。
rw+
读写打开一个文本文件,允许读和写。
w
打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。
w+
打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。
a
以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。(EOF符保留)
a+
以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。
(原来的EOF符不保留)
wb
只写打开或新建一个二进制文件;只允许写数据。
wb+
读写打开或建立一个二进制文件,允许读和写。
wt+
读写打开或着建立一个文本文件;允许读写。
at+
读写打开一个文本文件,允许读或在文本末追加数据。
ab+
读写打开一个二进制文件,允许读或在文件末追加数据。
上述的形态字符串都可以再加一个b字符,如rb、w+b或ab+等组合,加入b
字符用来告诉函数库打开的文件为二进制文件,而非纯文字文件。

返回值:文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回NULL,并把错误代码存在errno中。
2.例程:
#include<stdio.h>
#define F_PATH "d:\\myfile\\file.dat"
char c;
int main(){
FILE*fp=NULL;//需要注意
fp=fopen(F_PATH,"w"); //创建文件
if(NULL==fp) return -1;//要返回错误代码
while(scanf("%c",&c)!=EOF) fprintf(fp,"%c",c); //从控制台中读入并在文本输出
fclose(fp);
fp=NULL;//需要指向空,否则会指向原打开文件地址
return 0;
}

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

}

(6)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文件里面的数据

自己对比下吧...............

#include<stdio.h>
void main()
{
FILE *fp = NULL;
int number[4] = {};

if (fp = fopen("e:\\1.txt", "r"))
{
for (int i = 0; i < 4; i++)
fscanf(fp, "%*[^=]=%d", number+i);

fclose(fp);
printf("%d, %d, %d, %d\n", number[0], number[1], number[2], number[3]);
}
return;
}

..................................................................

最主要的原因,你那个for里面虽然有用 i 来控制次数,可fgets和sscanf里面根本就没用 i ,一直在往同一地址写数据,其他细节我就不多说了.....

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

1通过fopen函数打开文本,例如FILE *fp=fopen("in.txt","r");//返回一个FILE类型的句柄

2然后就可以通过fcanf()函数对txt文本进行读取

3操作完文本之后用fclose()函数 关闭已经打开的文件。

#include<stdio.h>
intmain()
{
intdata;
FILE*fp=fopen("in.txt","r");
if(!fp)
{
printf("can'topenfile ");
return-1;
}
while(!feof(fp))
{
fscanf(fp,"%d",&data);
printf("%4d",data);
}
printf(" ");
fclose(fp);
return0;
}