C++ 在使用get line()之前,如何使用ignore()、sync()或clear()清除任何数据?
我正在使用一个函数从规范化文件中读取字符串,以便将每个字符串推送到堆栈的顶部,该堆栈使用链表动态实现 规范化文件使用的数据如下:C++ 在使用get line()之前,如何使用ignore()、sync()或clear()清除任何数据?,c++,getline,C++,Getline,我正在使用一个函数从规范化文件中读取字符串,以便将每个字符串推送到堆栈的顶部,该堆栈使用链表动态实现 规范化文件使用的数据如下: racecar rotator rotor civic acecar rotator rotor civic 当我调用函数时,我希望我的输出在使用get line将每个字符串推送到堆栈后看起来是一样的,但是,实际的输出是以如下方式擦除第一个字符串中的第一个字符: racecar rotator rotor civic acecar rotator rotor c
racecar
rotator
rotor
civic
acecar
rotator
rotor
civic
当我调用函数时,我希望我的输出在使用get line将每个字符串推送到堆栈后看起来是一样的,但是,实际的输出是以如下方式擦除第一个字符串中的第一个字符:
racecar
rotator
rotor
civic
acecar
rotator
rotor
civic
在每次迭代get line()之前,我都尝试使用ignore()、sync()和clear函数,但我仍然不断遇到同样的问题
在这方面我需要一些真正的帮助,我希望你们中的一位代码大师能帮助我找到解决方案,因为我还没有能够让ignore()和其他代码正常工作!!!:/
下面是我的函数的代码:
void createStack(fstream &normFile, ostream &outFile)
{
string catchNewString;
do
{
DynStrStk stringStk;
getline(normFile,catchNewString,'\n');
stringStk.push(catchNewString);
//tracer rounds
cout << endl;
outFile << catchNewString << endl;
cout << "test: " << catchNewString << endl;
} while(!normFile.eof());
}
//
#include <iostream>
#include <fstream>
#include <ostream>
#include <sstream>
#include <string>
#include "DynStrStk.h"
using namespace std;
void processFile();
void parseFile(ifstream&, fstream&);
void createStack(fstream&, ostream&);
int main()
{
//call function to open file and process
cout << "processing file" << endl;
processFile();
return 0;
}
void processFile()
{
ifstream inFile;
fstream normFile;
ofstream outFile;
cout << "opening files" << endl;
// open files
inFile.open("inFile.txt");
normFile.open("normFile.txt");
outFile.open("outFile.txt");
cout << "parsing file" << endl;
//parse file for capitalization & punctuation
parseFile(inFile, normFile);
//create stack with parsed and normalized normFile
createStack(normFile, outFile);
//close files
outFile.close();
normFile.close();
inFile.close();
}
void parseFile(ifstream &inFile, fstream &normFile)
{
//create and initialize variables
string newString;;
int i;
if(!inFile)
{
cout << "ERROR!!! Cannot read file.";
}
else
{
do
{
//read each line in the input file until EOF
getline(inFile, newString, '\n');
i = 0;
//parse each string for punctuation
while(newString[i])
{
if(isalpha(newString[i])) //check each char in each
//string for punctuation
{
if(isupper(newString[i])) //check each string for
//capitalization
{
newString[i] = tolower(newString[i]); //convert
//string to lower case
}
normFile << newString[i]; //output each line tofile
cout << newString[i];
}
i++;
}
normFile << '\n';
cout << '\n';
} while(!inFile.eof());
}
}
void createStack(fstream &normFile, ostream &outFile)
{
string catchNewString;
do
{
DynStrStk stringStk;
getline(normFile,catchNewString,'\n');
stringStk.push(catchNewString);
//tracer rounds
cout << endl;
outFile << catchNewString << endl;
cout << "test: " << catchNewString << endl;
} while(!normFile.eof());
}
这是我的头文件:
//function that pushes the argument onto the list
void DynStrStk::push(string newString)
{
StackNode *newNode = nullptr; //Pointer to a node
//Allocate a new node and store string
newNode = new StackNode;
newNode->newString = newString;
//if there are no nodes in the list make newNode the first node
if(isEmpty())
{
top = newNode;
newNode->next = nullptr;
}
else //otherwise insert NewNode before top
{
newNode->next = top;
top = newNode;
}
}
#ifndef DynStrStk_h
#define DynStrStk_h
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class DynStrStk
{
private:
//struct stack nodes
struct StackNode
{
string newString; //string held in the node
StackNode *next; //pointer to the next node
};
StackNode *top; //pointer to the top of the stack
public:
//Constructor
DynStrStk()
{top = nullptr;}
//Destructor
~DynStrStk();
//Stack Operations
void push(string);
void pop(string &);
bool isEmpty();
};
#endif
\ifndef DynStrStk\h
#定义DynStrStk_h
#包括
#包括
#包括
使用名称空间std;
DynStrStk类
{
私人:
//结构堆栈节点
结构堆栈节点
{
string newString;//节点中保留的字符串
StackNode*next;//指向下一个节点的指针
};
StackNode*top;//指向堆栈顶部的指针
公众:
//建造师
DynStrStk()
{top=nullptr;}
//析构函数
~DynStrStk();
//堆栈操作
空推(串);
无效弹出(字符串&);
bool是空的();
};
#恩迪夫
除了几个例外,您的代码几乎是正确的
eof
作为循环条件进行测试,请参阅DynStrStk-stringStk循环内部的代码>看起来很可疑,因为在循环的出口处变量就不存在了
std::stack
,并且可以正常工作,因此问题可能出在DynStrStk
实现中。无需使用cin.clear
或cin.ignore
,只要您没有混淆cin
和getline
(在后一种情况下cin
会在缓冲区中留下一个新行,如果您不清除流,它可能会被`getline“吃掉”)
#包括
#包括
#包括
#包括
int main()
{
std::stack stringStk;//字符串堆栈
std::ifstream normFile(“test.txt”);//输入文件
std::of流输出(“out.txt”);//输出文件
std::string catchNewString;
while(getline(normFile,catchNewString))//读取并推送到堆栈
{
stringStk.push(catchNewString);//推到堆栈
}
而(!stringStk.empty())//将堆栈转储到文件中
{
输出端问题:while(!normFile.eof());
将其替换为while(getline(normFile,catchNewString,'\n')){/*循环的其余部分*/}
,否则您将“吃”“eof。同样不需要“\n
”,默认情况下,getline将一直读取到下一行。您想将您的评论作为答案发布,如果它有效,我将为您删除它?我不确定您的代码如何工作(例如,DynStrStk
中可能存在问题),这就是为什么我没有写一个答案,因为仅仅通过查看您的代码,我们无法确定问题出在哪里,因为没有足够的“数据”可用。所以我尝试了你的解决方案,但现在,它没有吃掉第一个单词的第一个字母,而是吃掉了整个第一个单词……对不起,但没有雪茄:/这与缓冲区已满有关,我知道这与我没有清除缓冲区有关。我只是一直在想如何在排队前清除缓冲区()你在while循环中声明DynStrStk stringStk是对的…我确实包含了你的编辑,但我得到了相同的结果。我继续在原始问题中添加了推送实现和头文件,供你审阅。谢谢!!没有恐慌的人…我已经测试了所有步骤,每一步都非常有效直到得到行…你能告诉我如何正确地清除缓冲区,这样我就可以先测试它。我从来都不知道如何使用ignore()正确地说…我回到我的原始代码,只是因为我只丢失了第一个字符串中的一个字符,而不是此时的整个输出…它必须在缓冲区中丢失。没有任何其他意义…is alpha循环用于创建normF文件…它工作得很好。我的问题是当我打开并读取normF时在createStack()中调用DynStrStk类中的push()的createStack()中的my get line()跳过文件第一个字符串中的第一个字符,这很可能是因为在createStack()中调用get line()时,缓冲区中仍有剩余的“\n”函数…我在编写代码后测试每一行代码…这就是我被卡住的地方,因为它没有通过测试…如果你能提供帮助,请告诉我。再次感谢,我非常感谢。我将按照你在上一次评论中所说的玩…谢谢again@lopezdp看看我发给你的链接,你就会知道发生了什么事e如下:您的文件指针指向EOF之前的字符,您读取它,指针移动到EOF(尚未读取),因此流的状态仍然正常。然后,在下一次迭代中,您读取它,它现在是EOF,因此您最终使用EOF。只有在读取EOF后,循环才会中断。