程序不写入另一个文件

问题描述 投票:1回答:1

该程序应该是用户对区域代码的输入,搜索一个单独的文件,其中包含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 */
}

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

c tokenize
1个回答
1
投票

您的计划中存在多个问题:

  • 数组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)你应该将令牌与area_code进行比较: if (strcmp(area_code, pch) == 0)
  • read_line也有一个潜在的问题:它的参数是要读取的最大字符数,这与fgets()的参数不同,后者是目标数组的大小。这是令人困惑的,如果为size参数赋予sizeof area_code函数,可能会导致错误。
© www.soinside.com 2019 - 2024. All rights reserved.