簡體   English   中英

從交流弦中刪除字符

[英]remove characters from a c string

gcc 4.4.4 c89

我正在從一個文本文件中讀取內容,該文本文件包含雙引號中的名稱。

"Simpson, Homer"
etc

但是,我想從字符串中刪除雙引號。

我就是這樣做的,但是我不確定這是最好的方法。

int get_string(FILE *in, char *temp)
{
    char *quote = NULL;
    /* Get the first line */
    fgets(temp, STRING_SIZE, in);
    printf("temp before [ %s ]\n", temp);
    /* Find the second quote */
    if((quote = strrchr(temp, '"')) == NULL) {
        fprintf(stderr, "Text file incorrectly formatted\n");
        return FALSE;
    }
    /* Replace with a nul to get rid of the second quote */
    *quote = '\0';

    /* Move the pointer to point pass the first quote */
    temp++;
    printf("temp after [ %s ]\n", temp);
    return TRUE;
}

非常感謝您的任何建議,

不,這行不通。 您正在更改參數temp ,但調用函數仍將具有舊值。 函數外部的temp將指向開始的報價。 您應該將字符移動到緩沖區中。

但是我建議在堆中分配緩沖區並返回指向它的指針,讓調用者在需要時釋放緩沖區。 這似乎是一個更清潔的解決方案。 再次,這樣您就不會依賴調用方傳遞足夠大的緩沖區。

通常,由於缺乏自動內存分配功能,因此在C語言中從文本文件中讀取大量內容並非易事。 如果可能切換到C ++,我建議嘗試使用更簡單的C ++ getline

所有行看起來都是這樣嗎?為什么不簡單地刪除第一個和最后一個字符?

quote++; // move over second char
quote[strlen(quote)-1]='\0'; // remove last char

假設

string =“ \\”辛普森,荷馬\\“”

然后

string_without_quotes =字符串+1;

string_without_quotes [strlen(string)-2] ='\\ 0';

准備!

char *foo(char *str, int notme)
{
    char *tmp=strdup(str);
    char *p, *q;
    for(p=str, q=tmp; *p; p++)
    {
       if((int)*p == notme) continue;
       *q=*p;
       q++;
    }
    strcpy(str, tmp);
    free(tmp);
    return str;
}

簡單的通用刪除字符

不知道這是否有幫助,這是我使用的簡單令牌生成器

#include <stdlib.h>
#include <string.h>

int token(char* start, char* delim, char** tok, char** nextpos, char* sdelim, char* edelim) {
    // Find beginning:
    int len = 0;
    char *scanner;
    int dictionary[8];
    int ptr;

    for(ptr = 0; ptr < 8; ptr++) {
        dictionary[ptr] = 0;
    }

    for(; *delim; delim++) {
        dictionary[*delim / 32] |= 1 << *delim % 32;
    }

    if(sdelim) {
        *sdelim = 0;
    }

    for(; *start; start++) {
        if(!(dictionary[*start / 32] & 1 << *start % 32)) {
            break;
        }
        if(sdelim) {
            *sdelim = *start;
        }
    }

    if(*start == 0) {
        if(nextpos != NULL) {
            *nextpos = start;
        }
        *tok = NULL;
        return 0;
    }

    for(scanner = start; *scanner; scanner++) {
        if(dictionary[*scanner / 32] & 1 << *scanner % 32) {
            break;
        }
        len++;
    }

    if(edelim) {
        *edelim = *scanner;
    }

    if(nextpos != NULL) {
        *nextpos = scanner;
    }

    *tok = (char*)malloc(sizeof(char) * (len + 1));

    if(*tok == NULL) {
        return 0;
    }

    memcpy(*tok, start, len);
    *(*tok + len) = 0;


    return len + 1;
}

參數為:

  • char *開始,(指向字符串的指針)
  • char * delim,(指向用於分隔字符串的定界符的指針)
  • char ** tok,對char *變量的引用(使用&),該變量將保存該toke
  • char ** nextpos,對char *變量的引用(使用&),該變量將保留最后一個標記之后的位置。
  • char * sdelim,對char變量的引用(使用&),該變量將保存-start分隔符的值
  • char * edelim,對char變量的引用(使用&),該變量包含結束定界符的值

后三個是可選的。

傳遞起始地址,分隔符為“,並傳遞對char *的引用以保存實際的中間字符串。

結果是新分配的字符串,因此您必須釋放它。

int get_string(FILE *in, char *temp)
{
    char *token = NULL;
    /* Get the first line */
    fgets(temp, STRING_SIZE, in);
    printf("temp before [ %s ]\n", temp);
    /* Find the second quote */
    int length = token(temp, "\"", &token, NULL, NULL, NULL)

    // DO STUFF WITH THE TOKEN
    printf("temp after [ %s ]\n", token);
    // DO STUFF WITH THE TOKEN

    // FREE IT!!!
    free(token);
    return TRUE;
}

令牌生成器是一種多功能工具,可以在很多地方使用,這是一個非常小的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM