检测文本文件中的空行
Detecting empty line from text file
我有这样的文本文件:
7
a
bkjb
c
dea
hash_table
是一个数组,使得每行line no.-2=index of hash_table array
对应于数组中的一个元素。该元素可以是空行或字符,如"an"
在文本文件中如下所示:
a
//empty line
第一个数字用于确定数组hash_table
的大小。运算符<<没有将空行或""字符视为字符串,因此不会添加到数组中。我试过这个但没有用.这是我的尝试:
ifstream codes ("d:\test3.txt"); //my text file
void create_table(int size, string hash_table[]) //creating array
{ string a;
for(int i=0;i<size;i=i+1)
{
codes>>a;
char c=codes.get();
if(codes.peek()=='n')
{char b=codes.peek();
a=a+string(1,b);
}
hash_table[i]=a;
a.clear();
}
}
void print(int size, string hash_table[])
{
for(int i=0;i<size;i=i+1)
{if(!hash_table[i].empty())
{cout<<"hash_table["<<i<<"]="<<hash_table[i]<<endl;}
}
}
int main()
{
int size;
codes>>size;
string hash_table[size];
create_table(size, hash_table);
print(size, hash_table);
}
注意:可以有任何随机序列的空行。
使用 std::getline()
而不是 std::ifstream::operator >>()
。>>
运算符将跳过空格,包括换行符。
std::string line;
while (std::getline(codes, line)) {
//...do something with line
}
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 已修改的LinkedList未在文本文件本身中更新
- C++文本文件的获取线
- 为什么C++的文件 I/O 在读取文本文件时忽略初始空行?我怎样才能让它不这样做?
- 打印逐行存储的文本文件
- C++文本文件输入