怎么查看程序里面的文本字符串
2个回答
展开全部
楼主你看看吧,不过最好你还是去看书,文件操作不难的。
这是从控制台写数据到文件中,然后把数据从文件中读出来同时记录文件中有多个字符,多少个单词,多少个句子。
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *out,*in;
char ch;
int cnt = 0,chnum = 0,wordnum = 0,sennum = 0;
if((out = fopen("G:\\abc0.txt","w")) == NULL)
{//以写方式打开G:\\abc0.txt文件
fprintf(stderr,"Can't not create the file!\n");
exit(0);
}
while((ch = getchar()) != EOF)
fputc(ch,out);//从控制台写数据到G:\\abc0.txt文件中
fclose(out);
if((in = fopen("G:\\abc0.txt","r")) == NULL)
{//以读方式打开G:\\abc0.txt文件
fprintf(stderr,"Can't not open the file!\n");
exit(0);
}
if((out = fopen("G:\\abc1.txt","w")) == NULL)
{//以写方式打开G:\\abc1.txt文件
fprintf(stderr,"Can't not create the file!\n");
exit(0);
}
while((ch = getc(in)) != EOF)
{//从G:\\abc0.txt文件中一个一个字符读入
if(ch == ' ')
{
if(cnt == 0)
{
fputc(ch,out);
++cnt;
}
else ++cnt;
}
else
{
fputc(ch,out);
cnt = 0;
}
}
fclose(in);
fclose(out);
if((in = fopen("G:\\abc1.txt","r")) == NULL)
{
fprintf(stderr,"Can't not open the file!\n");
exit(0);
}
while((ch = fgetc(in)) != EOF)
{
++chnum;
if(ch == ' ') ++wordnum;
else if(ch == '.')
{
++sennum;
++wordnum;
}
}
fclose(in);
printf("字符个数:%d\n单词个数:%d\n句子个数:%d\n",chnum,wordnum,sennum);
return 0;
}
这是从控制台写数据到文件中,然后把数据从文件中读出来同时记录文件中有多个字符,多少个单词,多少个句子。
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *out,*in;
char ch;
int cnt = 0,chnum = 0,wordnum = 0,sennum = 0;
if((out = fopen("G:\\abc0.txt","w")) == NULL)
{//以写方式打开G:\\abc0.txt文件
fprintf(stderr,"Can't not create the file!\n");
exit(0);
}
while((ch = getchar()) != EOF)
fputc(ch,out);//从控制台写数据到G:\\abc0.txt文件中
fclose(out);
if((in = fopen("G:\\abc0.txt","r")) == NULL)
{//以读方式打开G:\\abc0.txt文件
fprintf(stderr,"Can't not open the file!\n");
exit(0);
}
if((out = fopen("G:\\abc1.txt","w")) == NULL)
{//以写方式打开G:\\abc1.txt文件
fprintf(stderr,"Can't not create the file!\n");
exit(0);
}
while((ch = getc(in)) != EOF)
{//从G:\\abc0.txt文件中一个一个字符读入
if(ch == ' ')
{
if(cnt == 0)
{
fputc(ch,out);
++cnt;
}
else ++cnt;
}
else
{
fputc(ch,out);
cnt = 0;
}
}
fclose(in);
fclose(out);
if((in = fopen("G:\\abc1.txt","r")) == NULL)
{
fprintf(stderr,"Can't not open the file!\n");
exit(0);
}
while((ch = fgetc(in)) != EOF)
{
++chnum;
if(ch == ' ') ++wordnum;
else if(ch == '.')
{
++sennum;
++wordnum;
}
}
fclose(in);
printf("字符个数:%d\n单词个数:%d\n句子个数:%d\n",chnum,wordnum,sennum);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询