文件读取和矢量条目问题

时间:2016-10-04 19:31:28

标签: c++ vector readfile

该程序的目的是读取文本文件并将其内容存储在3个单独的向量中。

文本文件名为" InsultsSource.txt",包含50行制表符分隔的形容词列,如下所示:

public class Test {

private static void test1() {
        AutoDialer<T> dialer = new AutoDialer<T>();

        dialer.insert( new Person( "Mrs. X" ) );
        dialer.insert( new Person( "Mr. Y" ) );

        dialer.callAll();
    }

    private static void test2() {
        AutoDialer<AbstractConnection> dialer = new AutoDialer<AbstractConnection>();

        dialer.insert( new Mail( new Person( "Mrs. X" ) ) );
        dialer.insert( new Phone( new Person( "Mr. Y" ) ) );

        dialer.callAll();
    }

    public static void main( String[] args ) {
        test1();
        test2();
    }
}


 class AutoDialer <T extends AbstractConnection >{ 

    private Node<T> anchor;

    /*
     * insert an element at the first position
     */
    public void insert( T element ) {
        anchor = new Node<T>( element, anchor );
    }

    /*
     * call call() method on all elements contained in the list
     */
    public void callAll() {
        Node<T> iterator = anchor;

        while( iterator != null ) {
            iterator.getValue().call();
            iterator = iterator.getNext();
        }
    }
}
abstract class AbstractConnection implements Callable  {

    public final String type;
    private Person person;

    protected AbstractConnection( String type, Person person ) {
        this.type = type;
        this.person = person;
    }

    public Person getPerson() {
        return person;
    }

    protected abstract void connect();

    /*
     * establish a connection (implemented in subclass) and then call the person
     */
    public void call() {
        connect();
        getPerson().call();
    }
}
class Node <T> { 

    private T value;
    private Node<T> next;

    public Node( T value, Node<T> anchor ) {
        this.value = value;
        this.next = anchor;
    }

    public T getValue() { return value; }
    public Node<T> getNext() { return next; }
}

interface Callable {

    void call();
}
class Mail extends AbstractConnection {  

    public Mail( Person person ) {
        super( "Mail", person );
    }

    @Override
    public void connect() {
        System.out.println( "Establish Mail connection" );
    }
}
class Person implements Callable{  

    public final String name;

    protected Person( String name ) {
        this.name = name;
    }

    @Override
    public void call() {
        System.out.println( "Hello " + name );
    }
}
class Phone extends AbstractConnection {  

    public Phone( Person person ) {
        super( "Phone", person );
    }

    @Override
    public void connect() {
        System.out.println( "Start voice synthesizer" );
        System.out.println( "Establish phone connection" );
    }

以下是我用来实现此目的的代码。由于某种原因,一切都有效,直到第16行返回空白空间。我检查了文本文件,看看格式是否在那里发生了变化,但看起来很好。我只是想知道我的逻辑/代码中是否有任何导致此问题的错误。

happy    sad    angry
tired    mad    hungry

3 个答案:

答案 0 :(得分:1)

摆脱for循环,改为使用while

std::string text;
while (std::getline(fileIn, text, '\t'))
{
  col1.push_back(text);
  std::getline(fileIn, text, '\t');
  col2.push_back(text);
  std::getline(fileIn, text);
  col3.push_back(text);
}

这可能是您希望使用结构为每条线建模的情况。

struct Record
{
  std::string col1;
  std::string col2;
  std::string col3;
}
std::vector<Record> database;
Record r;
while (std::getline(fileIn, r.col1, '\t')
{
  std::getline(fileIn, r.col2, '\t');
  std::getline(fileIn, r.col3);
  database.push_back(r);
}

答案 1 :(得分:1)

您正在阅读50个单词 total,然后尝试从每列打印50个单词

答案 2 :(得分:1)

而是使用像

这样的东西
std::string val1, val2; val3;

vector<string> col1;
vector<string> col2;
vector<string> col3;

while(fileIn >> val1 >> val2 >> val3) {
    col1.push_back(val1);
    col2.push_back(val2);
    col3.push_back(val3);
}