在使用 get line() 之前如何使用 ignore()、sync() 或 clear() 清除任何数据?
how do i use ignore(), sync(), or clear() to clear out any data before using get line()?
我正在使用一个函数从规范化文件中读取字符串,以便将每个字符串推送到使用链表动态实现的堆栈的顶部。
规范化文件使用的数据如下:
racecar
rotator
rotor
civic
当我调用我的函数时,我希望我的输出在它使用 get line 将每个字符串推入堆栈后看起来相同,但是,实际输出是以一种方式擦除第一个字符串中的第一个字符看起来像这样:
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
你的代码几乎是正确的,除了几个例外。
您不应将 eof
作为循环条件进行测试,请参阅 Why is iostream::eof inside a loop condition considered wrong?
在循环内声明 DynStrStk stringStk;
似乎很可疑,因为在循环退出时变量不复存在。
这是一个非常简单的程序,它使用 std::stack<std::string>
代替,并且有效,所以问题可能出在您的 DynStrStk
实现中。没有必要使用 cin.clear
或 cin.ignore
只要你不混淆 cin
和 getline
(在后一种情况下 cin
留下一个新的如果您不清除流,缓冲区中的行可能会被 `getline" 结束 "eaten"。
#include <iostream>
#include <stack>
#include <string>
#include <fstream>
int main()
{
std::stack<std::string> stringStk; // stack of strings
std::ifstream normFile("test.txt"); // input file
std::ofstream output("out.txt"); // output file
std::string catchNewString;
while(getline(normFile,catchNewString)) // read and push to stack
{
stringStk.push(catchNewString); // push to stack
}
while(!stringStk.empty()) // dump the stack into the file
{
output << stringStk.top() << std::endl; // write it
stringStk.pop(); // remove
}
}
同样,getline
不会丢失任何内容,缓冲区中也不会丢失任何内容。在 void parseFile(ifstream &inFile, fstream &normFile)
中循环遍历字符串中的单个字符时可能会出现错误。还有一件事:你使用 parseFile(inFile, normFile);
然后立即 createStack(normFile, outFile);
。但是,normFile 将在末尾有它的位置(这是由 parseFile()
完成的),在调用 createStack()
之前不需要倒带它吗?
尝试 normFile.seekg(0, ios::beg);
adter parseFile()
和 createStack()
之前。如果它不起作用,则尝试在独立函数中移动尽可能多的代码并逐步测试每个函数,或者使用调试器并实时查看发生了什么。
我正在使用一个函数从规范化文件中读取字符串,以便将每个字符串推送到使用链表动态实现的堆栈的顶部。
规范化文件使用的数据如下:
racecar
rotator
rotor
civic
当我调用我的函数时,我希望我的输出在它使用 get line 将每个字符串推入堆栈后看起来相同,但是,实际输出是以一种方式擦除第一个字符串中的第一个字符看起来像这样:
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
你的代码几乎是正确的,除了几个例外。
您不应将
eof
作为循环条件进行测试,请参阅 Why is iostream::eof inside a loop condition considered wrong?在循环内声明
DynStrStk stringStk;
似乎很可疑,因为在循环退出时变量不复存在。
这是一个非常简单的程序,它使用 std::stack<std::string>
代替,并且有效,所以问题可能出在您的 DynStrStk
实现中。没有必要使用 cin.clear
或 cin.ignore
只要你不混淆 cin
和 getline
(在后一种情况下 cin
留下一个新的如果您不清除流,缓冲区中的行可能会被 `getline" 结束 "eaten"。
#include <iostream>
#include <stack>
#include <string>
#include <fstream>
int main()
{
std::stack<std::string> stringStk; // stack of strings
std::ifstream normFile("test.txt"); // input file
std::ofstream output("out.txt"); // output file
std::string catchNewString;
while(getline(normFile,catchNewString)) // read and push to stack
{
stringStk.push(catchNewString); // push to stack
}
while(!stringStk.empty()) // dump the stack into the file
{
output << stringStk.top() << std::endl; // write it
stringStk.pop(); // remove
}
}
同样,getline
不会丢失任何内容,缓冲区中也不会丢失任何内容。在 void parseFile(ifstream &inFile, fstream &normFile)
中循环遍历字符串中的单个字符时可能会出现错误。还有一件事:你使用 parseFile(inFile, normFile);
然后立即 createStack(normFile, outFile);
。但是,normFile 将在末尾有它的位置(这是由 parseFile()
完成的),在调用 createStack()
之前不需要倒带它吗?
尝试 normFile.seekg(0, ios::beg);
adter parseFile()
和 createStack()
之前。如果它不起作用,则尝试在独立函数中移动尽可能多的代码并逐步测试每个函数,或者使用调试器并实时查看发生了什么。