C++ C++;fstream读取和写入文件
我完全卡住了。这是我不完整的程序。程序卡在.at或if处,确定元音行是否正确。我需要一些建议来修正我的程序。程序应该做的是获取包含以下行的文本文件: C++ Programming is fun Winter is here, will it ever end? The style requirements document is so useful Functions really make programming easier Spring must be coming soon... I wish this #$##$$!!## program was done What comes after C++, D--? C++编程很有趣 冬天到了,它会结束吗? 样式需求文档非常有用 函数确实使编程更容易 春天一定很快就要来了。。。 我希望这个#$##$$程序完成了 C++、D—之后是什么? 我应该做一些计算并导出到一个文件中,这样在这个链接中看起来像: 这是到目前为止我的代码。任何关于我做错了什么的指点都将不胜感激。我必须按功能做每件事C++ C++;fstream读取和写入文件,c++,io,fstream,getline,C++,Io,Fstream,Getline,我完全卡住了。这是我不完整的程序。程序卡在.at或if处,确定元音行是否正确。我需要一些建议来修正我的程序。程序应该做的是获取包含以下行的文本文件: C++ Programming is fun Winter is here, will it ever end? The style requirements document is so useful Functions really make programming easier Spring must be coming soon... I
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
void OpenFiles (ifstream& inFile, ofstream& outFile);
void OutputDivider (ofstream& outFile, char symbol, int num);
void SetFileMessage (ifstream& inFile, bool& fileFound, string& message);
void OutputFileMessage (bool& fileFound, ofstream& outFile, string& message);
void InitializeFileCounters (int& lineCounter, int& totalOfAs, int& characterTotal);
void OutputHeading (ofstream& outFile, string information, string name, int width);
string ReadALine (ifstream& inFile, int& lineLength);
void InitializeLineCounters (int& vowelCounter, int& charactersOfA, int& blankCounter);
void CalculateLineStats (ifstream& inFile, string readLine, int& vowelCounter, int& lineLength,
int& blankCounter, int& charactersOfA);
//Header to display in output file
const string INFORMATION = "CST 133 - Exercise 4";
const string MY_NAME = "Tom Hangler";
//Width of screen for dividers
const int SCREEN_WIDTH = 110;
int main(void)
{
ifstream fin;
ofstream fout;
bool fileFound;
string message;
string lineCopy;
int lineCounter;
int totalOfAs;
int characterTotal;
int vowelCounter;
int charactersOfA;
int blankCounter;
int lineLength;
//Open input and output text files
OpenFiles(fin, fout);
//Set message based on if file was found or not
SetFileMessage(fin, fileFound, message);
//Output message to text file if file was not found
OutputFileMessage(fileFound, fout, message);
if (fileFound)
{
InitializeFileCounters(lineCounter, totalOfAs, characterTotal);
OutputHeading(fout, INFORMATION, MY_NAME, SCREEN_WIDTH);
InitializeLineCounters(vowelCounter, charactersOfA, blankCounter);
ReadALine(fin, lineLength);
CalculateLineStats(fin, lineCopy, vowelCounter, lineLength, blankCounter, charactersOfA);
ReadALine(fin, lineLength);
}
return 0;
}
void OpenFiles (ifstream& inFile, ofstream& outFile)
{
inFile.open("LinesEx4.txt");
outFile.open("Ex4Out.txt");
}
void OutputDivider (ofstream& outFile, char symbol, int num)
{
//Ouput to text file a divider with symbol '-' and specified length in 'num'
outFile << setfill(symbol) << setw(num) << symbol << endl;
}
void SetFileMessage (ifstream& inFile, bool& fileFound, string& message)
{
if (!inFile)
{
//Set message string to file not found.
message = "Input file was not found.";
//Set the file found flag
fileFound = false;
}
else
{
//Set the file found flag
fileFound = true;
//Set message string to file not found.
message = "Processing continues, file successfully opened.";
}
}
void OutputFileMessage (bool& fileFound, ofstream& outFile, string& message)
{
if (fileFound == false)
{
//Output divider to text file
OutputDivider(outFile, '-', SCREEN_WIDTH);
//Output message to text file.
outFile << message << endl;
//Output divider to text file
OutputDivider(outFile, '-', SCREEN_WIDTH);
}
else
{
//Output divider to text file
OutputDivider(outFile, '-', SCREEN_WIDTH);
//Output message to text file.
outFile << message << endl;
//Output divider to text file
OutputDivider(outFile, '-', SCREEN_WIDTH);
}
}
void InitializeFileCounters (int& lineCounter, int& totalOfAs, int& characterTotal)
{
//Initialize variables for total counting
lineCounter = 0;
totalOfAs = 0;
characterTotal = 0;
}
void OutputHeading (ofstream& outFile, string information, string name, int width)
{
outFile << '\n' << endl;
OutputDivider(outFile, '*', width);
outFile << setfill(' ') << setw((width + information.length()) / 2) << information << endl;
outFile << setfill(' ') << setw((width + name.length()) / 2) << name << endl;
outFile << "Line" << setw(50) << "Length" << setw(15) << "# of Vowels" <<
setw(15) << "# of As" << setw(15) << "# of Blanks" << endl;
OutputDivider(outFile, '*', width);
}
string ReadALine (ifstream& inFile, int& lineLength)
{
string readLine;
getline(inFile, readLine);
lineLength = readLine.length();
return readLine;
}
void InitializeLineCounters (int& vowelCounter, int& charactersOfA, int& blankCounter)
{
//Initialize variables for line analysis
vowelCounter = 0;
charactersOfA = 0;
blankCounter = 0;
}
void CalculateLineStats (ifstream& inFile, string readLine, int& vowelCounter, int& lineLength,
int& blankCounter, int& charactersOfA)
{
int index;
for (index = 0; index < lineLength; index++)
{
if (readLine.at(index) == 'A' || readLine.at(index) == 'E' || readLine.at(index) == 'I'
|| readLine.at(index) == 'O' || readLine.at(index) == 'U')
{
vowelCounter++;
}
if (readLine.at(index) == ' ')
{
blankCounter++;
}
if (readLine.at(index) == 'A')
{
charactersOfA++;
}
}
#包括
#包括
#包括
#包括
使用名称空间std;
作废打开文件(ifstream和INFLE、ofstream和OUTILE);
无效输出分隔符(流和输出文件、字符符号、整数);
void SetFileMessage(ifstream&infle、bool&fileFound、string&message);
void OutputFileMessage(bool和fileFound、流和输出文件、字符串和消息);
无效初始化计数器(int&lineCounter、int&totalOfAs、int&characterTotal);
无效输出标题(流和输出文件、字符串信息、字符串名称、整数宽度);
字符串读取线(ifstream和infle、int和lineLength);
无效初始值计数器(整型和元音计数器、整型和字符计数器、整型和空白计数器);
void CalculateLineStats(ifstream和infle、字符串readLine、int和元音计数器、int和lineLength、,
int和blankCounter、int和charactersOfA);
//要在输出文件中显示的标题
const string INFORMATION=“CST 133-练习4”;
const string MY_NAME=“Tom Hangler”;
//分隔器的屏幕宽度
屏幕宽度=110;
内部主(空)
{
流鳍;
流式流量计;
找到布尔文件;
字符串消息;
字符串行复制;
内线计数器;
总积分;
整数字符总数;
int元音计数器;
int字符;
整数空白计数器;
int行长度;
//打开输入和输出文本文件
开放文件(fin、fout);
//根据是否找到文件设置消息
SetFileMessage(fin、fileFound、message);
//如果找不到文件,则将消息输出到文本文件
OutputFileMessage(fileFound、fout、message);
如果(找到文件)
{
初始化计数器(lineCounter、totalOfAs、characterTotal);
输出权限(fout、信息、我的姓名、屏幕宽度);
初始化计数器(元音计数器、字符计数器、空白计数器);
ReadALine(鳍、线长度);
计算器状态(fin、lineCopy、元音计数器、线宽、空白计数器、charactersOfA);
ReadALine(鳍、线长度);
}
返回0;
}
void OpenFiles(ifstream和infle、ofstream和outFile)
{
infle.open(“LinesEx4.txt”);
outFile.open(“Ex4Out.txt”);
}
无效输出分隔符(流和输出文件、字符符号、整数)
{
//向文本文件输出一个带符号“-”且指定长度为“num”的分隔符
outFile看起来您正在将一个空字符串lineCopy
作为第二个参数传递给您的函数CalculateLineStats
,然后调用.at()
。在调用此函数之前,您应该读入该行。您没有存储ReadALine()的返回值
但将lineLength>0
传递到CalculateLineStats()
。这将导致抛出std::out\u of \u range
异常,调用方未捕获该异常,并将导致程序异常终止
改为:
lineCopy = ReadALine(fin, lineLength);
没有理由将线宽
存储在单独的变量中