C-混合文本文件数据>到不同的数组(整数和字符串)

时间:2018-11-13 23:44:38

标签: c arrays string pointers

我只有一个普通的“ project.txt”文件,其中包含以下数据:

Programming 10 3 4 5 4 3 2 4 5 2 3
Mathematics 8 3 3 4 5 3 2 2 3
Physics 6 3 4 5 3 4 5
Design 6 5 4 5 3 2 4
Logistics 8 3 4 5 3 1 1 2 3

需要打开此文件,并将所有这些数据读写到数组中。 我需要以某种方式将String和Integer彼此分开。 *现在不需要它了,但是以后我需要将文本和数字写到不同的文件中。 ***无需立即执行此操作*

只需要对2个不同的int和char数组进行处理,但是我坐了几个小时,却找不到关于如何将此字符串与其他东西分开的正常解释。

这是我的代码,有人可以帮助我吗?

#include <stdio.h>
#include <stdlib.h>

int main(void) {
FILE *fp;
fp = fopen("C:\\Project\\project.txt", "r");
char arrayWords[140];
int i;

if(fp == NULL){
    printf("Can't Read The File!");
    exit(0);
}

for (i = 0; i < 140; i++){
    fscanf(fp, "%s,", &arrayWords[i]);
}

for (i = 0; i < 140; i++){
    printf("Number is: %s\n\n", &arrayWords[i]);
}


fclose(fp);
return 0;

}

这是我得到的输出,确实令人困惑(其中一部分)...

Number is: P13454324523M833453223P6345345D6545324L834531123

Number is: 13454324523M833453223P6345345D6545324L834531123

Number is: 3454324523M833453223P6345345D6545324L834531123

Number is: 454324523M833453223P6345345D6545324L834531123

Number is: 54324523M833453223P6345345D6545324L834531123

Number is: 4324523M833453223P6345345D6545324L834531123

Number is: 324523M833453223P6345345D6545324L834531123

Number is: 24523M833453223P6345345D6545324L834531123

Number is: 4523M833453223P6345345D6545324L834531123

Number is: 523M833453223P6345345D6545324L834531123

Number is: 23M833453223P6345345D6545324L834531123

Number is: 3M833453223P6345345D6545324L834531123

Number is: M833453223P6345345D6545324L834531123

Number is: 833453223P6345345D6545324L834531123

Number is: 33453223P6345345D6545324L834531123

Number is: 3453223P6345345D6545324L834531123

Number is: 453223P6345345D6545324L834531123

Number is: 53223P6345345D6545324L834531123

Number is: 3223P6345345D6545324L834531123

Number is: 223P6345345D6545324L834531123

Number is: 23P6345345D6545324L834531123

Number is: 3P6345345D6545324L834531123

Number is: P6345345D6545324L834531123

Number is: 6345345D6545324L834531123

Number is: 345345D6545324L834531123

Number is: 45345D6545324L834531123

Number is: 5345D6545324L834531123

Number is: 345D6545324L834531123

Number is: 45D6545324L834531123

Number is: 5D6545324L834531123

认为问题出在指针上,但不知道,我是一个初学者,找不到任何问题。

谢谢大家

1 个答案:

答案 0 :(得分:0)

使用strtol()函数可以很容易地解析这些以空格分隔的数字列表。这是一个很好的解决方案,因为它可以方便地将指针设置为刚转换的数字后的 next 空格/非数字块。如果没有要转换的数字,它将仅发送回原始指针。因此,代码对此进行了测试,以了解何时完成。

编辑:也更新为处理名称。

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

int main(void)
{
    char *number_str = "SomeWords 19 9 6 2 22 1 0 -4";
    char *ptr;
    char *next_number;
    int   numbers[1000];
    int   number_count = 0;
    char  name[100];

    next_number = number_str;

    // Copy off the name
    ptr = strchr(number_str, ' ');
    if (ptr != NULL)
    {
        strncpy(name, number_str, ptr-number_str);  // TODO - check bounds of name[]
        name[ptr-number_str] = '\0';

        // Point to the first number
        next_number = ptr+1;

        printf("Stored name [%s]\n", name);
    }

    // Then parse all the numbers
    do 
    {
        ptr = next_number;
        long num = strtol(ptr, &next_number, 10);
        if (ptr != next_number) // found one
        {
            numbers[number_count] = (int)num;
            printf("Stored %3d into numbers[%d]\n", numbers[number_count], number_count);
            number_count += 1;
        }
    } while(ptr != next_number);

    return 0;
}

输出:

Stored name [SomeWords]
Stored  19 into numbers[0]
Stored   9 into numbers[1]
Stored   6 into numbers[2]
Stored   2 into numbers[3]
Stored  22 into numbers[4]
Stored   1 into numbers[5]
Stored   0 into numbers[6]
Stored  -4 into numbers[7]