Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::getline将字符串“混合”在一起_C++ - Fatal编程技术网

C++ std::getline将字符串“混合”在一起

C++ std::getline将字符串“混合”在一起,c++,C++,我试图从一个.txt文件中读取数据,并将其内容放入一个链表中,该链表每个节点包含两个字符串。因为.txt文件中的一些字符串包含空格,我宁愿让它们保持原样也不愿删除空格,所以我使用std::getline 我将.txt文件格式化为: 旅游、娱乐和体育管理 “TRS” 人类学 “蚂蚁” 等,但每条线之间没有空行。链表有一种打印方法,它以这种格式打印数据:data1/data2;数据1/数据2: 因此,当我使用data1==旅游、娱乐和体育管理和data2==TRS打印节点时,所需的输出是: 旅游、娱

我试图从一个.txt文件中读取数据,并将其内容放入一个链表中,该链表每个节点包含两个字符串。因为.txt文件中的一些字符串包含空格,我宁愿让它们保持原样也不愿删除空格,所以我使用std::getline

我将.txt文件格式化为:

旅游、娱乐和体育管理

“TRS”

人类学

“蚂蚁”

等,但每条线之间没有空行。链表有一种打印方法,它以这种格式打印数据:data1/data2;数据1/数据2:

因此,当我使用data1==旅游、娱乐和体育管理和data2==TRS打印节点时,所需的输出是:

旅游、娱乐和体育管理/TRS

然而,我实际得到的是:

旅游、娱乐和体育管理

但是,当我读取这些行并将它们指定给字符串,然后打印这些字符串而不将它们插入链表时,我得到了正确的输出。就是

std::cout << data1 << std::endl;
std::cout << data2 << std::endl;
将正确输出:

旅游、娱乐和体育管理 TRS

有什么好处

编辑: 链表标题:

#ifndef _2DLL_H_
#define _2DLL_H_
#include <iostream>
#include <string>



class Llist{
  struct node{
    //node member var
    std::string department;
    std::string abv;
    node * next;

    //node member methods

    std::string search(std::string dep);
    void print();
    void remove(const std::string dep);
    void clear();
    //constructer
  node(const std::string dep , const std::string a):department(dep), abv(a), next(NULL){}

  };// end node

 public:
  //Llist member var
  node * head;

  //LList constructer & destructor
 Llist():head(NULL){}
  ~Llist(){clear();}

  //Llist member functions
  std::string search(const std::string dep);
  void insert(const std::string dep , const std::string a);
  void print();
  void remove(const std::string dep);
  void clear();
  const int operator[](unsigned int index)const;
};// end Llist


#endif //_2DLL_H_
链表

#include "2DLL.h"
#include <algorithm>


//=========INSERT==============

void Llist::insert(const std::string dep, const std::string a){ //will just prepend. Fuck the police;
  node * n = new node(dep , a);
  n->next = head;
  head = n;
}

//========PRINT=================
void Llist::print(){
  if(head==NULL){
    std::cout << "ERROR: List is empty" << std::endl;
  }
  else{
    head->print();
  }
}

void Llist::node::print(){
  if(next==NULL){
    std::cout << department << ";" << abv << std::endl;
  }
  else{
    std::cout << department << ";" << abv << " / " ;
    std::cout << std::endl;
    next->print();
  }
}

//=======REMOVE========

void Llist::remove(const std::string dep){
  if(head==NULL){
    std::cout << "ERROR: List is empty" << std::endl;
  }
  else{
    head->remove(dep);
  }
}

void Llist::node::remove(const std::string dep){
  if(next->department == dep){
    node * n = next;
    next = n->next;
    delete n;
  }
  else{
    next->remove(dep);
  }
}

//===========CLEAR()==================

void Llist::clear(){
  if(head==NULL){
    std::cout << "ERROR:List is empty" << std::endl;
  }
  else{
    head->clear();
    head = NULL;
  }
}

void Llist::node::clear(){
  if( this==NULL){
    return;    

  }
  else{
    next->clear();
    delete this;
  }

}

//=========OPERATOR=============
/*
const int Llist:: operator[] (unsigned int index) const{
  node * n = head;
  for(int i = 0 ; i < index && n!=NULL ; ++i){
    n=n->next;
  }
  return n->data;
}

*/

//========SEARCH====================

std::string Llist::search(std::string dep){
  if(head == NULL){
    std::cout << "ERROR: List is empty" << std::endl;
    return "ERROR";
  }
  else{
    //dep.erase(std::remove(dep.begin(),dep.end(),' '),dep.end());
    //std::cout << dep << std::endl;
    return head->search(dep);
  }
}

std::string Llist::node::search(std::string dep){
  if(department == dep){
      return abv;
  }
  else{
      return next->search(dep);
    }
  }
阅读教学的实施

#include "genCollege.cpp"
#include "genDepartment.cpp"
#include "2DLL.cpp"
#include <ctime>
#include <fstream>

using namespace std;


int main(){
    std:: ifstream file;
    file.open("DepList.txt");
    std::string department;
    std::string abv;

    srand(time(0));
    /*for (int  i = 0 ; i < 30 ; i++){
        std::string college = genCollege();
        std::string department = genDepartment(college);

        std::cout << "College: "<< college << std::endl;
        std::cout << "\t" << "Department: " << department << std::endl;
        std::cout << std::endl;
    }   */
    Llist list;

    while(file.is_open()){
        if(file.eof()){break;};

        std::getline(file , department);
        std::getline(file, abv);

        list.insert(department , abv);

    }
    //file.close();
    list.print();



    return 0 ; 
}

正如用户n.m建议的那样,似乎因为我在读Windows的文本文件并在Ubuntu上运行程序,所以输出看起来是错误的。他逐字逐句地回答:


您有一个为Windows创建的文本文件,\r\n作为行终止符。您的程序在un*x上运行,或者无法以文本模式打开文件。因此,在每个字符串的末尾都会出现\r,这会弄乱终端窗口

他建议我检查使用std::getline后字符串中的最后一个字符是否为\r,如果是,则将其从字符串中删除。在使用std::getline获取相关字符串后,我只需生成这些字符串的子字符串即可


然后我将新的子字符串插入到链表中,打印方法现在可以根据需要输出。

一篇5页的文章格式不好,没有代码?给出了什么?听起来您的节点没有正确地为每个字符串分配独立的存储。节点是否包含std::string?字符[]?char*?我想这可能只是一般使用getline方法的一个怪癖,我不知道。您希望看到什么代码?您只需询问:您有一个为Windows创建的文本文件,\r\n作为行终止符。您的程序在un*x上运行,或者无法以文本模式打开文件。因此,在每个字符串的末尾都会出现\r,这会弄乱终端窗口。我们希望看到的代码是调用std::getline读取数据1和数据2的部分。我们还想了解这两个变量的定义。