简体   繁体   English

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

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

I am having trouble reading a specific amount of words from a text file. 我无法从文本文件中读取特定数量的单词。 The program i have so far reads two strings from a text file, and stores it in a linked list. 到目前为止,我拥有的程序从文本文件中读取两个字符串,并将其存储在链接列表中。 However, the values read from the text file should be: 但是,从文本文件读取的值应为:

(Command) (Value) (命令)(值)

in that order and no more than that. 按此顺序,仅此而已。 If I add an extra command or value it stores that string in the next node of the list and shifts everything by one. 如果我添加一个额外的命令或值,它将把该字符串存储在列表的下一个节点中,并将所有内容都移动一个。 My problem is that I cannot find a way to error check for extra commands on the same line in the text file. 我的问题是我无法找到一种方法来对文本文件中同一行上的其他命令进行错误检查。 My initial thought was to just read the first two strings and ignore anything else on the line. 我最初的想法是只读取前两个字符串,而忽略该行上的其他任何内容。 If there are any other ways to solve this problem please do tell me so. 如果还有其他方法可以解决此问题,请告诉我。 Any help to improve my code is appreciated! 任何帮助改善我的代码的人表示赞赏!

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

Try this loop: 试试这个循环:

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 */
  }

}

You can allocate space and pass your values in the same loop. 您可以分配空间并在同一循环中传递值。 You can use strtok to get the string until the first occurance of space and strdup to allocate space and assign the value at the same time. 您可以使用strtok来获取字符串,直到第一次出现空格为止,然后使用strdup来分配空间并同时分配值。 So now if you have more than one (command) (value) on the same line it will be added. 因此,现在如果在同一行上有多个(命令)(值),它将被添加。

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