C 比原稿多的字符
我试图用空格代替所有的非alfabetic字符,但是这会增加很多字符,有人知道如何解决这个问题吗 投入:(查尔斯·狄更斯的古腾堡计划《双城记》电子书) 这本电子书是供你使用的 输出:查尔斯·狄更斯的古腾堡计划电子书《双城记》这本电子书供使用C 比原稿多的字符,c,C,我试图用空格代替所有的非alfabetic字符,但是这会增加很多字符,有人知道如何解决这个问题吗 投入:(查尔斯·狄更斯的古腾堡计划《双城记》电子书) 这本电子书是供你使用的 输出:查尔斯·狄更斯的古腾堡计划电子书《双城记》这本电子书供使用 int main(int argc, const char * argv[]) { FILE * fp; FILE * newFile; fp = fopen("/Users/renatorollino/Desktop/labb.txt&qu
int main(int argc, const char * argv[]) {
FILE * fp;
FILE * newFile;
fp = fopen("/Users/renatorollino/Desktop/labb.txt", "r");
newFile = fopen("/Users/renatorollino/Desktop/newlabb.txt", "w");
if(fp==NULL){
printf("File could not open");
}
else{
while (1) {
char c = fgetc(fp);
if(isalpha(c)){
if (feof(fp)) break;
fputc(c, newFile);
}else{
if (feof(fp)) break;
c = ' ';
fputc(c, newFile);
}
}fclose(fp);
}
printf("done");
return 0;
}这样做不是更好吗:
(!isalpha(c))
{
c = ' ';
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, const char* argv[]) {
FILE* fp;
FILE* newFile;
fp = fopen("labb.txt", "r");
if (fp == NULL) {
printf("File could not open");
exit(1);
}
newFile = fopen("newlabb.txt", "w");
if (newFile == NULL) {
printf("File could not open written");
exit(1);
}
while (1) {
char c = fgetc(fp);
if (feof(fp))
break; // end of file => exit the loop
if (!isalpha(c)) // is it somethign else that A-Z or a-z?
{
c = ' '; // replace with space
}
fputc(c, newFile); // write character
}
fclose(fp); // close all files
fclose(newFile);
printf("done");
return 0;
}
我不太清楚你想达到什么目的 如果要将所有非字母替换为空格,则可能需要以下内容:
(!isalpha(c))
{
c = ' ';
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, const char* argv[]) {
FILE* fp;
FILE* newFile;
fp = fopen("labb.txt", "r");
if (fp == NULL) {
printf("File could not open");
exit(1);
}
newFile = fopen("newlabb.txt", "w");
if (newFile == NULL) {
printf("File could not open written");
exit(1);
}
while (1) {
char c = fgetc(fp);
if (feof(fp))
break; // end of file => exit the loop
if (!isalpha(c)) // is it somethign else that A-Z or a-z?
{
c = ' '; // replace with space
}
fputc(c, newFile); // write character
}
fclose(fp); // close all files
fclose(newFile);
printf("done");
return 0;
}
#包括
#包括
#包括
int main(int argc,const char*argv[]{
文件*fp;
文件*newFile;
fp=fopen(“labb.txt”,“r”);
如果(fp==NULL){
printf(“文件无法打开”);
出口(1);
}
newFile=fopen(“newlabb.txt”,“w”);
if(newFile==NULL){
printf(“文件无法打开写入”);
出口(1);
}
而(1){
char c=fgetc(fp);
if(feof(fp))
break;//文件结尾=>退出循环
如果(!isalpha(c))//是A-Z还是A-Z?
{
c='';//替换为空格
}
fputc(c,newFile);//写入字符
}
fclose(fp);//关闭所有文件
fclose(新文件);
printf(“完成”);
返回0;
}
- 代码要简单得多
- 对
功能进行错误检查fopen
- 添加了所有必需的
#包括
- 可能源文件不是ASCII码
while (1) {
int c = fgetc(fp); // changed type to int
if (c == EOF) break; // removed feof()
if ((c < 0) || (c > 127)) continue; // ignore non-ASCII data
if (!isalpha(c)) c = ' ';
fputc(c, newFile);
}
while(1){
int c=fgetc(fp);//将类型更改为int
if(c==EOF)break;//删除了feof()
如果((c<0)|(c>127))继续;//忽略非ASCII数据
如果(!isalpha(c))c='';
fputc(c,新文件);
}
注意:使用宽字符函数可能更好。您能澄清一下确切的输入、预期结果和实际结果是什么吗?它没有改变Gilad的任何内容