如果\n
文件末尾有ifstream
,我的程序可以正常运行。
任务是这样的: 定义产品的结构 (产品名称,类别,价格,库存数量)。 来自File trgovina.txt(它可能已包含预定义的产品。"在我发布的图片中相似) 通过功能:
问题开始here:如果此人停止在第一个黄色标记处写字,我想添加一个新行,这样当我想添加新文本时,它将以新行开头。如果文本文件末尾已有新行,我想继续写,不添加新行。
第二个问题是如果ofstream文件没有文本:在这种情况下,我不想添加新行。
ofstream dat("trgovina.txt", std::ios_base::app);
if (!dat)
{
cout << "Mistake!" << endl;
return;
}
string name;
string category;
bool a = true;
double price;
int quantity;
while (a)
{
**//Here I want to check if there is a "\n" sign at the end of text
//if there isn't I want to add one, and then continue writing.
// But if the text file was empty i dont want to add new line**
cout << "enter the name of new product: ";
getline(cin, name);
dat << name << "|";
cout << "enter the category of new product: ";
getline(cin, category);
dat << category << "|";
cout << "enter the price of new product: ";
cin >> price;
dat << price << "|";
cout << "enter the quantity of new product: ";
cin >> quantity;
dat << quantity << endl; **//This works if the txt file is empty or if
//there is "\n" at the end**
cout << "more? (1 / 0)";
cin >> a;
cin.ignore();
system("cls");
};
dat.close();
抱歉我的英语不好。
我使用
收到错误while (a)
{
trgovina.seekg(-1);
char c;
trgovina.get(c);
if (c == '\n')
{
}
else
{
dat << endl;
}
答案 0 :(得分:0)
它是一个index.html
,所以你只能写一个流。你需要的是阅读(确定内部)和写(添加项目),这称为make_presentation.slides.html
。您可以阅读,写入和寻找ofstream
。要确定它的最后一个字符是否是换行符,只需查找最后一个字符并使用类似的字符读取它:
fstream