简体   繁体   中英

Read/write from text file in C++

I ma quite new C++ programmer trying to implement following codes that try to find the all paths between two given nodes (graph).It uses connected edges pairs and a given node pair to calculate all possible paths between them as input from console and write the all possible paths between a given node pair to console.The algorithm works very good. However, I would like to read/write inputs/output from/to a txt file. But I could not do it. Is there anyone to show the correct way?

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <iostream>
#include <fstream>
using namespace std;

vector<vector<int> >GRAPH(100);
inline void printPath(vector<int>path)
{
    cout<<"[ ";
    for(int i=0; i<path.size(); ++i)
    {
        cout<<path[i]<<" ";
    }
    cout<<"]"<<endl;
}

bool checksAdjacencyNode(int node,vector<int>path)
{
    for(int i=0; i<path.size(); ++i)
    {
        if(path[i]==node)
        {
            return false;
        }
    }
    return true;
}

int findpaths(int sourceNode ,int targetNode,int totalnode,int totaledge)
{
    vector<int>path;
    path.push_back(sourceNode);
    queue<vector<int> >q;
    q.push(path);
    while(!q.empty())
    {
        path=q.front();
        q.pop();
        int lastNodeOfPath=path[path.size()-1];
        if(lastNodeOfPath==targetNode)
        {
            printPath(path);
        }
        for(int i=0; i<GRAPH[lastNodeOfPath].size(); ++i)
        {
            if(checksAdjacencyNode(GRAPH[lastNodeOfPath][i],path))
            {
                vector<int>newPath(path.begin(),path.end());
                newPath.push_back(GRAPH[lastNodeOfPath][i]);
                q.push(newPath);
            }
        }
    }
    return 1;
}

int main()
{
    int T,totalNodes,totalEdges,u,v,sourceNode,targetNode;
    T=1;
    while(T--)
    {
        totalNodes=6;
        totalEdges=11;
        for(int i=1; i<=totalEdges; ++i)
        {
            scanf("%d%d",&u,&v);
            GRAPH[u].push_back(v);
        }
        sourceNode=1;
        targetNode=4;
        findpaths(sourceNode,targetNode,totalNodes,totalEdges);
    }
    return 0;
}

Input::
1 2 
1 3 
1 5 
2 1
2 3
2 4
3 4
4 3
5 6
5 4
6 3

output:

[ 1 2 4 ]
[ 1 3 4 ]
[ 1 5 4 ]
[ 1 2 3 4 ]
[ 1 5 6 3 4 ]

Regarding output you could simply use a std::ofstream and replace where you use std::cout :

inline std::ostream& os printPath(std::ostream& os, vector<int>path)
{
    os <<"[ ";
    for(int i=0;i<path.size();++i)
    {
        os<<path[i]<<" ";
    }
    os<<"]"<<endl;
}

int main(int argc, char** argv)
{
    if(argc > 1)
    {
        std::ofstream ofs(argv{1]);

        // ...
        printPath(ofs,path)
    }
}

Regarding reading this format from a file, I'd suggest s.th. like this:

std::ifstream ifs("MyGraphFile.txt");

while(ifs && !ifs.eof())
{
    std::string line = std::string::getline(ifs);
    // strip the '[' and ']' characters
    line = line.substr(1,line.length() - 2);
    std::istringstream iss(line);

    std::vector<int> currentInputs;
    int value;
    while(iss >> value)
    {
        currentInputs.push_back(value);
    }

    GRAPH.push_back(currentInputs);
    currentInputs.clear();
}

I thought this would be a breeze with Boost Graph Library. And, in a way, it is.

Parsing the input file:

std::vector<std::pair<int, int>> parse_input(const char* const fname, int& min_vertex, int& max_vertex)
{
    std::vector<std::pair<int, int>> data;

    min_vertex = std::numeric_limits<int>::max();
    max_vertex = std::numeric_limits<int>::min();

    std::ifstream ifs("input.txt");
    std::string line;
    while (std::getline(ifs, line))
    {
        int a, b;
        if (std::istringstream(line) >> a >> b)
        {
            data.emplace_back(a, b);

            if (a>b) std::swap(a,b);
            min_vertex = std::min(min_vertex, a);
            max_vertex = std::max(max_vertex, b);
        }
        else throw "oops";
    }
    return data;
}

Then, a simple stub for the program should be:

struct my_visitor : boost::default_bfs_visitor 
{
    template < typename Vertex, typename Graph >
        void discover_vertex(Vertex const& u, const Graph & g) const
        {
            std::cout << "discover_vertex: " << u << "\n";
        }
};

int main() 
{
    typedef boost::adjacency_list<> Graph;
    typedef Graph::vertex_descriptor Vertex;

    int min_vertex, max_vertex;
    auto const lines = parse_input("input.txt", min_vertex, max_vertex);

    const Graph G(begin(lines), end(lines), max_vertex+1);
    print_graph(G);

    const auto source = vertex(min_vertex, G);

    breadth_first_search // visit ... needs explicit ColorMap
        (G, source, boost::visitor(my_visitor()));
}

This compiles and works, see it live on Coliru .

However, BFS by default searches all vertices only once (using color coding):

0 --> 
1 --> 2 3 5 
2 --> 1 3 4 
3 --> 4 
4 --> 3 
5 --> 6 4 
6 --> 3 
discover_vertex: 1
discover_vertex: 2
discover_vertex: 3
discover_vertex: 5
discover_vertex: 4
discover_vertex: 6

We'd actually need to use breadth_first_visit instead, while leaving the color for each vertex at WHITE, to generate all paths.

Sadly, I ran out of time trying to see how to feed breadth_first_visit a suitable custom ColorMap .

I hope this somehow helps, if only for input parsing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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