【问题标题】:Split a file of text at delimiters and separate integers and text in c在分隔符处拆分文本文件并在 c 中分隔整数和文本
【发布时间】:2016-11-28 20:33:26
【问题描述】:

我正在从输入文件中读取文本。我必须将文本与分数分开,即

John Doe 100 95 67 85 
jane doe 67 78 99

然后平均数字。我可以使用 strtok 用空格分隔,但我怎么知道我什么时候有一个整数?我需要将名称和整数的读取拆分为 2 个函数。我的阅读代码有效,但我需要在每个名称的末尾停下来。我尝试使用转换为字符串的数字并使用 strcmp 但它不起作用。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void read_reverse_write(FILE * ptr_file, FILE * op);
void write_and_avarage(int * fp, char * op);
//int checker(char *token);

int main(int argc, char** argv) {

    FILE *fp;
    FILE *op;
    //opens quiz and checks and checks to make sure it did
    fp = fopen("quiz.txt", "r");
    if (fp == NULL) {
        printf("Error opening file");
        return (-1);
    }
    //opens op and checks that it did 
    op = fopen("output.txt", "w");
    if (op == NULL) {
        printf("Error opening file");
        return (-1);
    }
    // runs read reverse write
    read_reverse_write(fp, op);
    fclose(fp);
    fclose(op);
    return (EXIT_SUCCESS);
}

void read_reverse_write(FILE * ptr_file, FILE * op) {
    char buf[1000];
    char *token;
    const char delim[2] = " ";
    fgets(buf, 1000, ptr_file);
    token = strtok(buf, delim);

    while (token != 100) {
            fprintf(op, "%s ", token);
            token = strtok(NULL, delim);


    }


}

/*void write_and_avarage(int * fp, char * op) {

}

int checker(char *token) {
    char *arr[102];
    char ph[4];
    for (int p = 0; p < 100; p++) {
        if (p < 10) {
            snprintf(ph, 1, "%d", p);
            arr[p] = ph;
        } else if (p < 99) {
            snprintf(ph, 2, "%d", p);
            arr[p] = ph;
        } else if (p = 100) {
            snprintf(ph, 3, "%d", p);
            arr[p] = ph;
        }
    }
    for (int z = 0; z < 100; z++) {
        if (strcmp(token, arr[z]) == 1) {
            return 1;
        } else {
            z++;
        }
        return 0;
    }
}

*/

【问题讨论】:

  • 检查 C 标准库中字符到 int 的转换。提示 atoi
  • 试过我的编译器没有把它重新编译为一个有效的函数
  • 如果您查看strtok 生成的每个令牌的第一个字符(如果不是NULL),您可以判断它是isalpha() 还是isdigit() 在第一种情况下构建名称,在第二个中,再记下一个分数。
  • hmmmm....你用的是什么编译器?你试过了吗?
  • @WeatherVane 可以工作,但是如果我的令牌长度为 20 并且只有 12 个字符怎么办?我将如何实现这一点

标签: c io


【解决方案1】:

您可以使用以下代码检查字符串是否为数字。

#include <stdio.h>
#include <string.h>
#define MAX_NUM 9
#define MIN_NUM 0
#define ZERO 0
#define MAX_SIZE 1024

int checkDigit(int num, int len)
{
    int divisor = 1, checkVal = 0;
    if(len <= 2)
        divisor = 10;
    else if(len > 2)
    {
        len = len - 1;
        while(len != ZERO)
        {
            divisor = divisor * 10;
            len = len - 1;
        }
    }
    checkVal = num/divisor;
    if(checkVal > MIN_NUM && checkVal <= MAX_NUM)
        return 1;
    else
        return 0;
}


void main()
{
    char array[MAX_SIZE] = "JOHN DOE 120 DOE HELLO 2323 90909";
    char *split_token = NULL;
    int status = 2, len = 0, sum = 0, total_digits = 0;
    float average = 0;
    split_token = strtok(array, " ");
    while( split_token != NULL )
    {
        len = strlen(split_token);
        status = checkDigit(atoi(split_token), len);
        if (1 == status)
        {
            sum = sum + atoi(split_token);
            total_digits = total_digits + 1;
        }
        split_token = strtok(NULL, " ");
    }
    average = (float)sum/total_digits;
    printf("Average is : %f\n", average);
}

此代码将检查您的字符串是否为数字,并最终计算给定字符串中所有数字的平均值。 如果您需要从文件中读取多组输入,请使用 fscanf() 并对每一行输入重复使用完整的代码逻辑。 希望能帮助到你!请询问您是否需要完整的代码或对代码的任何说明。

【讨论】:

    猜你喜欢
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    相关资源
    最近更新 更多