使用c中的文件
Working with files in c
如何从字符串中删除每个不是字母的字符从字符串CCD_ 1,我需要一个输出afhbi
。请注意,考虑到我不想要逗号和其他类似的符号。到目前为止,这是我的代码,它不起作用,有什么提示吗?
while((fgets(str,30,fpointer))!=NULL)
{ //i get a string
for(i=0;i<strlen(str);i++)//going thru the string
if(isalpha(str[i])){strcat(Need,str[i]);}
//if the char is alpha put it in a new string called Need
}
您不希望使用strcat
向数组添加字符。这是为了将一个字符串附加到另一个字符串上。只需在数组中插入字符。
int j = 0; // Index of the new string
for(i = 0; i < strlen(str); i++) { //going thru the string
if(isalpha(str[i])) {
Need[j++] = str[i];
}
}
Need[j] = 0; // Make sure you terminate the new string
您也可以使用memmove来执行类似的操作。首先在Need中复制字符串;
Need = strdup(str);
p = Need;
q = str;
while (*q) {
if (!isalpha(*q)) {
len = strlen(p);
memmove(p, p + 1, len); // this will move the NULL terminator too
} else {
p++;
}
q++;
}
现在,Need已经清理掉了所有丑陋的非角色!
相关文章:
- 通过多个头文件使用常量变量
- 生成文件不对文件使用隐式规则
- 两个文件使用彼此的功能-如何解决
- 生成一个生成文件,该生成文件使用Automake在一个步骤中编译和链接所有源文件
- 对同一文件使用 .read 的次数是否有限制?
- 是否有正确的方法对生成文件中的对象文件使用模板命令?(C++)
- 如何在C++中为整个文本文件使用字符串流while循环逻辑
- 如何对继承的文件使用前向声明
- 获取对源文件中特定函数的所有调用并生成其他文件(使用 C、C++预处理器或脚本)
- 从文件(使用strtok()和strcpy())解开数据时进行分割故障
- 在 c++ 中对两个文件使用标头
- 当删除部分依赖列表时,GNU make 对每个对象文件使用相同的源文件
- 如何在文本文件中仅读取一行,该文本文件使用QT具有特定单词
- 我应该只对原始 cpp 文件使用 add_executable() 还是通过 add_library() 创建一个库
- 在 c++ 和 c# 中,对文件容器中包含的文件使用文件路径
- CMD窗口停止工作,文本文件使用fstream读取错误
- 如何在 C++_ 中通过不同目录中的文件使用该变量
- JPEG编码位图(BMP)图像从文件使用libjpeg / C++
- 创建 时出错.SO文件使用Cygwin
- 文件I/O使用C++同时打开两个文件