C++ Can';t使用stringstream C++; 我只是要警告大家,我的代码包含了少量的法语和英语,所以如果你不理解变量或某事,请一定要问,并把它看作是免费的法语课哈哈。好的,我有一个项目,包括为学校制定一个时间表,我负责管理“时间”部分,即为教授们上课创造时间间隔和空间。为了确保所有情况都能正常工作,我需要确保两门课不能同时在同一时间和地点上课。在我的函数“rajoutcours”中,法语的意思是“AddsClass”,我引入了几个参数: 主语 heure_deb=开始的时间 heure_fin=结束的时间 日期=嗯,这是上课的日期 salle=房间

C++ Can';t使用stringstream C++; 我只是要警告大家,我的代码包含了少量的法语和英语,所以如果你不理解变量或某事,请一定要问,并把它看作是免费的法语课哈哈。好的,我有一个项目,包括为学校制定一个时间表,我负责管理“时间”部分,即为教授们上课创造时间间隔和空间。为了确保所有情况都能正常工作,我需要确保两门课不能同时在同一时间和地点上课。在我的函数“rajoutcours”中,法语的意思是“AddsClass”,我引入了几个参数: 主语 heure_deb=开始的时间 heure_fin=结束的时间 日期=嗯,这是上课的日期 salle=房间,c++,string,char,sstream,C++,String,Char,Sstream,在我的函数中,我创建了3个变量,它们是heure_start(另一个类开始的时间)、heure_end(另一个类结束的时间)、day和room(其他类发生的日期以及它们发生的房间)。我使用字符串运算符“+”属性填充这些变量,其中我将txt文件(其他类)中的每行字符转换为1个字母的字符串,以便使用sstream库将它们相加。奇怪的是,只有一个字符被转换成字符串,而其他字符没有,我真的不明白为什么 不管怎么说,我知道很多,但是我已经试了好几天了,我不知道问题出在哪里。谢谢大家的帮助 void

在我的函数中,我创建了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. 



}
void usilizateur::rajout_cours(字符串matiere、字符串heure_deb、字符串heure_fin、字符串日期、字符串salle)
{
ifstream user_in(“Cours.txt”);//列出类的txt文件
流式用户输出;
用户_out.open(“Cours.txt”,of stream::out | of stream::app);
用户_out;//写入
string ligne;//读取txt文件行的line变量
string heure_start;//类开始的时间
字符串heure_end;//类结束的时间
string day;//不言自明
弦乐室;//以前
int i=0;
int j=0;
int-cptr=0;
INTA、b、c、d;
stringstream ss;//应该将字符转换为字符串
while(getline(用户输入,对齐))
{

对于(i=0;i我已经提到,通过简单地使用相应的构造函数,将
char
转换为
std::string
非常简单:

然而,在我第三次读到这个问题后,我意识到它实际上应该像OP尝试的那样工作

因此,对我来说,实际问题的结果是:

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

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

嗯,它可以–考虑到在用输出(例如,
)填充它之后,它的内部状态变为
eof
,下一次读取将导致
失败
状态。因此,(尽管我仍然建议使用上述更简单的方法)

  • 在重新使用之前,可以重置
    std::stringstream
    的状态(这样做很好)
  • 或者只使用
    std::stringstream
    的本地实例(将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;
}


经过再三考虑,我意识到在这种特定情况下需要清除
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;
}

现在,即使没有
sstr.clear()
,它也可以工作,因为它从不尝试读取超过文件结尾的内容(也称为stringstream)

提到对
操作符>>(std::stringstream&,std::string)
的“贪婪”阅读,我想到了另一个想法:

如果quest中的
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;
}


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

欢迎来到堆栈溢出。请阅读,获取,阅读,以及。最后学习如何创建。如前所述,请缩小您的代码以重现问题中的错误。这是被认为是一个问题,也是您在这里讨论主题时需要的。顺便说一句,这里有一个将
char
转换为
std::string
的非常简单的方法,因为后者有一个专门的构造函数:。由于
count
参数,您可能忽略了它。(我个人认为,引入它不仅是为了方便,也是为了消除众多构造函数的歧义。)然而,它是这样工作的:
charc='A';std::string str(1,c);
甚至
std::string str(1,'A');
。Et voilá-A
char
转换为
std::string
。向国际观众展示代码时,请使用英语标识符。
#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;
}