C++ 我试图用一些条件清除数据文件中的特殊字符,但这些条件不满足?
这是我的密码 这段代码试图从.txt文件中删除像“,”,{,},(,)这样的特殊字符,并用空格替换它们C++ 我试图用一些条件清除数据文件中的特殊字符,但这些条件不满足?,c++,file-io,C++,File Io,这是我的密码 这段代码试图从.txt文件中删除像“,”,{,},(,)这样的特殊字符,并用空格替换它们 #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <string.h> #include <stdio.h> #include <fcntl.h> #include <iostream> #include <
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <iostream>
#include <time.h>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
int fd;
int i;
int j;
int len;
int count = 0;
int countcoma = 0;
int countquote = 0;
char buf[10];
char spec[] = {',','"',':','{','}','(',')','\''};
fd = open(argv[1],O_RDWR,0777);
while (read(fd,buf,10) != 0) {
len = strlen(buf);
for (i=0;i<len;i++) {
for (j=0;j<8;j++) {
if (buf[i]==spec[j]) {
count =1;
countquote=0;
if (j==1) {
if (countcoma == 0) {
countcoma++;
}
if (countcoma == 1) {
countcoma--;
}
}
if ((j==7) && (countcoma ==1)) {
countquote = 1;
}
break;
}
}
//cout<<countquote;
if ((count != 0) && (countquote == 0)) {
buf[i] = ' ';
}
count = 0;
}
lseek(fd, -sizeof(buf), SEEK_CUR);
write(fd,buf,sizeof(buf));
memset(buf,' ',10);
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
int-fd;
int i;
int j;
内伦;
整数计数=0;
int countcoma=0;
int countquote=0;
char-buf[10];
char spec[]={'、'、''”、':'、'{'、'}'、'('、')'、'\'};
fd=开路(argv[1],O_RDWR,0777);
while(读取(fd,buf,10)!=0){
len=strlen(buf);
对于(i=0;i看一看和其他库。(在UNIX类型man regex
上时)现在您不必再编写此代码了,有无数库可以为您编写此代码。看一看和其他库。(在UNIX类型man regex
上时)现在你不必再编写代码了,有无数的库可以为你编写代码。好的,那么代码的问题是你只做了一件事,然后在下一节中撤消。特别是:
if (countcoma == 0) {
countcoma++;
}
if (countcoma == 1) {
countcoma--;
}
按照逻辑:我们的countcoma
为零。因此,第一个if
为真,并且它会递增。现在它是1
。下一个if表示if(countcoma==1)
所以它现在为真,我们将它递减
我将其替换为countcoma=!countcoma;
,这是一种更简单的方式,可以说“如果它是0,就把它设为1,如果它是1,就把它设为0。你可以在第一个的背面加一个else,如果`来做同样的事情
还有一大堆风格上的东西:例如硬编码常量,写回原始文件(意味着如果有bug,您会丢失原始文件-幸好我没有用示例文件关闭编辑器窗口…),包括头文件中的一半,并计算出它基于索引的spec
字符。好的,因此代码的问题是您正在做一件事,然后在下一节中撤消。特别是:
if (countcoma == 0) {
countcoma++;
}
if (countcoma == 1) {
countcoma--;
}
按照逻辑:我们的countcoma
为零。因此,第一个if
为真,并且它会递增。现在它是1
。下一个if表示if(countcoma==1)
所以它现在为真,我们将它递减
我将其替换为countcoma=!countcoma;
,这是一种更简单的方式,可以说“如果它是0,就把它设为1,如果它是1,就把它设为0。你可以在第一个的背面加一个else,如果`来做同样的事情
还有一大堆风格上的东西:例如硬编码常量,写回原始文件(意味着如果有bug,您会丢失原始文件-幸好我没有用示例文件关闭编辑器窗口…),包括头文件中的一半,并计算出它基于索引的spec
字符。在我看来,您的代码比以前指出的更普遍的缺陷:
char buf[10]; /* Buffer is un-initialized here!! */
while (read(fd,buf,10) != 0) { /* read up to 10 bytes */
len = strlen(buf); /* What happens here if no \0 byte was read? */
...
lseek(fd, -sizeof(buf), SEEK_CUR); /* skip sizeof(buf) = 10 bytes anyway */
write(fd,buf,sizeof(buf)); /* write sizeof(buf) = 10 bytes anyway */
memset(buf,' ',10); /* initialize buf to contain all spaces
but no \0, so strlen will still result in
reading past the array bounds */
在我看来,您的代码比以前指出的更普遍的缺陷:
char buf[10]; /* Buffer is un-initialized here!! */
while (read(fd,buf,10) != 0) { /* read up to 10 bytes */
len = strlen(buf); /* What happens here if no \0 byte was read? */
...
lseek(fd, -sizeof(buf), SEEK_CUR); /* skip sizeof(buf) = 10 bytes anyway */
write(fd,buf,sizeof(buf)); /* write sizeof(buf) = 10 bytes anyway */
memset(buf,' ',10); /* initialize buf to contain all spaces
but no \0, so strlen will still result in
reading past the array bounds */
上帝!整理一下代码,好吗?没有必要让它这么难读。这段代码想做什么?你应该给我们一个代码摘要,然后告诉我们你的问题。为什么你需要在这里显示的代码中包含所有这些@include
?看看正则表达式。lseek不可能是正确的-如果文件长度不正确怎么办10字节的精确倍数?天哪!整理一下代码好吗!没有必要让它这么难读。这段代码想做什么?你应该给我们一个代码摘要,然后告诉我们你的问题。为什么你需要这里显示的代码中所有的@include
好的-如果文件长度不是10字节的精确倍数怎么办?不能使用这些库中的任何一个。好的,但是为什么不呢?你可以使用很多其他的东西查看你的\include
。不能使用任何库。好的,但是为什么不呢?你可以使用很多其他的东西查看你的\include
。