A. c语言如何实现对txt文件的读取和写入
1、使用VS新建空工程,直接点击确定,如下所示。
B. c语言 读TXT
void Write()
{
FILE *fp = fopen("data.txt","w");
int a = 25555;
fwrite(&a,sizeof(int),1,fp);
fclose(fp);
}
void Read()
{
FILE *fp = fopen("data.txt","r");
int a;
fread(&a,sizeof(int),1,fp);
fclose(fp);
}
C. 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;
}
以上演示了文本文件的读写操作,供你参考.
在读写文件时,文件内部有一个"指针"会悄悄地变化(但你看不到),所以读一个数据后,再读可以得到下一个数据.
D. 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] = '