Read a file in C++ from a specific Line and then store it in Vectors/Array

可紊 提交于 2019-12-14 03:28:35

问题


I am new to C++ programming and I did my homework for reading a file. I am learning C++ from this cpp-tutorial: Basic-file-io site.

I have a file whose content looks like:

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...

This file has about 10000 Nodes and their corresponding CommID. The Node and CommId are seperated by TAb space in this file. I am reading this file as Input by using the following code:

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;

}

I have two questions:

  1. I want to start reading from Line 7 i.e 1 0 and so on.
  2. How can I start reading it from the line 7?

I checked lot of questions and came across that it is not possible to jump to line number if the lines of the txt files are of different length.

I wonder how to use seekg in which I need to count the bits before I can reach Line 7.

Please let me know, how to do it?

I want to get the Nodes and CommId in two seperate Integers. As soon as I have Node in an Integer, I want to search the Neighbour Nodes of this Node from a Graph file(This file is also provided as input and it has edges information). After getting the Neighbours of this Node, I want to store the collected Neighbours and their CommId (commId of every Node is available from the above file). I want to store them as pair in an Array/Vector.

For example:

After reading 1 0 from this file. I will take the Node 1 and will find the Neighbours of Node 1 from a Graph File. For every Neighbours of Node 1, I want to save the information as a pair. For example, if Node 1 has two Neighbours Node. i.e Node 63 and Node 55. If Node 63 belong to commId 100 and Node 55 belongs to CommId 101, the pair should be:

[(63,100),(55,101)..] and so on.

The learning link, STackOverflow forum suggested me to use Vectors,Map, STructs for Graphs. I haven't used Vector/Maps,Structs anytime before in my life. I am aware of Array as I used it before.

Please suggest what would be the best way.

Thanks in advance. Every help would be greatly appreciated.


回答1:


you can read text file from 7 no line by this:

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

after getting NId and CommId from #7 line, you can take it in stringstream. You can learn it from stringstream

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

Then can take 2D array and I think you must handle 2D array which if

array[row][column]

Every row define as NId and corresponding in a row you and store commId as a column value.

according to your Example: [(63,100),(55,101)..] and so on.

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

You can do it by a count and handle column value like 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;
}


来源:https://stackoverflow.com/questions/32007569/read-a-file-in-c-from-a-specific-line-and-then-store-it-in-vectors-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!