从文件读取时,getline()无限循环
getline() infinite loop while reading from file
我试图从文件1中提取文本,并通过用随机种子填充空格来将内容调整为文件2。
一切似乎都正常,但我无法到达输入文件的末尾。程序在读取某一行时陷入循环。
用于读取的文件1
https://pastebin.com/raw/rRhcz3Tw
粘贴文件1后的文件2
https://pastebin.com/raw/uRrJVdy3
我读过关于flags的问题,我想知道是不是这样?
我的代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
unsigned int plusLongueChaine(ifstream& file);
void ajoutEspace(string& ligne, const unsigned int maxChaine);
int main()
{
srand(time(0));
// Création des instances de lecture et écriture
ifstream fin("ip.txt", ios::in);
ofstream fout("ip2.txt", ios::out);
// Si le fichier n'est pas trouvé
if (!fin.is_open()) {
cerr << "Ouverture du fichier impossible." << endl;
return 1; // Prototype dans cstdlib
}
// 1 - Trouver la plus longue ligne dans le fichier
const unsigned int maxChaine = plusLongueChaine(fin);
string ligneAEcrire;
fin.clear();
fin.seekg(0, ios::beg);
// 2 - Boucle principale
while (getline(fin, ligneAEcrire)) {
if (ligneAEcrire.empty()) {
fout << "n" << endl;
continue;
}
if (ligneAEcrire.size() < maxChaine)
ajoutEspace(ligneAEcrire, maxChaine);
fout << ligneAEcrire << endl;
}
cout << maxChaine; // 84
return 0;
}
void ajoutEspace(string& ligne, const unsigned int maxChaine) {
unsigned int position = 0;
while (ligne.size() < maxChaine) {
position = ligne.find(' ', position);
if (position < ligne.size() && position != string::npos) {
if (rand() & 1)
ligne.insert(position, "_");
position = ligne.find_first_not_of(' ', position);
cout << position << endl;
}
else {
position = 0;
}
}
}
unsigned int plusLongueChaine(ifstream& file) {
string ligne;
unsigned int longueurChaine = 0;
while (getline(file, ligne)) {
if (ligne.size() > longueurChaine)
longueurChaine = ligne.size();
}
return longueurChaine;
}
您的问题出现在void ajoutEspace(string& ligne)
中,它进入了一个无限循环。您不需要将maxChaine
作为参数传递。您正在传递对std::string
行的引用。您所要做的就是在行中找到' '
,并以不同的方式转换为'_'
。只需使用position = ligne.find(' ', position)
,然后在下一次调用之前更新position += 1;
(如果空间被'_'
随机替换,则更新另一个+1
(,例如
void ajoutEspace(string& ligne)
{
size_t position = 0;
while ((position = ligne.find(' ', position)) != std::string::npos) {
if (rand() & 1) {
ligne.insert(position, "_");
position += 1;
}
position += 1;
}
}
(注意:从unsigned int
更改为size_t
。当您进行比较时,您应该收到关于您的测试始终为true
的警告,例如position != string::npos
,因为与size_t
相比,unsigned int
的范围受到限制(
您正在使用find
,因此根据find()
的返回来控制循环,例如
while ((position = ligne.find(' ', position)) != std::string::npos) {
这样可以确保在第一次找不到空间时打破循环。
示例输出文件
修改文件的前十行:
$ head ip2.txt
1. Introduction
1.1. Motivation
Le Protocole_ Internet est conçu pour_ supporter l'intercommunication_ de systèmes_
informatiques sur une base_ de_ réseau_ par_ commutation de paquets. Un_ tel_ système_ est
appelé "catenet"_ [1]. Le rôle du protocole_ Internet est_ la_ transmission de blocs de
données,_ appelés_ datagrammes,_ d'une_ source_ vers une destination, la_ source_ et la
(注意:您的行fout << "n" << endl;
为原始文件中的每一空行插入两个新行(
做出更改,如果您还有其他问题,请告诉我。
相关文章:
- 过载'operator new'如何导致无限循环?
- C++正则表达式无限循环
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 遍历链表时的无限循环
- 循环链表:无限循环
- 比较两个字符串后卡在无限循环中
- 在做一段时间内检查字符的无限循环
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 可能的无限循环
- 超出 int 的值范围后的无限循环
- 为什么这段代码会导致无限循环?
- 运行无限循环的最小二叉树问题
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- 连接线程时的无限循环
- Getline 在将文件重定向到标准输入时会导致无限循环
- Visual Studio 2010 中'getline'上的无限循环
- 如何解决由文件末尾的 getline 引起的无限循环 (C++)
- 当check为eof时,使用getline读取文本文件会导致无限循环
- c++ getline()在文件行中读取,无限循环遍历文件