I/O程序卡在循环C 中
I/O program stuck in loop C++
我正在研究一个代码,该代码在C 源文件中读取,并将所有'<'符号转换为"<"以及">"的所有">"符号。我写出了主要方法,所有内容都很好地编译了,但是现在我实际上在程序的顶部写下了转换函数,我陷入了无限的循环中,并且我对罪魁祸首的是一堵墙。有人可以帮我吗?我包括整个程序,以防问题在我的I/O编码中,但我用斜杠包围了该功能。希望我不会被燃烧。
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
//FUNCTION GOES THROUGH EACH CHARACTER OF FILE
//AND CONVERTS ALL < & > TO < or > RESPECTIVELY
//////////////THIS IS THE FUNCTION IN QUESTION//////////
void convert (ifstream& inStream, ofstream& outStream){
cout << "start" << endl;
char x;
inStream.get(x);
while (!inStream.eof()){
if (x == '<')
outStream << "<";
else if (x == '>')
outStream << ">";
else
outStream << x;
}
cout << "end" << endl;
};
///////////////////////////////////////////////////////////////////////////
int main(){
//FILE OBJECTS
ifstream inputStream;
ofstream outputStream;
string fileName;
//string outFile;
//USER PROMPT FOR NAME OF FILE
cout << "Please enter the name of the file to be converted: " << endl;
cin >> fileName;
//outFile = fileName + ".html";
//ASSOCIATES FILE OBJECTS WITH FILES
inputStream.open(fileName.c_str());
outputStream.open(fileName + ".html");
//CREATES A CONVERTED OUTPUT WITH <PRE> AT START AND </PRE> AT END
outputStream << " <PRE>" << endl;
convert(inputStream, outputStream);
outputStream << " </PRE>" << endl;
inputStream.close();
outputStream.close();
cout << "Conversion complete." << endl;
return 0;
}
在阅读文件时,操纵文件不是一个好方法。正确的方法是,首先read
整个文件,store
数据,manipulate
存储的数据,然后 update
文件。希望此代码对您有所帮助:)
void convert()
{
int countLines = 0; // To count total lines in file
string *lines; // To store all lines
string temp;
ifstream in;
ofstream out;
// Opening file to count Lines
in.open("filename.txt");
while (!in.eof())
{
getline(in, temp);
countLines++;
}
in.close();
// Allocating Memory
lines = new string[countLines];
// Open it again to stroe data
in.open("filename.txt");
int i = 0;
while (!in.eof())
{
getline(in, lines[i]);
// To check if there is '<' symbol in the following line
for (int j = 0; lines[i][j] != ' '; j++)
{
// Checking the conditon
if (lines[i][j] == '<')
lines[i][j] = '>';
}
i++;
}
in.close();
// Now mainuplating the file
out.open("filename.txt");
for (int i = 0; i < countLines; i++)
{
out << lines[i];
if (i < countLines - 1)
out << endl;
}
out.close();
}
相关文章:
- C++程序在循环后给出奇怪的int值
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 为什么我的程序在for循环中k=0时返回垃圾值
- 密码登录程序将永远循环并显示不正确的结果
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 而循环:简单的除法程序输出零,不明白为什么
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 虽然循环在使用回车键时不退出程序
- 如何调试我的 while 循环C++程序?
- if/else 循环:C++程序:不会显示最终提示/最终循环
- 简单的C 嵌套以用于循环程序,以打印带有星星错误的正方形
- 带有空间的C 星号循环程序
- 循环程序的行为是不可预测的
- 如何解决嵌套循环程序的问题
- 对我的做边循环程序感到困惑
- gcc和clang都无法编译循环程序
- 需要帮助do-while循环程序?(c++)
- 一个用c++编写的简单游戏循环程序
- 如何循环程序,使其继续执行