简体   繁体   English

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

[英]Read each line from a file and split the line into a string and an array in C

Homework - I have an assignment to write a program that will read a file. 作业 -我分配了一个程序来读取文件。 The file looks like this: 该文件如下所示:

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

Where B is build a binary heap from an array of numbers(the numbers following the B. I is insert a number into the array/heap Dm is delete minimum and DM is delete maximum. 其中B是从数字数组构建二进制堆(B后面的数字。我是在数组/堆中插入一个数字Dm是删除最小值,而DM是删除最大值。

I have written the code for the heap, and can fill an array with random numbers . 我已经为堆编写了代码,并且可以用random numbers填充数组。 My problem is reading that first line and parsing it into a string B and an array . 我的问题是读取first line并将其解析为string Barray

I have tried using the following code but obviously it does not work. 我尝试使用以下代码,但显然不起作用。

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

I have read for about 6 hours over a total of 3 days and can't find a solution to my problem. 我已经阅读了大约6个小时,总共3天,但无法找到解决我问题的方法。 Any help would be great. 任何帮助都会很棒。

Here's a little program that will open a file, read out 1 line, and split what it finds around spaces: 这是一个小程序,它将打开一个文件,读出1行,并在空格附近分割它找到的内容:

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

So were I to run this on a file containing: 所以我要在包含以下内容的文件上运行此文件:

B 34 55 66 456 789 78 59 2 220

I could expect to see: 我可以期待看到:

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

I think you can see how to modify this to help yourself out. 我认为您可以看到如何修改此设置以帮助自己。

First of all, the line array should probably have a size greater than 8, probably something like char line[256] . 首先, line数组的大小可能应该大于8,可能类似于char line[256] The same goes for the com array, which should have at least 3 characters. com数组也是如此,该数组至少应包含3个字符。

char line[256];
char com[3];

You'll have to read the file line by line using fgets(line, sizeof(line), fp) and the use strtok() to separate the command from the command arguments. 您必须使用fgets(line, sizeof(line), fp)读取文件fgets(line, sizeof(line), fp)并使用strtok()将命令与命令参数分开。

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

The idea is to read the file line by line, then for each line to first read the command and based on its value determine in which way you should read the rest of the line. 想法是逐行读取文件,然后针对每一行首先读取命令,并根据其值确定应以哪种方式读取其余部分。

In the code above I considered the B command, for which I added code to read an array. 在上面的代码中,我考虑了B命令,为此我添加了代码以读取数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从文件中读取行并将每个单词存储到数组(C语言)中 - read line from file and store each word into an array (C language) 如何从C中的文件中读取每一行到一个整数数组? - How to read each line into an integer array, from a file in C? 如何读取文件并将每个readed行拆分为变量或数组 - how to read a file and split each readed line into variables or array 如何读取c中的文本文件,然后将每一行拆分为标记? - how to read a text file in c and then split each line into tokens? 如何使用分隔符分割我从文件中读取的行并将它们存储在 C 的每个 int 变量中 - How to split line I read from file with delimiters and store them in each int variable in C 如果我打算将文件分成字符串,如何从文件中读取每一行? C - How to read each line from a file if I intend to split it up into strings? C 读取C中的一行并将其拆分 - Read a line in C and split it 逐行读取文本文件并检索C中每一行的内容 - Read a text file line by line and retrieve the content of each line in C 如何将每一行放入从 c 程序中的文件读取的数组中? - How do I put each line in an array which read from the file in c program? 从文件中逐个字符读取并将每一行放入一个字符串中 - Read character by character from a file and put each line in a String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM