【问题标题】:Reading data from multiple .txt files with c++使用 C++ 从多个 .txt 文件中读取数据
【发布时间】:2014-08-16 23:06:54
【问题描述】:

我需要从一个目录中的多个文件(64 个文件)中提取一系列信息。每个文件由几行组成,例如:

运算符对:0& ins: 0& rmv: 0& weight: 0.124354

运算符对:1& ins: 1& rmv: 0& weight: 0.00672458

运算符对:2& ins: 2& rmv: 0& weight: 0.000467531

...

信息存储在向量中。我写了一个代码,但是,我收到一个分段错误错误。这是我的代码。谁能指出问题出在哪里?

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <dirent.h>

using namespace std;

struct info {
    char* name;
    vector<vector<double>> weights; // dest-const-weight
};

int main(int c, char* v[]) {
    struct dirent* pDirent;
    DIR* pDir;
    char buffer[50];
    strcpy(buffer, v[1]);

   if (c < 2) {
      printf ("Usage: testprog <dirname>\n");
      return 1;
   }

   pDir = opendir (buffer);

   if (pDir == NULL) {
       printf ("Cannot open directory '%s'\n", v[1]);
       return 1;
   }

    vector<info> Information;
    for (int inst = 0; inst < 64; inst++) {
        info temp;
        temp.weights.resize(9);
        for (int j = 0; j < 9; j++)
            temp.weights[j].resize(4);
        Information.push_back(temp);
    }
    int cmp = 0;
    while ((pDirent = readdir(pDir)) != NULL) {
        if (strcmp(pDirent->d_name, ".") != 0 &&
            strcmp(pDirent->d_name, "..") != 0) {
            char line[1024];
            ifstream file(pDirent->d_name);
            Information[cmp].name = pDirent->d_name;
            int oper = -1;
            int dest = -1;
            int ins = -1;
            double weight = -1;

            for (int l = 0; l < 36; l++) {
                file.getline(line, 1024);
                // cout<<line<<endl;
                sscanf(line,
                       "Pair of Operators: %d& ins: %d& rmv: %d&  weight: %f",
                       &oper, &dest, &ins, &weight);
                // sscanf(line, "%*s %d%*s %d%*s %d%*S %f", &oper, &dest, &ins,
                // &weight);
                // cout<<oper<<"   "<<dest<<"   "<<ins<<"   "<<weight<<endl;
                Information[cmp].weights[dest][ins] = weight;
            }
            cmp++;
        }
    }

    closedir(pDir);
    return 0;
}

【问题讨论】:

  • 分段错误时执行的是哪一行代码?
  • 我认为它来自这一行:ifstream file(pDirent->d_name);

标签: c++ unix filereader


【解决方案1】:

我可以看到两个明显的段错误。 首先,您将v[1] 复制到缓冲区中。您是否为您的程序提供任何参数? 我即时看到的第二个问题是您尝试在未初始化的 DIR 指针上调用 readdir。 试试DIR* pDir = opendir(...);

当参数计数不匹配时,您应该始终打印使用消息!

if (c != 1) {
  fprintf(stderr, "USAGE: %s needs exactly one argument!\n" v[0]);
  return 0;
}

还有一件事我忘了提。您可以使用-g 标志编译您的程序并尝试使用调试器来查找这些错误。

【讨论】:

  • 我提供了应该从中读取文件的目录的名称作为参数。这是 main.cpp 目录的子目录。我想知道 ifstream file(pDirent->d_name);是否在正确的目录中查找文件!
  • 如果您想提供路径以我在上面发布的 if 条件开始。然后初始化DIR* pDir = opendir(v[1]);后跟if (!pDir) { perror("Dir not found.\n"); return 1; }看目录是否找到。
  • 首先确保您的代码可以编译,因为您在权重vector&lt;vector&lt;double&gt; &gt; 的定义处遗漏了一个空格。在那之后重新考虑你在这里做什么Information[cmp].weights[dest][ins] = weight;尝试打印这些值。我猜你得到了一些导致程序崩溃的-1 值。
  • 当我打印“行”时,它是空的。我认为文件根本没有打开。
  • 当我在最后一个“for”的开头打印“line”时,它是一个空行。所以“oper”、“dest”和“weight”的所有值都保持为-1。我认为该文件根本没有打开。我想知道 ifstream file(pDirent->d_name);它的工作与否!
猜你喜欢
  • 2012-01-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-04
  • 2012-02-08
相关资源
最近更新 更多