- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编程类(class)的最终项目将于明天到期,感谢任何帮助,在接受文件名后,程序在此模块中崩溃。我所说的崩溃是指它输出“此应用程序已请求运行时以异常方式终止它”,然后通常的 Windows “CotD.exe 已停止工作”
:
void load(vector<Fish>& stock)
{
char c;
do {
cout << "Welcome to Catch of The Day, enter (f) to choose a file to load from, otherwise enter anything else to load from default file.\n";
cin >> c;
if (c == 'f' || c == 'F')
{
cout << "Enter file name\n";
cin >> file;
}
ifstream fin(file.c_str());
if (fin.fail())
{
cout << "Could not open " << file << " Check the directory location of CotD.exe and try again\n";
}
else
{
while (!fin.eof())
{
Fish f;
string blank;
fin >> f.amt;
fin >> f.prc;
fin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
getline(fin, blank);
stock.push_back(f);
}
fin.close();
break;
}
} while (true);
}
编辑其他相关代码:
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
using namespace std;
//
string file = "default.txt"; //Global variable used to store name of save file.
//It is global so that load() and save() can both access it.
struct Fish
{
string type;
double amt;
double prc;
double val;
};
void addType(vector<Fish>&);
void editStock(vector<Fish>&);
void sortBy(vector<Fish>&);
void sortAsc(vector<Fish>&,char);
void sortDesc(vector<Fish>&,char);
void display(vector<Fish>&);
int search(vector<Fish>&);
void save(vector<Fish>&);
void load(vector<Fish>&);
string getType();
int dispType(string,vector<Fish>&);
int find(string,vector<Fish>&);
double getAmt();
void delType(string,vector<Fish>&);
void menu(vector<Fish>&);
double getPrc();
int main(){
std::vector<Fish> stock;
load(stock);
menu(stock);
save(stock);
cout<<endl<<endl
<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
<<"|Thank you for using Catch of the Day|\n"
<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
system("Pause");
return 0;
}
我最近编写了这个程序,它看起来与我非常相似,并且运行完美,我看不出有什么区别:
void load(vector<string>& names)
{
string file, name, bad;
while (true)
{
cout << "Input file name\n";
getline(cin, file);
ifstream fin(file.c_str());
if (fin.fail())
{
cout << "Could not open " << file << ", try again.\n";
}
else break;
}
ifstream fin(file.c_str());
while (!fin.eof())
{
fin >> bad;
fin >> name;
cout << "\"" << name << "\"" << endl;
}
system("Pause");
fin.close();
ifstream fin(file.c_str());
while (!fin.eof())
{
getline(fin, name);
names.push_back(name);
}
system("Pause");
fin.close();
cout << "Names added to list\n";
}
最佳答案
我已经编辑了你的代码,这就是我得到的:
void load(vector<Fish>& stock)
{
char c;
do {
cout << "Welcome to Catch of The Day, enter (f) to choose a file to load from, otherwise enter anything else to load from default file.\n";
cin >> c;
if (c == 'f' || c == 'F')
{
cout << "Enter file name\n";
cin >> file;
}
ifstream fin(file.c_str());
if (fin.fail())
{
cout << "Could not open " << file << " Check the directory location of CotD.exe and try again\n";
}
else
{
Fish f;
string blank;
if (fin>>f.amt)
{
if (fin>>f.prc)
{
getline(fin,blank);
stock.pushback(f);
}
}
fin.close();
break;
}
} while (true);
}
当然,这是在不知道文件中有什么以及 Fish 到底是什么的情况下,所以我不知道这是否是您要查找的内容。
编辑:如果您可以包含该文件,或者只是一条“鱼”的一部分(我认为这就是文件的内容),那么提供帮助会更容易。
关于使用 fstream fin 从文件读取时 C++ 程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22901068/
我需要能够创建一个不存在的文件。设计如下:我有 1 个线程用于所有文件 IO,并且在封装文件的数据结构中,我有一个 std::fstream file_handle。 我可以在模式 - std::fs
如标题所说,以fstream::out 模式打开fstream 会清除其当前内容吗?如果不是,使用 fstream 库删除 .txt 文件的最佳方法是什么。 C++ fstream 库中 EOF 的等
我只是想写(追加)到一个日志文件。我在这里查了一下: http://www.cplusplus.com/reference/iostream/fstream/open/ 这就是我所做的 #includ
我最近使用 fstream 完成一项家庭作业,我想知道这两个东西是如何工作的。 #include #include using namespace std; int main(int argc,
我将如何为 seekp 获取输入并将其用于 hex?是这样的吗? Cin>>hex>>mychar; 如何打印正确的十六进制值?说在 0x16 字节是 FF 或 255 无符号 8 位(十进制?)当我
据我所知,read() 和 write() 在那里,所以我们可以直接从文件读取字节或向文件写入字节,我被教导byte 在 c++ 中的等价物是 unsigned char,那么为什么他们将 char
所以我尝试编写一个小程序,当我尝试从 .txt 文件输入负数时,我的程序失败了,只是在第一个负数之后输出随机数。 #include #include #include #include usi
我的计算机类(class)有一个代码编写作业,其中包括 fstream 类(class)。我必须编写一个代码来管理和存储用户数据。 我在类里面很难理解这个概念,而 C++ 教科书只想继续向我展示 IP
我无法在文件中间写入文本。我可以正确找到添加文本的位置,并且可以使用 tellg()/tellp() 检查它。但是,在 seekp() 之后,我添加了新文本: myfstream << "new te
我正在尝试制作一个程序,该程序将从一个文件读取并根据输入整数输出到另一个文件,该值与读入的值不同。我在打开文件时遇到错误。没有匹配的调用函数 #include #include #include
当文件位于特定位置时,我遇到了 fstream 无法写入文件的问题。在下面的代码中,当注释行被取消注释时,文件被写入,但是当它被注释时,文件根本没有被写入。我添加了一堆控制台输出,它表示围绕“outp
我在这个网站上搜索了很多几乎相同的问题,但没有一个对我有用。首先,让我告诉你我正在使用 Code::Blocks 并且我正在使用 Ubuntu。这是我的代码: #include #include
我正在尝试将一些内容写入文本文件,但它甚至无法创建文件。如果您能帮助我,我将不胜感激。谢谢。 #include #include int main(){ std::ofstream fil
我现在尝试将一些整数值写入文件,然后使用 fstream 读取它。这是我的做法: #include #include #include #include typedef u
我是 C++ 编程的新手,遇到了一个问题。我将代码块 IDE 与它附带的默认编译器一起使用。 我的问题是,当我编写这段代码时,为什么没有在我的桌面上创建一个文件? #include #include
当我向文件流写入一个 double ,然后写入一个整数时,整数会作为额外数字附加到 double ,我不知道为什么会这样。有人可以为我解释一下吗?最小示例: #include #include i
我正在尝试序列化我的类的一些私有(private)属性: class Task { public: enum Status { COMPLETED, PENDIENT };
我一直在研究 C++ 中的 fstream 类,看看我是否能够将一些数据写入文本文件 (.txt)。据我所知,如果程序试图写入一个不存在的文件,那么它会自动创建该文件,我错了吗?这个程序非常简单,没有
我在我的代码中找不到问题。 readFile 函数运行良好,但 writeFile 函数不会对文件进行任何更改: #include #include using namespace std; co
如果您的文件系统有很多交叉链接文件,是否有一种好方法可以确保文件内容与您编写的内容相同。 问题是当我写入文件内容时关闭文件并重新打开它文件被其他文件中的其他行损坏。因此,如果我在文件中写入“AAA”,
我是一名优秀的程序员,十分优秀!