Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从文件中读取每一行,并在C中将该行拆分为字符串和数组_C_Arrays_File_Binary_Heap - Fatal编程技术网

从文件中读取每一行,并在C中将该行拆分为字符串和数组

从文件中读取每一行,并在C中将该行拆分为字符串和数组,c,arrays,file,binary,heap,C,Arrays,File,Binary,Heap,家庭作业-我有一个作业要写一个程序来读取文件。该文件如下所示: B 34 55 66 456 789 78 59 2 220 366 984 132 2000 65 744 566 377 905 5000 I 9000 I 389 Dm DM 其中B是从一个数字数组(B后面的数字)构建一个二进制堆。 在数组/堆中插入一个数字 Dm是delete minimum,Dm是delete maximum 我已经为堆编写了代码,可以用随机数填充数组。我的问题是读取第一行,并将其解析为字符串B和数组 我

家庭作业-我有一个作业要写一个程序来读取文件。该文件如下所示:

B 34 55 66 456 789 78 59 2 220 366 984 132 2000 65 744 566 377 905 5000
I 9000
I 389
Dm
DM
其中
B
是从一个数字数组(B后面的数字)构建一个二进制堆。 在数组/堆中插入一个数字 Dm是delete minimum,Dm是delete maximum

我已经为堆编写了代码,可以用
随机数
填充数组。我的问题是读取
第一行
,并将其解析为
字符串B
数组

我尝试过使用下面的代码,但显然它不起作用

char line[8];
char com[1];
int array[MAX] //MAX is pre-defined as 100

FILE* fp = fopen( "input_1.txt", "r" );
if( fp )
{
    while( fgets ( line, sizeof(line), fp ) != NULL  )
    {
        sscanf(line, "%s" "%d", &com, &array );
        ... //Following this, I will have a nested if that will take
        each string and run the appropriate function.

        if ( strcmp( com, "B" ) == 0 )
        {
            fill_array( array, MAX );
            print_array( array, MAX );
        }        

在总共3天的时间里,我已经阅读了大约6个小时,但找不到解决问题的方法。任何帮助都会很好。

这里有一个小程序,它将打开一个文件,读取一行内容,并将在空格中找到的内容进行拆分:

void main()
{
    char str[50];
    char *ptr;
    FILE * fp = fopen("hi.txt", "r");
    fgets(str, 49, fp);             // read 49 characters
    printf("%s", str);              // print what we read for fun
    ptr = strtok(str, " ");         // split our findings around the " "

    while(ptr != NULL)  // while there's more to the string
    {
        printf("%s\n", ptr);     // print what we got
        ptr = strtok(NULL, " "); // and keep splitting
    }
    fclose(fp);
 }
因此,我要在包含以下内容的文件上运行此操作:

B 34 55 66 456 789 78 59 2 220
我可以期待看到:

B 34 55 66 456 789 78
B 
34
55 
66
456
789
78

我想你可以看看如何修改它来帮助你自己。

这里有一个小程序,它将打开一个文件,读取一行内容,并将在空格中找到的内容拆分:

void main()
{
    char str[50];
    char *ptr;
    FILE * fp = fopen("hi.txt", "r");
    fgets(str, 49, fp);             // read 49 characters
    printf("%s", str);              // print what we read for fun
    ptr = strtok(str, " ");         // split our findings around the " "

    while(ptr != NULL)  // while there's more to the string
    {
        printf("%s\n", ptr);     // print what we got
        ptr = strtok(NULL, " "); // and keep splitting
    }
    fclose(fp);
 }
因此,我要在包含以下内容的文件上运行此操作:

B 34 55 66 456 789 78 59 2 220
我可以期待看到:

B 34 55 66 456 789 78
B 
34
55 
66
456
789
78

我想你可以看看如何修改它来帮助你自己。

首先,
数组的大小应该大于8,可能类似于
字符行[256]
。对于
com
数组也是如此,它应该至少有3个字符

char line[256];
char com[3];
您必须使用
fgets(line,sizeof(line),fp)
逐行读取文件,并使用
strok()
将命令与命令参数分开

char separators[] = " ";
fgets(line, sizeof(line), fp);

char * p = strtok(line, separators);    // p will be a pointer to the command string
strncpy(&com, p, sizeof(com));    // copy the command string in com


// If the command is B, read an array
if (strcmp(com, "B") == 0) {
    p = strtok(NULL, separators);

    while (p != NULL) {
        int value_to_add_to_your_array = atoi(p);
        // ... add code to add the value to your array
        p = strtok(NULL, separators);
    }

    // ... call the function that creates your heap
}

// ... add code to handle the other commands
其思想是逐行读取文件,然后针对每一行首先读取命令,并根据其值确定应以何种方式读取该行的其余部分


在上面的代码中,我考虑了
B
命令,我为该命令添加了读取数组的代码。

首先,
数组的大小应该大于8,可能类似于
字符行[256]
。对于
com
数组,也应该至少有3个字符

char line[256];
char com[3];
您必须使用
fgets(line,sizeof(line),fp)
逐行读取文件,并使用
strok()
将命令与命令参数分开

char separators[] = " ";
fgets(line, sizeof(line), fp);

char * p = strtok(line, separators);    // p will be a pointer to the command string
strncpy(&com, p, sizeof(com));    // copy the command string in com


// If the command is B, read an array
if (strcmp(com, "B") == 0) {
    p = strtok(NULL, separators);

    while (p != NULL) {
        int value_to_add_to_your_array = atoi(p);
        // ... add code to add the value to your array
        p = strtok(NULL, separators);
    }

    // ... call the function that creates your heap
}

// ... add code to handle the other commands
其思想是逐行读取文件,然后针对每一行首先读取命令,并根据其值确定应以何种方式读取该行的其余部分

在上面的代码中,我考虑了
B
命令,为此我添加了读取数组的代码