我们如何从C ++中的txt文件中添加多个条目 [英] How do we add multiple entries together from a txt file in C++

查看:86
本文介绍了我们如何从C ++中的txt文件中添加多个条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在我开始之前,我会说这是我作业的一部分。我必须要求用户输入将被插入到文本文件中的信息,在该文本文件中将信息呈现给用户。我遇到的问题是其中一个变量输出必须是它自身的附加物。



例如,假设问题是,创建一个小的资金管理帐户。

我们需要插入之前的金额购买后,购买后,以及其他形式的信息。我会发布我到目前为止提出的片段。



我已插入代码。我的问题是,如何从我们给出的每个输入中添加第二列(Cost)?它现在没有循环,但理想情况下我希望每次插入一个成本,最后它将从所有

 COST 

> accountCost

已插入。



任何帮助表示赞赏,即使您只是在其他地方给我一个链接,也不胜感激。



PS。我的教授想要使用CSTRINGS而不是C ++ Strings,因此我使用char aStringVar [SIZE] =;典型的std :: string aStringVar [SIZE] =;



我尝试过:



  #include   <   iostream  >  
#include < fstream >
#include < cstdlib >

const int SIZE = 256 ;

int main( void ){

ifstream readFile;
ofstream writeFile;
char accountName [SIZE] = ;
char cost [SIZE] = ;
char 总计[SIZE] = ;
char Tax [SIZE] = ;
int accountCost = 0 ;
int accountTotal = 0 ;
int accountTax = 0 ;

/ *
用户使用cout和cin将所有信息输入到变量中功能。
不插入它,因为它非常基本,我不想花费我所有的午休时间(工作)编写这部分代码。
* /


// 这部分只会保存根据我指定的格式输入文本文件。 EX:Jimmy,120,450,2
writeFile.open( bank.txt,ios :: app | ios :: ate);
writeFile<< accountName<< <<成本<< <<总计<< <<税<< \ n;
writeFile.close();

// 这里我们将输出
readFile.open( bank.txt);
readFile.getline(accountName,SIZE,' ,');
readFile.getline(Cost,SIZE,' ,');
readFile.getline(Total,SIZE,' ,');
readFile.getline(Tax,SIZE); // 从最后一个开始就没有使用delimeter。

accountCost = atoi(成本);
accountTotal = atoi(总计);
accountTax = atoi(Tax);

cout<< 帐户成本:<< accountCost<< ENDL;
cout<< 帐户总计:<< accountTotal<< ENDL;
cout<< 帐户税:<< accountTax<< ENDL;


}

解决方案

用法通常是

< pre lang =c ++> std :: string aStringVar = 快乐编码; // 一个字符串
std :: string aStringVar1 [SIZE] = ; // 数组
aStringVar1 [ 0 ] = hello world; // 访问第一个字符串



您可以在打开的文件中写入,直到它关闭。扩展代码

 writeFile.open(  bank。 txt,ios :: app | ios :: ate); 
writeFile<< accountName<< <<成本<< <<总计<< <<税<< \ n;
writeFile<< 进一步写入文件;
writeFile.close();





仔细想想你在文件中写什么以及如何写,因为有些代码或人类会阅读。测试很重要!!!



因为您对std :: string缺少一些知识,所以请阅读tutorial


So before I get started, I will state this is part of my homework. I have to ask the user to input information that will get inserted into a text file in which it will be presented back to the user. The issue I'm having is that one of the variable outputs must be an addition of itself.

Example, lets say the problem is, creating a small money management account.
We need to insert the amount of money we had before purchase, after purchase, and total, as well as other forms of information. I'll post the snippet I've come up with so far.

I've inserted the code. My question is this, how can I have it add the second column (Cost) from every input we give it? Its not looping right now but ideally I want every time I insert a cost, at the end it will add all the

COST

from all the

accountCost 

inserted.

Any help is appreciated, even if you just give me a link elsewhere, it is appreciated.

PS. My professor wants the use of CSTRINGS not C++ Strings, hence why im using char aStringVar[SIZE] = ""; over a typical std::string aStringVar[SIZE] = "";

What I have tried:

#include <iostream>
#include <fstream>
#include <cstdlib>

const int SIZE = 256;

int main(void){

ifstream readFile;
ofstream writeFile;
char accountName[SIZE] = "";
char cost[SIZE] = "";
char Total[SIZE] = "";
char Tax[SIZE] = "";
int accountCost = 0;
int accountTotal = 0;
int accountTax = 0;

/*
user inputs all the information into the variables with cout and cin functions.
Not inserting it since its pretty basic and I don't want to spend all my lunch break (work) writing this part of the code.
*/

//This part will simply save the input into the text file according to the format I specified. EX: Jimmy,120,450,2
writeFile.open("bank.txt", ios::app | ios::ate);
writeFile << accountName << "," << Cost << "," << Total << "," << Tax << "\n";
writeFile.close();

//Here we will output
readFile.open("bank.txt");
readFile.getline(accountName, SIZE, ',');
readFile.getline(Cost, SIZE, ',');
readFile.getline(Total, SIZE, ',');
readFile.getline(Tax, SIZE); //didn't use delimeter since its the last one.

accountCost = atoi(Cost);
accountTotal = atoi(Total);
accountTax = atoi(Tax);

cout << "Account Cost: " << accountCost << endl;
cout << "Account Total: " << accountTotal << endl;
cout << "Account Tax: " << accountTax << endl;


}

解决方案

The usage is normally

std::string aStringVar = "happy coding";//one string
std::string aStringVar1[SIZE] = "";// an array
aStringVar1[0] = "hello world";//access the first string


You can write in an open file til it is closed. Extending your code

writeFile.open("bank.txt", ios::app | ios::ate);
writeFile << accountName << "," << Cost << "," << Total << "," << Tax << "\n";
writeFile << "Further writing in file";
writeFile.close();



Think carefully what and how you write in the file, because some code or human will read it. Testing is important!!!

Because you have some missing knowledge of the std::string read this tutorial.


这篇关于我们如何从C ++中的txt文件中添加多个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆