首页 > 解决方案 > 读取现有文件以在c中处理数据

问题描述

我已从 ` 修改了现有代码

https://www.programiz.com/c-programming/examples/lexicographical-order

完成修改以从现有的指向数组的指针中读取。但是,我不断得到标志输出。https://i.stack.imgur.com/iqWBb.jpg

    char *s1, *st1;
    int i, j;
    char arr[10][10], temp[50];

        s1 = strtok(str1, ";");
        do
        {
            st7 = strstr(s1, "s__");
            if (st7 != NULL)
            {
                for (i = 0;i < 10; ++i)
                {
                    for (j = i + 1; st7[j] < 10; ++j)
                    {                           
                        if(strcmp(arr[st7[i]], arr[st7[j]]) > 0)
                        {
                            strcpy(temp, arr[st7[i]]);
                            strcpy(arr[st7[i]], arr[st7[j]]);
                            strcpy(arr[st7[j]], temp);
                        }                           
                    }
                }
                printf("%s\n", arr[i]);     
            }           
        } while (s1 = strtok(NULL, ";"));

s1:分隔字符串

st7:从字符串中选择子字符串(主要结果)

str1:通过主文件的子字符串初始化(我使用了fopen)结果包含以eg开头的名称:k__hi,s__bye

进行修改是为了在从 st7 [从字符串(s1)中选择的子字符串] 中获取字符串时按字典顺序组织字符串。

请告知,因为我是 c 编程的新手 :)

标签: cdictionary

解决方案


If str1 points to the string "k__hi, s__bye" then st7 points to "s__bye". So when you do

if(strcmp(arr[st7[i]], arr[st7[j]]) > 0)

with i equal 0 and j equal 1, you do:

if(strcmp(arr[st7[0]], arr[st7[1]]) > 0)

since st7 points to the string "s__bye", it is the same as

if(strcmp(arr['s'], arr['_']) > 0)

Using 's' and '_' as array index is not what you want as the array is defined as arr[10][10], i.e. the valid index is 0 to 9 and 's' is more than 10. In other words - your access is outside the array and therefore the code has undefined behavior.

Further the arr is uninitialized so you are not comparing any valid data. Once again this is undefined behavior.

So you need to do two things:

1) Initialize the array

2) Fix the index so that it's always in the range 0..9

It's a bit unclear what you are trying but I guess that you should copy the string pointed to by st7 into the array and then sort the array. Perhaps like:

        if (st7 != NULL)
        {
            strcpy(arr[0], st7);  // Not sure which index to use here
                                  // So I just used index 0

            for (i = 0;i < 10; ++i)
            {
                for (j = i + 1; j < 10; ++j)
                {                           
                    if(strcmp(arr[i], arr[j]) > 0)  // Only use i and j for indexing
                    {
                        strcpy(temp, arr[i]);
                        strcpy(arr[i], arr[j]);
                        strcpy(arr[j], temp);
                    }                           
                }
                printf("%s\n", arr[i]);     // Moved inside the loop
            }
        }           

Putting it all together in an example with 4 words could be:

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

#define NUM_WORDS 4
char arr[NUM_WORDS][50];

void add_word(char* str1)
{
    char *s1, *st1, *st7;
    int i, j;
    char temp[50];

    s1 = strtok(str1, ";");
    do
    {
        st7 = strstr(s1, "s__");
        if (st7 != NULL)
        {
            strcpy(arr[0], st7 + 3);
            for (i = 0;i < NUM_WORDS; ++i)
            {
                for (j = i+1; j < NUM_WORDS; ++j)
                {
                    if (strcmp(arr[i], arr[j]) > 0)
                    {
                        strcpy(temp, arr[i]);
                        strcpy(arr[i], arr[j]);
                        strcpy(arr[j], temp);
                    }
                }
            }
        }           
    } while (s1 = strtok(NULL, ";"));   
}

int main(void) {
    for(int i=0; i<10;++i) strcpy(arr[i], "");
    char s[] = "s__hello;s__seeyou;s__bye;s__hi";
    add_word(s);
    for(int i=0; i<10;++i) printf("%s\n", arr[i]);
    return 0;
}

Output:

bye
hello
hi
seeyou

推荐阅读