无法使用stringstream C ++将char转换为字符串

时间:2018-10-06 10:17:23

标签: c++ string char sstream

我只是要警告您所有我的代码在这里和那里都包含少量法语和英语,因此,如果您不理解变量或其他内容,请提出并将其视为免费的法语课程哈哈。好的,所以我有一个项目,其中包括创建时间表供学校使用,我负责管理“时间”部分,即为老师开设课时和空隙。为了确保所有情况都能正常工作,我需要确保不能同时发生两个类。在我的函数“ rajoutcours”(法文为“ AddsClass”)中,我输入了几个参数:

  • matiere =主题
  • heure_deb =开始时间
  • heure_fin =结束时间
  • date =好,这是课程的日期
  • 房间=房间。

在我的函数中,我创建了3个变量,分别是heure_start(另一个类开始的时间),heure_end(另一个类结束的时间),day和room(其他类发生的日期以及发生它们的房间) )。我使用字符串运算符“ +”属性填充这些变量,在该属性中,我将txt文件(其他类)中的行的每个字符转换为1个字母字符串,以便可以使用sstream库将它们加起来。奇怪的是,只有一个字符转换为字符串,而其他字符却没有,我真的不知道为什么。

无论如何我都知道很多,但是我已经试图找出几天了,但是我不知道问题出在哪里。谢谢大家的帮助。

    void  Utilisateur::rajout_cours(string matiere, string heure_deb, string heure_fin, string date, string salle )
{
    ifstream user_in ("Cours.txt"); // txt file where classes are listed
    ofstream user_out;
    user_out.open("Cours.txt", ofstream::out | ofstream::app);
    user_out; //write
    string ligne; //line variable which reads the lines of the txt file
    string heure_start; // time at which the class starts
    string heure_end; // time at which the class ends 
    string day; // self explanatory
    string room; // ie before
    int i=0;
    int j=0;
    int cptr=0;
    int a,b,c,d;
    stringstream ss;  // this is supposed to convert char into string
    while (getline(user_in,ligne))
    {
        for(i=0;i<ligne.size();i++)
        {
            if(ligne.at(i)=='$' && cptr==0)
            {
                a=i;
                cptr++;
            cout << "Premier '$'" << endl;  // Keep in mind that the '$' is the delimiter when i write on the txt file, it's also a way to know where I am. 
                for(j=0;j<a;j++)
                {
                    char tmpc='\0';
                    string tmps;
                    tmpc=ligne.at(j);
                    ss << tmpc;
                    cout << "Lettre a la case " << j << " : "<< tmpc << endl; //I want to know what's the char in the j-th character of the word
                    ss >>tmps;
                    cout << "Lettre string a la case " << j << " : "<< tmps << endl; // I want to display it's string version
                    heure_start=heure_start+tmps;

                }
            }

           else if(ligne.at(i)=='$' && cptr==1)
            {
                b=i;
                cptr++;
            cout << "Deuxieme '$'" << endl;

                for(j=a+1;j<(b);j++)
                {
                    char tmpc = '\0';
                    string tmps;
                    tmpc=ligne.at(j);
                    ss << tmpc;// conversion char en string
                    cout << "Lettre char a la case " << j << " : "<< tmpc << endl; //I want to know what's the char in the j-th character of the word
                    ss >>tmps;// conversion complète à priori
                    cout << "Lettre string a la case " << j << " : "<< tmps << endl; // I want to display it's string version
                    heure_end=heure_end+tmps;

                }
            }
           else if(ligne.at(i)=='$' && cptr==2)
            {
                c=i;
                cptr++;
            cout << "3eme '$'" << endl;

                for(j=b+1;j<(c);j++)
                {
                    char tmpc='\0';
                    string tmps="";
                    tmpc=ligne.at(j);
                    ss << tmpc;
                    cout << "Lettre a la case " << j << " : "<< tmpc << endl; //I want to know what's the char in the j-th character of the word
                    ss >>tmps;
                    cout << "Lettre string a la case " << j << " : "<< tmps << endl; // I want to display it's string version

                   room=room+tmps;

                }
            }
           else if(ligne.at(i)=='$' && cptr==3)
            {
                d=i;
                cptr++;
            cout << "4eme '$'" << endl;

                for(j=c+1;j<(d);j++)
                {
                    char tmpc='\0';
                    string tmps="";
                    tmpc=ligne.at(j);
                    ss << tmpc;
                    cout << "Lettre char a la case " << j << " : "<< tmpc << endl; //I want to know what's the char in the j-th character of the word
                    ss >>tmps;
                    cout << "Lettre string a la case " << j << " : "<< tmps << endl; // I want to display it's string version

                   day=day+tmps;

                }

            }

        }
    }



    if(heure_deb==heure_start && heure_fin==heure_end && date==day && salle==room) // I make sure here that the class I'm writing isn't already written in the file and in that case we leave the program.
    {
        cout << "Impossible d'ajouter un cours ! Un cours de " << matiere <<"a deja lieu à ce moment! Changez d'horaires ou de salles. " << endl;
        exit(1);
    }

        cout <<"ecris" << endl;
        user_out << heure_deb << "$"<< heure_fin << "$" << salle  << "$" << date << "$" << matiere << endl; // If not, write the new class. 



}

1 个答案:

答案 0 :(得分:0)

我已经提到过,只需使用resp将char转换为std::string非常简单。构造函数std::string::string(size_t count, char ch)

 char c = 'A';
 std::string str(1, c); // constructs a string str with one character

但是,当我第三次阅读问题后,我意识到它实际上应该按照OP尝试的方式工作。

所以,对我来说,实际的问题是:

为什么std::stringstream不能重复用于交替的输出/输入?

过了一会儿,我想起了stringstream中的“流”。

好吧,它可以–考虑以下事实:在将其填充输出(例如<<)并将其清空以输入(例如>>)之后,内部状态变为eof,在下一次读取时将进入fail状态。因此,(尽管我仍然建议使用上述简单方法),

  • 在重用之前重置std::stringstream的状态(std::stringstream::clear()很好。)
  • 或仅使用std::stringstream的本地实例(通过将字符串流的结构和输入/输出语句括在一对大括号{ }中)

一个小例子:

#include <iostream>
#include <sstream>
#include <string>

int main()
{
  { // "recycle" one instance of std::stringstream
    std::stringstream sstr;
    for (char c = 'A'; c <= 'C'; ++c) {
      std::cout << "c: '" << c << "', ";
      sstr << c;
      std::string str;
      sstr >> str;
      std::cout << "str: '" << str << "', "
        << "sstr.good(): " << sstr.good() << ", "
        << "sstr.eof(): " << sstr.eof() << '\n';
      sstr.clear();
    }
  }
  { // one instance per I/O
    for (char c = 'A'; c <= 'C'; ++c) {
      std::cout << "c: '" << c << "', ";
      std::stringstream sstr;
      sstr << c;
      std::string str;
      sstr >> str;
      std::cout << "str: '" << str << "'\n";
    }
  }
  // done
  return 0;
}

输出:

c: 'A', str: 'A', sstr.good(): 0, sstr.eof(): 1
c: 'B', str: 'B', sstr.good(): 0, sstr.eof(): 1
c: 'C', str: 'C', sstr.good(): 0, sstr.eof(): 1
c: 'A', str: 'A'
c: 'B', str: 'B'
c: 'C', str: 'C'

Live Demo on coliru


三思后,我意识到在这种特定情况下有必要清除eof状态还有另一个原因:

  • 输出恰好是一个char
  • 输入试图填充std::string(某种程度上更贪婪),并读取直到定界符或文件结束(并且总是后者发生)。

检查了此内容

#include <iostream>
#include <sstream>

int main()
{
  // "recycle" one instance of std::stringstream
  std::stringstream sstr;
  for (char c = 'A'; c <= 'C'; ++c) {
    std::cout << "c: '" << c << "', ";
    sstr << c;
    char cIO;
    if (sstr >> cIO) std::cout << "cIO: '" << cIO << "'\n";
    else std::cout << "reading cIO failed\n";
  }
  // done
  return 0;
}

输出:

c: 'A', cIO: 'A'
c: 'B', cIO: 'B'
c: 'C', cIO: 'C'

Live Demo on coliru

现在,它甚至在没有sstr.clear()的情况下也可以工作,因为它从未尝试读取文件末尾(aka字符串流)。

提到operator>>(std::stringstream&, std::string)的“贪婪”阅读时,我想到了另一个主意:

如果任务中的char是分隔符怎么办?

#include <iostream>
#include <sstream>
#include <string>

int main()
{
  char c = ' ';
  std::stringstream sstr;
  sstr << c;
  std::cout << "c: '" << c << "', ";
  std::string str;
  sstr >> str;
  std::cout << "str: '" << str << "'\n";
  std::cout
    << "sstr.good(): " << sstr.good() << '\n'
    << "sstr.eof(): " << sstr.eof() << '\n'
    << "sstr.fail(): " << sstr.fail() << '\n'
    << "sstr.bad(): " << sstr.bad() << '\n';
  // done
  return 0;
}

输出:

c: ' ', str: ''
sstr.good(): 0
sstr.eof(): 1
sstr.fail(): 1
sstr.bad(): 0

Live Demo on coliru

我必须承认(因为我从未尝试过使用charstd::string转换为std::stringstream),我不知道std::stringstream是一个糟糕的选择。

相关问题