C语言读取步骤一:首先需要以写入的方式打开一个空白的texr.txt文件。FILE*fpWrite=fopen("text.txt""w")
C语言读取步骤二:打开以后,继续进行循环写入0到9的 *** 作。代码。
C语言读取步骤三:完成文件写入 *** 作后,一定要记得对打开文件进行关闭 *** 作。
C语言读取步骤四:接下来对文件中写入数据的读取。首先需要创建一个用于保存读取数据的数组。
C语言读取步骤五:创建好数组以后,以读取的方式对text.txt文件进行打开 *** 作。
C语言读取步骤六:最后,将读取到的数据循环保存的创建好的数组中,并且将其显示到控制台中。
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\n" )
else
printf( "The file 'data' was opened\n" )
/* Open for write */
if( (stream2 = fopen( "data2", "w+" )) == NULL )
printf( "The file 'data2' was not opened\n" )
else
printf( "The file 'data2' was opened\n" )
/* Close stream */
if(fclose( stream2 ))
printf( "The file 'data2' was not closed\n" )
/* All other files are closed: */
numclosed = _fcloseall( )
printf( "Number of files closed by _fcloseall: %u\n", numclosed )
}
扩展资料
使用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] = '\0'
printf( "%s\n", buffer )
fclose( stream )
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)