用fgets( char *string, int n, FILE *stream );Sample:
FGETS.C/* FGETS.C: This program uses fgets to display
* a line from a file on the screen.
*/#include <stdio.h>void main( void )
{
FILE *stream;
char line[MAX_STRING]; if( (stream = fopen( "fgets.c", "r" )) != NULL )
{
if( fgets( line, MAX_STRING, stream ) == NULL)
printf( "fgets error\n" );
else
printf( "%s", line);
fclose( stream );
}
}
输出(得到文件第一行):/* FGETS.C: This program uses fgets to display
FGETS.C/* FGETS.C: This program uses fgets to display
* a line from a file on the screen.
*/#include <stdio.h>void main( void )
{
FILE *stream;
char line[MAX_STRING]; if( (stream = fopen( "fgets.c", "r" )) != NULL )
{
if( fgets( line, MAX_STRING, stream ) == NULL)
printf( "fgets error\n" );
else
printf( "%s", line);
fclose( stream );
}
}
输出(得到文件第一行):/* FGETS.C: This program uses fgets to display
解决方案 »
- 遍历网站网页文本可以用到多少种技术呢?
- 求救!!!一个困扰很久的问题.
- 问一个很笨的问题:installshield有控制台的操作命令吗?
- 请问如何获得指定进程(句柄或者ID)的图标?
- 如何减少CPU的占用率
- vc调用excel多次读写数据后关闭(或注销)出错的问题
- 写成什么程序(库)才能在win2k和UNINX下都能调用?谢谢
- 一个菜鸟问题,哪位大侠能回答?
- H323的VC有什么调试程序的好方法啊?
- 怎样去掉CFrameWnd的缺省WS_EX_CLIENTEDGE属性
- 那位大哥有最新的winuser.h和user32.lib? 帮我着必给分,这是我能拿得最多的分了
- 如何读取文本中特定的字符(数字)并转换为数字????
FILE *stream;
if(getFileName(stream)!=1)
{
........
}
// 读取文件数据
char strData[255];
for(; !feof(stream) ; )
{
if(fscanf( stream, "%s", &strData)!=0)
// 读入一行 注意 strData 的长度
{
// do my operation
}
}
fclose( stream );
CStdioFile pFile;
CString str;
pFile.Open("文件名","CFile::modeRead");
……
pFile.ReadString(str);str就是读取的一行字符,如果想一行一行的读取直到文件尾,可以判断ReadString()的返回值(返回值为布尔值):
while(pFile.ReadString(str))
{
……
}