简体   繁体   English

从文本文件获取说明

[英]Get instructions from text file

I have a homework. 我有作业 I have to create a hash table and use linked list to solve colissions. 我必须创建一个哈希表并使用链接列表来解决大肠菌病。 The hashtable is working quite good. 哈希表工作得很好。 Part of the asignment is reading a file and parse the contents to get instruccions. 任务的一部分是读取文件并解析内容以获取指令。

The file contents: 文件内容:

Load("Via Lactea", "Galaxia")

Load("Galaxia", "Sistema Solar", "Sol")

Load("Via Lactea", "Hoyo negro", "001")

Find("Via Lactea","Luna")

Delete("Via Lactea","Jupiter")

Show()

My question what's the best (and easiest) way to create a C/C++ program to read the file contents and parse the instructions to operate my program. 我的问题是,创建C / C ++程序以读取文件内容并解析指令以操作我的程序的最佳(最简便)方法是什么。 I'm new to C/C++ so i'm not sure what's the best way to solve this. 我是C / C ++的新手,所以我不确定什么是解决此问题的最佳方法。

How can i read one line and know what kind of instruction is? 我如何阅读一行并知道是哪种指示?

I would like to know some ideas 我想知道一些想法

(My hashtable code is here http://pastebin.com/yVEeqvzG ) (我的哈希表代码在这里http://pastebin.com/yVEeqvzG

Since the main goal of your assignment is the hashtable part, you might want to make a quick-and-dirty hack that parses your file, just so you can start with the main part quickly. 由于分配的主要目标是哈希表部分,因此您可能希望进行快速,肮脏的黑客操作来解析文件,以使您可以快速开始使用主体部分。

The following is written in C though it will wirk in C++ too. 以下内容是用C语言编写的,尽管它也会在C ++中使用。

char line[100], command[100], word1[100], word2[100], word3[100];
FILE* f = fopen("whatever", "rt");

while (fgets(line, sizeof(line), f)) // read one line of text from file
{
    // The following is a format string for scanf.
    // It matches an opening quote, some text, and a closing quote.
    #define WORD "\"%[^\"]\""

    // Try to parse the line of text, applying all possible patterns.
    if (sscanf(line, "Load("WORD", "WORD", "WORD")\n", word1, word2, word3) == 3)
    {
        ...
    }
    else if (sscanf(line, "Load("WORD", "WORD")\n", word1, word2) == 2)
    {
        ...
    }
    else if (sscanf(line, "Find("WORD", "WORD")\n", word1, word2) == 2)
    {
        ...
    }
    else if (strcmp(line, "Show()\n") == 0)
    {
        ...
    }
}

Obligatory note: this usage of sscanf has security holes though you probably don't care about it. 强制性注释: sscanf这种用法存在安全漏洞,尽管您可能并不在意。

This basic snippet is able to load the file line by line. 这个基本代码段可以逐行加载文件。 How to manage the parsing is your duty, I would go with strtok_s but you will have to care about trimming space, checking for right amount of parameters, extract double quotes from strings and whatever. 如何管理解析是您的职责,我将与strtok_s一起使用,但是您将不得不考虑修剪空间,检查正确数量的参数,从字符串中提取双引号等。

#include <iostream>
#include <fstream>
using namespace std;

int main () {
  filebuf fb;
  fb.open("data.txt",ios::in);
  istream is(&fb);
  char buffer[256];

  while ((is.rdstate() & ifstream::eofbit) == 0) {
    is.getline(buffer,256);

    // handle your parsing here
  }

  fb.close();
  return 0;
}

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

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