用C语言从一个文本文档(假设文本文当路径和文件名都为:input.txt)中读取一串数字到一维数组中

如题所述

第1个回答  2012-11-26
#include <stdio.h>
/*
*先向文本文档中写入一行数字:1234567890
*再读出来
*/
void main( void )
{
FILE *stream;
char list[30];
unsigned long text= 1234567890;
int numread,numwritten;
/* 以文本文档模式打开文件 */
if( (stream = fopen( "input.txt", "w+t" )) != NULL )
{
/*转换为字符串数组*/
_itoa(text,list,10);
/* 写入10个字符*/
numwritten = fwrite( list, sizeof( char ), 10, stream );
printf( "Wrote %d items\n", numwritten );
fclose( stream );
}
else
printf( "Problem opening the file\n" );
if( (stream = fopen( "input.txt", "r+t" )) != NULL )
{
/*读十个字符*/
numread = fread( list, sizeof( char ), 10, stream );
printf( "Number of items read = %d\n", numread );
printf( "Contents of buffer = %s\n", list );
fclose( stream );
}
else
printf( "File could not be opened\n" );
}
这是我写的,你看看,望采纳。
第2个回答  2012-11-26
#include <stdio.h>
int main()
{
char buff[100];
FILE *pf = fopen("input.txt", "r");
fgets(buff, sizeof(buff), pf);

retrun 1;
}本回答被网友采纳
第3个回答  2012-11-26
#include <stdio.h>
int main()
{
char buf[256];
FILE *pf = fopen("input.txt", "r"); // 按行读取 遍历整个文件
while (NULL !== fgets(buff, sizeof(buff), pf))
{
// 这里 文本数据会按行 保存在buf中 可以DEBUG单步跟踪 了解
}

pf->close();

retrun 1;
}来自:求助得到的回答
第3个回答  2012-11-26
FILE *in;
if((in=fopen("input.txt","rb"))==NULL)
{
printf("无法打开输入文件\n");
return 1;
}
//假设a[n]
for(i=0; i<n; i++)
{
a[i]=fgetc(in);
}
fclose(in):

参考资料:http://zhidao.baidu.com/question/98292910.html

相似回答