gpt4 book ai didi

c++ - 在使用 get line() 之前如何使用 ignore()、sync() 或 clear() 清除任何数据?

转载 作者:行者123 更新时间:2023-11-28 02:28:00 25 4
gpt4 key购买 nike

我正在使用一个函数从规范化文件中读取字符串,以便将每个字符串推送到使用链表动态实现的堆栈的顶部。

规范化文件使用的数据如下:

racecar
rotator
rotor
civic

当我调用我的函数时,我希望我的输出在它使用 get 行将每个字符串插入堆栈后看起来相同,但是,实际输出是以一种看起来像的方式删除第一个字符串中的第一个字符这个:

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

最佳答案

您的代码几乎是正确的,除了几个异常(exception)。

  1. 你不应该测试 eof作为循环条件,请参见 Why is iostream::eof inside a loop condition considered wrong?

  2. 声明 DynStrStk stringStk;循环内部看起来很可疑,因为在循环退出时变量不复存在。

这是一个非常简单的程序,它使用 std::stack<std::string>相反,哪个有效,所以问题可能出在你的 DynStrStk执行。无需使用 cin.clearcin.ignore只要你不搞混cingetline (在后一种情况下 cin 在缓冲区中留下一个新行,如果您不清除流,它可能最终被“getline”“吃掉”)。

#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);广告 parseFile()和之前 createStack() .如果它不起作用,则尝试在独立函数中移动尽可能多的代码并逐步测试每个函数,或者使用调试器并实时查看发生了什么。

关于c++ - 在使用 get line() 之前如何使用 ignore()、sync() 或 clear() 清除任何数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29870125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com