在分隔符处拆分文本文件,并在c中分隔整数和文本

时间:2016-11-28 20:33:27

标签: c io

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

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

*/

1 个答案:

答案 0 :(得分:0)

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

#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()并对每行输入重复使用完整的代码逻辑。 希望能帮助到你!请询问您是否需要完整的代码或代码的任何说明。