0

这是我的代码

此代码试图从 .txt 文件中删除特殊字符,如 ",',{,},(,) 并用空格替换它们。

#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;
}

现在我希望文件中双引号内的单引号保持不变,但所有特殊字符都替换为代码中提到的空格。我希望这些单引号保持不变“what's”但是在我运行文件之后它变成了 what s 而不是 what's

4

3 回答 3

2

Have a look at regex and other libraries. (When on UNIX type man regex.) You don't have to code this anymore nowadays, there are a zillion libraries that can do this for you.

于 2013-07-10T23:12:52.740 回答
1

Ok, so the problem with your code is that you are doing one thing, that you then undo in the next section. In particular:

                    if (countcoma == 0) {
                        countcoma++;
                    }
                    if (countcoma == 1) {
                        countcoma--;
                    }

Follow the logic: We come in with countcoma as zero. So the first if is true, and it gets incremented. It is now 1. Next if says if (countcoma == 1) so it is now true, and we decrement it.

I replaced it with countcoma = !countcoma; which is a much simpler way to say "if it's 0, make it 1, if it's 1, make it 0. You could put anelseon the back of the firstif` to make the same thing.

There are also a whole bunch of stylistic things: For example hard-coded constants, writing back into the original file (means that if there is a bug, you lose the original file - good thing I didn't close the editor window with my sample file...), including half the universe in header files, and figuring which of the spec characters it is based on the index.

于 2013-07-10T23:13:38.283 回答
0

在我看来,您的代码存在比之前指出的更普遍的缺陷:

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 */
于 2013-07-10T23:57:28.610 回答