繁体   English   中英

在文本文件C中最多读取两行时出现问题

[英]Problem reading at most two lines in text file, C

我无法从文本文件中读取特定数量的单词。 到目前为止,我拥有的程序从文本文件中读取两个字符串,并将其存储在链接列表中。 但是,从文本文件读取的值应为:

(命令)(值)

按此顺序,仅此而已。 如果我添加一个额外的命令或值,它将把该字符串存储在列表的下一个节点中,并将所有内容都移动一个。 我的问题是我无法找到一种方法来对文本文件中同一行上的其他命令进行错误检查。 我最初的想法是只读取前两个字符串,而忽略该行上的其他任何内容。 如果还有其他方法可以解决此问题,请告诉我。 任何帮助改善我的代码的人表示赞赏!

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


/*This typedefs a struct...*/
typedef struct LinkedListNode
{
    char* commandstring;
    char* valuestring;
    char valueint;
    struct LinkedListNode *next;
}LINKEDLISTNODE;


int main (int argc, char *argv[])
{
    FILE* fp;
    LINKEDLISTNODE *current, *head, *temp;

    int integer_check;

    head = NULL;
    current = head;


    fp = fopen (argv[1], "r");


    /*This will set a buffer to find the maximum length we need for the buffer. The max length will be the length of the longest line in the text file.*/
    fseek(fp,0, SEEK_END);
    long filesize = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    char* buffer = malloc(filesize + 1);

    char tempCommand[filesize];
    char tempValue[filesize];


    /*Initialise linked list with the same amount of nodes that the text file has lines*/
    while(fgets(buffer, filesize, fp) != NULL)
    {
        LINKEDLISTNODE* node = malloc(sizeof(LINKEDLISTNODE));
        node->commandstring = (char*)malloc(sizeof(char)*8);
        node->valuestring = (char*)malloc(sizeof(char)*5);
        node->next = NULL;

        if (head == NULL)
        {
            head = node;
            current = head;
        }
        else
        {
            current->next = node;
            current = current->next;
        }
    }



    /*Allocate the command string to the command field and the value string to the value field:*/
    current = head;
    rewind(fp);
    while(current != NULL)
    {
        fscanf(fp, "%s %s\n", current->commandstring, current->valuestring);
        current = current->next;
    }


    /*Print the list to make sure the strings are set correctly in the fields*/ 
    current = head;
    rewind(fp);
    while(current != NULL)
    {
        printf("node[%p]:[%s],[%s] \n", current->commandstring, current->commandstring, current->valuestring);
    current = current->next;
    }
    /*Free each node:*/
    current = head;
    while(current != NULL)
    {
        temp = current->next;
        current = temp;
    }

    free(head);
    free(temp);
    free(current);
    fclose (fp);

    return (0);
}

试试这个循环:

while (current != NULL) {

  char temp[8 + 5 + 1];

  if (fgets(temp, sizeof(temp), fp) != NULL) {

    const char* space = strchr(temp, ' ');

    if (space == NULL) {
      strcpy(current->commandstring, temp);
      *current->valuestring = 0;
    }
    else {
      strncpy(current->commandstring, temp, space - temp);
      strcpy(current->valuestring, space + 1);
    }

    /* rest of your loop code */
  }

}

您可以分配空间并在同一循环中传递值。 您可以使用strtok来获取字符串,直到第一次出现空格为止,然后使用strdup来分配空间并同时分配值。 因此,现在如果在同一行上有多个(命令)(值),它将被添加。

while(fgets(buffer, filesize, fp) != NULL) {

    char * command = strtok(buffer, " \n");
    char * value = NULL;

    while ((value = strtok(NULL, " \n")) != NULL) {

        LINKEDLISTNODE* node = malloc(sizeof(LINKEDLISTNODE));

        node->commandstring = strdup(command);
        node->valuestring = strdup(value);
        node->next = NULL;

        if (head == NULL) {

            head = node;
            current = head;
        }

        else {

            current->next = node;
            current = current->next;
        }

        command = strtok(NULL, " \n");
    }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM