为散列程序读取带有分隔符和空行的文件
Reading in a file with delimiter and blank lines for hashing program
如何从文件中读取行并将该行的特定段分配给结构体中的信息?我如何在空白行处停止,然后继续,直到到达文件末尾?
背景:我正在构建一个程序,它将接收一个输入文件,读入信息,并使用双哈希法将该信息放入哈希表的正确索引中。
假设我有一个结构体:
struct Data
{
string city;
string state;
string zipCode;
};
但是文件中的行格式如下:
20
85086,Phoenix,Arizona
56065,Minneapolis,Minnesota
85281
56065
我似乎弄不明白这个。我真的很难读懂这份文件。第一行基本上是要构造的哈希表的大小。下一个空行应该被忽略。接下来的两行是应该进入结构体并被散列到散列表中的信息。那么另一个空行应该被忽略。最后,最后两行是需要匹配的输入,以查看它们是否存在于哈希表中。所以在这种情况下,没有找到85281。而56065被找到。这是我所拥有的,它似乎没有做我想要它做的事情:
int main(int argc, char *argv[])
{
string str;
//first line of file is size of hashtable
getline(cin, str);
stringstream ss(str);
int hashSize;
ss >> hashSize;
//construct hash table
Location *hashTable = new Location[hashSize];
//skip next line
getline(cin, str);
string blank = " ";
while(getline(cin, str))
{
{
//next lines are data
Location locate;
string line;
getline(cin, line);
istringstream is(line);
getline(is, locate.zipCode, ',');
getline(is, locate.city, ',');
getline(is, locate.state, ',');
insertElementIntoHash(hashTable, locate, hashSize);
}
}
dispHashTable(hashTable, hashSize);
//read third set of lines that check if the zipCodes are in the hashtable or not
while(getline(cin, str))
{
//stop reading at a blank line or in this case, end of file
stringstream is(str);
string searchZipCode;
is >> searchZipCode;
searchElementInHash(hashTable, hashSize, searchZipCode);
}
//delete hash table after use
delete []hashTable;
return 0;
}
您可以这样读取输入:
#include <iostream>
#include <sstream>
#include <vector>
struct Location
{
std::string city;
std::string state;
std::string zipCode;
};
int main(int argc, char *argv[]) {
std::istringstream input(
"2n"
"n"
"85086,Phoenix,Arizonan"
"56065,Minneapolis,Minnesotan"
"n"
"85281n"
"56065n"
);
// Make the size unsigned, to avoid signed/unsigned compare warnings.
unsigned hashSize;
std::string line;
getline(input, line);
std::istringstream hash_line(line);
// Ignore white space.
if( ! (hash_line >> hashSize >> std::ws && hash_line.eof())) {
std::cerr << "Error: Invalid file format [1].n" << line << 'n';
return -1;
}
else {
getline(input, line);
std::istringstream first_blank_line(line);
// Ignore white space.
first_blank_line >> std::ws;
if( ! first_blank_line.eof()) {
// Missing blank line.
std::cerr << "Error: Invalid file format [2].n" << line << 'n';
return -2;
}
else {
// Have a local variable (No need to allocate it)
// (Is it a hash table !???)
std::vector<Location> hashTable;
hashTable.reserve(hashSize);
while(hashTable.size() < hashSize && getline(input, line)) {
std::istringstream data_line(line);
Location locate;
getline(data_line, locate.zipCode, ',');
getline(data_line, locate.city, ',');
getline(data_line, locate.state); // Note: No comma here.
if(data_line && data_line.eof()) {
// Note: The fields may have leading and/or trailing white space.
std::cout
<< "Insert the location into the hash table.n"
<< locate.zipCode << 'n'
<< locate.city << 'n'
<< locate.state << 'n';
hashTable.push_back(locate);
}
else {
std::cerr << "Error: Invalid file format [3].n" << line << 'n';
return -3;
}
}
if(hashTable.size() != hashSize) {
std::cerr << "Error: Invalid file format [4].n";
return -4;
}
else {
getline(input, line);
std::istringstream second_blank_line(line);
// Ignore white space.
second_blank_line >> std::ws;
if( ! second_blank_line.eof()) {
// Missing blank line.
std::cerr << "Error: Invalid file format [5].n";
return -5;
}
else {
std::string searchZipCode;
while(input >> searchZipCode) {
// Search element in the hash table
}
}
}
}
}
return 0;
}
以下修改应该可以工作:
//skip next line
getline(cin, str);
string blank = " ";
string line;
while(getline(cin, line) && (line != ""))
{
{
//next lines are data
Location locate;
istringstream is(line);
getline(is, locate.zipCode, ',');
getline(is, locate.city, ',');
getline(is, locate.state, ',');
insertElementIntoHash(hashTable, locate, hashSize);
}
}
相关文章:
- C++使用分隔符读取文件
- 到达分隔符时跳到文本文件的下一行
- 使用空格分隔符从文本文件将对象读入数组
- 使用任意分隔符从文件流读取
- 使用 C++ 读取具有两个连续分隔符的 csv 文件
- 通过自定义分隔符从文件中读取对象数组
- 使用不同类型的分隔符从.txt文件中读取输入
- 如何在没有分隔符的大文本文件中查找所有字典单词
- 将 OpenCV MAT 保存到 TXT 文件作为不带分隔符的十六进制值
- 如何在C++中正确解析带有多个分隔符的文本文件
- 从没有分隔符 C++ 的文件读取数字出现次数
- 生成文件,缺少分隔符错误
- 生成文件错误:缺少分隔符
- 检测 CSV 文件中的记录/行分隔符
- C++ 使用多个分隔符存储输入文件
- 用指定的分隔符按线解析文本文件
- 如何获取标准 C/C++ 中的文件分隔符符号:/ 或 \
- 在C++中搜索二进制文件分隔符时出现不正确的偏移量
- C++具有混合分隔符和数据类型的文件输入
- 我的生成文件中缺少分隔符