c - 程序不写入另一个文件

标签 c tokenize

这个程序应该接受用户对区号的输入,搜索一个单独的文件,该文件在 txt 文件中列出了一堆电话号码,并且函数搜索应该用输入的区号测试电话号码看看它是否匹配。 main 函数然后将电话号码写入一个单独的 txt 文件。

我曾尝试使用 strstr 但我认为测试它的最佳方法是使用 strtokstrcmp,这就是我现在有。

/* This program will search phone_numbers.txt for phone numbers. It will create a new file with unique area codes in each file.
    (Ex: 813 area codes will go into 813_phone_numbers file).
    --Brandon Yates
*/

#include <stdio.h>
#include <string.h>

int search(char *area_code, char *phone_number);
int read_line(char *str, int n);

int main() {
    char area_code[3];
    char phone_number[101];
    char *file_name;
    FILE *number_file;
    FILE *new_file;

    printf("Enter the area code: ");
    scanf("%s", &area_code);
    //area_code = &ac;
    read_line(area_code, 2);

    number_file = fopen("phone_numbers.txt", "r");
    if (number_file == NULL) {
        printf("Cannot open file.\n");
        return -1;
    }

    new_file = fopen("dest_file.txt", "a");
    if (new_file == NULL) {
        printf("Cannot open file.\n");
        return -1;
    }

    //scat = strcat(area_code, file_name);

    while (fgets(phone_number, sizeof(phone_number), number_file) != NULL && !feof(number_file) && !ferror(number_file)) {
        if (search(area_code, phone_number))
            fputs(phone_number, new_file);
    }

    fclose(number_file);
    fclose(new_file);

    printf("Output: encoded words are written to file %s", file_name);

    return 0;
}

/*
    Search function determines if a phone number in the input file
    matches the area code.

    Search function returns 1 if the phone number matches the area code
    and 0 otherwise.
*/
int search(char *area_code, char *phone_number) {
    printf("testing");
    char *pch;
    pch = strtok(phone_number, "()");
    while (pch != NULL) {
        if (strcmp(area_code, phone_number) == 0)
            return 1;
        pch = strtok(NULL, "()");
    }
    return 0;
}

int read_line(char *str, int n) {
    int ch;
    int i = 0;

    while ((ch = getchar()) != '\n') {  
        if (i < n) { 
            *str++= ch;
            i++;
        }
    }
    *str = '\0';   /* terminates string */
    return i;      /* number of characters stored */
}

我希望将电话号码写入文本文件,但最终得到的是一个空文件。这是怎么回事?

最佳答案

您的程序中存在多个问题:

  • area_code 数组太小:它只能容纳 0、1 或 2 个字符的字符串。由于您没有告诉 scanf() 将输入限制为最多 2 个字符,因此用户键入的区号将填充数组并且 scanf 会修改超出末尾的内存数组,导致未定义的行为。

  • 您将用户输入两次读取到 area_code 数组中。第一次使用 scanf(),这可能会导致未定义的行为,但会在标准输入中留下挂起的换行符,然后使用 read_line() 读取挂起的换行符并使area_code 一个空字符串...

    增大 area_code 并告诉 scanf() 要存储在那里的最大字符数,或者只使用 read_line():

        char area_code[10];
        ...
        if (scanf("%9s", area_code) != 1)
            return 1;
    
  • 另请注意,fgets(phone_number, sizeof(phone_number), number_file) != NULL && !feof(number_file) && !ferror(number_file) 是多余的:fgets( ) 将在错误和/或文件结束时返回 NULL,无需执行冗余测试。

  • search 中的测试不正确:if (strcmp(area_code, phone_number) == 0) 您应该将 token 与 area_code 进行比较:

        if (strcmp(area_code, pch) == 0)
    
  • read_line 也有一个潜在的问题:它的参数是要读取的最大字符数,这与 fgets() 的参数是不同的目标数组的大小。如果为函数的 size 参数指定了 sizeof area_code,这会造成混淆,并且以后可能会导致错误。

关于c - 程序不写入另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55343419/

相关文章:

c - C 中的泛型函数指针

如果在调用任何函数后调用 gtkfilechooser,C GTK+ 段错误

python - 如何在 NLTK 中使用 word_tokenize 忽略单词之间的标点符号?

elasticsearch - 如何在ElasticSearch中标记罗马数字术语?

c - 使用 atoi 将 char 标记化字符串为 int

C 绩效衡量

c - 双向结构指针链接,C

c - 我正在尝试打印电话号码,但出现错误

python - 使用 Look Behind 或 Look Ahead 函数查找匹配项时的正则表达式模式

java - 如何在java中标记输入文件