c读取文件全部内容(C读取文件内容)

本文目录
- C读取文件内容
- C语言用fscanf()函数如何读取文件全部内容
- C语言中读取txt文件内容
- C语言如何读取TXT全部字符
- (c语言)从一个文本文件中读出所有的数据,然后要输出其中的一个数据,要怎么做呢(最好有程序说明)
- C语言如何读取txt文本里面的内容
C读取文件内容
程序你看看吧,有问题再问,满意请采纳:
无论你的文件里面有多少行数据,程序都会自动识别文件结尾
#include《stdio.h》
int main()
{
int a1; // 存的第一列数据,根据文件行数修改
int a2; // 存的第二列数据,根据文件行数修改
int a3; // 存的第三列数据,根据文件行数修改
FILE*fp;
int k=0;
int i;
if((fp=fopen("test.in","r"))==NULL) // 打开数据文件
{
printf("cant find the file!");
return -1;
}
while(!feof(fp))
{
fscanf(fp,"%d %d %d",&a1);
k++;
}
printf("读取文件后,数组内的结果显示:\n");
for(i=0;i《k;i++)
printf("%d %d %d\n",a1);
fclose(fp);
return 0;
}
C语言用fscanf()函数如何读取文件全部内容
void read_txt(const char* Input, const char* Output){
FILE *fin = fopen(Input, "rb");//以二进制读入
FILE *fout = fopen(Output, "w");
unsigned char ch1,ch3;
while(fscanf(fin, "%c%c", &ch1,ch3) != EOF){//直到文件结束
fprintf(fout, "%d%d", ch1,ch3);//以10进制输出
}
}
int main(){
read_txt("D:/IN.txt","D:/OUT.txt");//txt文件目录
return 0;
}
注:判断文件结束处的语句:fscanf(fin, "%c%c", &ch1,ch3)。其中两个%c之间不能加空格,否则读到的二进制文件会不完整,比源文件少好多个字节
C语言中读取txt文件内容
楼主朋友,你的程序中的问题出在分配空间不足上。比如当你想让a指向某段内存时,用的是
a=(char *)malloc(sizeof(char)); 而到了后面你是要在这段内存中存入一个字符串的,所以就发生了越界。下面是我根据你的代码片段写的一个测试程序,经过修改应该没问题了。
#include 《stdio.h》
#include 《stdlib.h》
int main (void)
{
char path="12345.txt";
FILE *create;
if((create=fopen(path,"r"))!=NULL)
{
int j; char **a;
a=(char **)malloc(100*sizeof(char*)); //此处如果只申请一个char *大小的空间时,
//你以后的a往哪里放?此处的100是假设你的文件中有100行信息。如果超过100还得多分配
for (j=0;;j++)
{
a=(char *)malloc(10000*sizeof(char)); //此处只申请一个字符的空间,后面读取
//长度为10000的字符串就没地方存放了
fgets(a,10000,create);
printf("%s",a); //测试读取是否成功,将文件中信息显示到屏幕上
if(feof(create)!=0)
{
for(;j=0;j--)
free(a);
break;
}
}
free(a);
}
else
printf("Fail to open the file.\n");
fclose(create);
printf("\n");
}
C语言如何读取TXT全部字符
你可以使用输入输出重定向来将TXT文本中的字符内容导入程序中,或者使用标准C库函数:fopen()和fgetc();
先使用fopen()函数打开TXT文本文件,然后使用fgetc读取文本文件中的字符。读取全部文本中全部字符可以使用一个while循环加判断是否读取到文件结尾来实现:
char ch;
while((ch= fgetc(fp)) != EOF)
这样当读取到文件结尾时,while循环就会终止。
(c语言)从一个文本文件中读出所有的数据,然后要输出其中的一个数据,要怎么做呢(最好有程序说明)
#include《stdio.h》
void main()
{
char xm;
float sx,wl,wy,c,dy;
FILE *fp=fopen("data.txt","r"); /* 打开数据文件 */
fscanf(fp,"%s %f %f %f %f %f",xm,&sx,&wl,&wy,&c,&dy); /* 读取文件第一行 */
while(!feof(fp)) /* 文件没有结束 */
{
printf("%s\t%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\n",xm,sx,wl,wy,c,dy);
fscanf(fp,"%s %f %f %f %f %f",xm,&sx,&wl,&wy,&c,&dy); /* 读取文件下一行 */
}
fclose(fp); /* 关闭文件 */
}
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\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;
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 = (char)ch;
ch = fgetc( stream );
}
/* Add null to end string */
buffer = ’\0’;
printf( "%s\n", buffer );
fclose( stream );
}

更多文章:
全球新冠肺炎疫情背景下航运发展(盐田港复苏日志:半年历劫从“低谷”到“爆仓” 疫情之后巨轮如何越洋航行)
2026年9月7日 17:10
matlab求解带字母参数方程组(我想matlab求一个关于x,y的方程组 ab c d f e h m n 都是参数)
2026年9月7日 16:30
oracle中的循环语句(下面哪个不是oracle程序设计中的循环语句 a for)
2026年9月7日 15:30
电脑里2个系统怎么删除一个(电脑开机显示有两个系统,如何删除一个)
2026年9月7日 12:20
scrollthrough意思(“scroll”是什么意思)
2026年9月7日 08:00
怎么激活keygen(注册机如何激活cad2008一个简单激活cad2008的方法)
2026年9月7日 06:30





