【问题标题】:Read a file in C++ from a specific Line and then store it in Vectors/Array从特定行读取 C++ 文件,然后将其存储在 Vectors/Array
【发布时间】:2015-08-14 10:10:21
【问题描述】:

我是 C++ 编程的新手,我完成了阅读文件的作业。我正在从这个cpp-tutorial: Basic-file-io 站点学习 C++。

我有一个文件,其内容如下:

Input: /path/to/the/file/
Information :xxx
Type of File: Txt file
Extra Information Value = 4
Development = 55
NId      CommId
1        0
3        0
8        7
.   .
And so on...

这个文件有大约10000 Nodes 和它们对应的CommIDNode and CommId 在此文件中由 TAb space 分隔。我正在使用以下代码将此文件作为输入读取:

ifstream commFile("CommTest.txt");
if (!commFile)
       {
         // Print an error and exit
    cerr << "Uh oh, CommunityTest File could not be opened for reading!" << endl;
                 exit(1);
             }
while(commFile)
{
    // read communityFile from the 6th Line
    string strLine;

    getline(commFile, strLine);
    cout << strLine << endl;

}

我有两个问题:

  1. 我想从第 7 行开始读取,即 1 0 等等。
  2. 如何从第 7 行开始阅读?

我检查了很多问题,发现如果txt文件的行长度不同,则无法跳转到行号。

我想知道如何使用 seekg,我需要在到达第 7 行之前计算位数。

Please let me know, how to do it?

我想在两个单独的整数中获取节点和 CommId。一旦我有一个整数中的节点,我想从图形文件中搜索这个节点的邻居节点(这个文件也作为输入提供,它有边信息)。获得该节点的邻居后,我想存储收集到的邻居及其 CommId(每个节点的 commId 都可以从上面的文件中获得)。我想将它们成对存储在数组/向量中。

例如:

从此文件中读取 1 0 后。我将使用节点 1 并将 从图形文件中找到节点 1 的邻居。对于每一个邻居 节点 1,我想将信息保存为一对。例如,如果 节点 1 有两个邻居节点。即节点 63 和节点 55。如果节点 63 属于 commId 100,节点 55 属于 CommId 101,这对 应该是:

[(63,100),(55,101)..] 等等。

学习链接,STackOverflow 论坛建议我使用 Vectors,Map, STructs for Graphs。我以前从未使用过 Vector/Maps、Structs。我知道 Array,因为我以前使用过它。

请建议最好的方法。

提前致谢。我们将不胜感激任何帮助。

【问题讨论】:

  • 您在循环中放置了一个计数器并跳过处理读取的行,直到计数器达到 7?

标签: c++ file c++11 graph stdvector


【解决方案1】:

您可以通过以下方式从 7 中读取文本文件:

for (int lineno = 0; getline (myfile,line) && lineno < 7; lineno++)
  if (lineno > 6)
      cout << line << endl;

从#7 行获取NId and CommId 后,您可以将其放入字符串流中。可以向stringstream学习

std::stringstream ss;
ss << line;
int nId,CId;
ss >> nId >> CId;

然后可以采取二维数组,我认为你必须处理二维数组,如果

array[row][column]

每个row 定义为NId 并在一行中对应您并将commId 存储为column 值。

根据您的示例:[(63,100),(55,101)..] 等等。

Here NId 63, 55 .....
So you can..
array[63][0] = 100
array[55][0] = 101
so on....

您可以通过 count 来完成,并处理像 0,1,2 这样的列值.....

【讨论】:

  • 哇。那是一个快速的反应。非常感谢您的回复和建议。如果我遇到问题,我会尝试建议的方法并返回。再次非常感谢:)
【解决方案2】:
  int main(int argc, char **argv) {
    vector<int> nodes;
    map<int, vector<int> > m;
    ifstream commFile("file.txt");
    string strLine, node_string;
    int node;
    if (!commFile.is_open()) {
        cerr << "Uh oh, CommunityTest File could not be opened for reading!" << endl;
        return -1;
    }
    // Ignore the first lines of your file, deal with the empty lines
    for(int i = 0; i < 12 && std::getline(commFile,strLine); i++) {
        cout << "Line to ignore = " << strLine << endl;
    }

    while(getline(commFile,strLine)) {
        istringstream split(strLine);
        // split the string with your nodes
        while(getline(split, node_string, ' ')) {
            std::istringstream buffer(node_string);
            buffer >> node;
            // push it into a temporary vector
            nodes.push_back(node);
        }
        if(nodes.size() >= 2) {
            // add into your map
            m[nodes[0]].push_back(nodes[1]);
        }
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2017-08-02
    相关资源
    最近更新 更多