簡體   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