代码将只接受一个用户输入文件,而不是其他的
Code will only accept one user input file instead of the other ones
我使用Visual studio在c++中编码,我的代码只接受一个名为"perm15K.txt"的文件。如果我尝试输入"perm30K.txt"或"sorted15K.txt",我的代码将无法从中读取。它不会输出错误文件,但它不会让我输入我想要执行的搜索。
#include "stdafx.h"
#include "binarysearchtree.h"
#include "redblacktree.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <ctime>
#include <chrono>
using namespace std;
int main()
{
struct bstnodes *root = NULL;
std::ifstream in;
std::ofstream out;
std::stringstream buffer;
buffer << in.rdbuf();
std::string test = buffer.str();
std::cout << test << std::endl << std::endl;
ifstream myFile;
string input;
string output;
cout << "Name of the input file? (ie. perm15K.txt)";
getline(cin, input);
myFile.open(input.c_str());
if (myFile.fail())
{
cout << "Error with filen";
}
for (int j = 0; j < 10; j++)
{
cout << "Which search? Pleast enter bst or redblackn";
binarysearchtree temp;
redblacktree temp1;
while (!myFile.eof())
{
while (getline(myFile, input)) {
myFile >> input;
string words = input;
temp.insert(words);
temp1.rbinsert(words);
}
}
getline(cin, input);
if (input == "bst")
{
cout << "nSearch for what word in the treen";
getline(cin, input);
temp.insert(input);
clock_t start_s = clock();
std::cout << "Match Found: " << temp.search(input) << std::endl;
clock_t stop_s = clock();
double sum = ((double)(stop_s - start_s));
cout << endl << "Time: " << sum << " seconds" << endl;
cout << "nSearch for what word in the treen";
getline(cin, input);
}
if (input == "redblack")
{
cout << "nSearch for what word in the treen";
getline(cin, input);
temp1.rbinsert(input);
clock_t start_s = clock();
temp1.rbsearch(input);
std::cout << "Match Found: ";
clock_t stop_s = clock();
double sum = ((double)(stop_s - start_s));
cout << endl << "Time: " << sum << " seconds" << endl;
cout << "nSearch for what word in the treen";
getline(cin, input);
}
myFile.close();
return 0;
}
}
任何帮助解决我的问题,我都非常感激。就像我被困在一个无止境的for循环中。这个问题我已经找了几个小时了,但还是找不到解决办法。
可能,如果文件打开失败,!myFile.eof()
永远不会为false,因为myFile.getline()
的行为不像预期的那样(无限循环)。
如果打开失败,应该在错误消息后返回。
另外,你应该只做while(myFile.getline())
,而不是检查eof。
试着找到Visual Studio存放可执行文件的位置,看看其他文本文件是否在那里。
你的print语句也应该是count <<"文本"& lt; & lt;endl;如果以"n"结尾,可能实际上并没有刷新打印缓冲区,这可能会导致丢失错误输出。
相关文章:
- C++文本文件输入
- C++ 中文件输入输出中的长长 v/s int
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- 用空格填充 2D 数组(从文件输入)
- 流对象来表示文件输入,然后是标准输入?
- 用精神解析文件输入
- 从文件输入和输出
- 来自文本文件输入 c++ 的向量
- 如何通过键盘或分数文件输入分数
- 了解文件输入和输出,我可以写入文件,但无法从文件打印
- 词频程序 - 文件输入太大?
- 尝试从 csv 文件输入大量数据时出错
- 如何将文件从文件输入到C 中的矩阵中
- 如何从Windows CMD运行可执行文件并将其传递给文件输入的参数参数和重定向
- 如何解析文本文件并使用构造函数中的文件输入来创建对象的容器
- 这是读取文件输入的正确方法吗?
- 如何在 c++ 中使用二进制文件输入/输出读取/写入结构的字符串类型成员
- 如何创建一个具有空值、递减编号系统和.txt文件输入值的二维阵列网格
- C 文件输入流循环问题
- 使用对输入整数的文件输入