如何在C ++中打印出文件的内容

时间:2017-04-30 23:56:05

标签: c++ fstream

目前正致力于数据结构的分配和分配如下:

编写程序以帮助您建立更好的社交关系。该程序应该读取一个数据文件,其中包含您社区中的人员列表以及谁知道谁是谁。允许用户输入关于哪些人彼此了解的各种查询。

我有一个名为“friends.txt”的文件,我试图让我的程序显示我的文本文件的实际内容。到目前为止,每次我测试我的程序时,输出都是:

C:\用户\ rehmasan \ assignment12 \ cmake的建造调试\ assignment12.exe 将其写入文件。

处理完成,退出代码为0

textfile picture

这是我的代码:

#include <iostream>
#include <cstdlib>  // provides size_t
#include <algorithm>
#include <cassert> // provides assert
#include <fstream> // handles file streaming
#include <sstream> // also handles file streaming
#include <string> // lets us use string
#include <set>
#include <list>
#include "graph.h"

using namespace std;
using namespace main_savitch_15;

void tokenize (const string& s, string& first, string& second)
{
    stringstream ss (s);
    ss >> first;
    ss >> second;
}
//postcondition:

size_t get_index (const graph<string>& g, const string& target)
{
    size_t i = 0;
    while (i < g.size() && g[i] != target)
        ++i;
    assert (i < g.size());
    return i;
}
//postcondition: index of the graph is returned


graph<string>* create_graph (const string& file_name)
{
    graph<string>* g = new graph<string>;
    ifstream input(file_name.c_str());
    size_t num;
    input >> num;
    string temp;
    getline (input, temp);
    assert (num <= g -> MAXIMUM);
    string* names = new string[num];
    for (size_t i = 0; i < num; ++i)
    {
        string s;
        getline (input, s);
        names[i] = s;
        g -> add_vertex (s);
    }
    string line;
    while (getline (input, line))
    {
        string first, second;
        tokenize (line, first, second);
        g -> add_edge (get_index (*g, first), get_index (*g, second));
    }
    input.close();
    return g;
}
//postcondition: graph is created

set<string> friends (const graph<string>& network, const string& name)
{
    set<size_t> f =  network.neighbors(get_index(network, name));
    set<string> friendzone;

    for(auto it: f)
        friendzone.insert(network[it]);

    return friendzone;
}
//postcondition: list all the friends of a specified person, has been     displayed.

set<string> common_friends (const graph<string>& network, const string&    name1, const string& name2)
{
    set<string> f1 = friends(network, name1);
    set<string> f2 = friends(network, name2);
    set<string> f3;

    for(auto it: f1)
    {
        for(auto itt: f2)
        {
            if(it == itt)
            {
                f3.insert(it);
            }
        }
    }

    return f3;
}
//postcondition: list all the friends that 2 specified people have in common, has been displayed

set<string> friends_of_friends ( const graph<string>& network, const     string& my_name)
{
    set<string> mineFriends = friends(network, my_name);
    set<size_t> fof[mineFriends.size()];
    set<string> return_another_friend;
    for(size_t go_i = 0; go_i < mineFriends.size(); ++go_i)
    {
        fof[go_i] = network.neighbors(get_index(network, my_name));
    }
    for(size_t index = 0; index <  mineFriends.size(); ++index)
    {
        for(auto it: fof[index])
        {
           return_another_friend.insert(network[it]);
        }
    }

    return return_another_friend;
}
//postcondition: list all the friends of the friends of a specified person, has been displayed

 void add_friend (graph<string>& network, const string& my_name, const string& new_friend)
 {
      network.add_edge(get_index(network, my_name), get_index(network, new_friend));
 }
    //postcondition: friend has been added

void remove_friend (graph<string>& network, const string& my_name, const string& ex_friend)
{
    network.remove_edge(get_index(network, my_name), get_index(network, ex_friend));
}
//postcondition: friend has been removed

bool is_friend (const graph<string>& network, const string& my_name, const string& maybe_friend)
{
    set<string> checkfriend= friends(network, my_name);

    for(auto it: checkfriend)
    {
        if(it == maybe_friend)
        {
            return true;
        }
    }
    return false;
}
//postcondition: the function will check to see if someone is a friend



int main()
{
    /*graph<string> DeezGraph = *(create_graph("friends.txt"));
    for(int g = 0; g < 10; ++g)
    {
        cout<< DeezGraph[g] << endl;
    }*/

    std::ifstream f("friends.txt");

    //Opens .txt file
    ifstream fr;
    fr.open("friends.txt");

    //Fail check
    if(fr.fail())
    {
        cout << "File failed to open.\n";
        exit(1);
    }

    //Prints file to screen (not correctly)
    cout << fr.rdbuf();

    //Closes file
    fr.close();
    return 0;
}

0 个答案:

没有答案