如何在visual studio 2013中使用c++从文本文件中获取整数
How to fetch the intergers from a text file using c++ in visual studio 2013
我正在为我的项目制作一个小型会计控制台应用程序。显然我在文件I/O上卡住了。我创建了一个文件来存储客户的数据。其格式如下。(obj是一个std::ofstream对象)
obj << name << "t" << ac_no << "`enter code here`t" << l_bal << "t" << txn << "t" << bal;
我想做的是提取"bal"下的最后一个值。这是最后一个文件中第4个选项卡后面的整数值。文件的内容是这样的
名称A/C否L_Bal Tnx Bal
维卡斯s000001 0 1000 1000
维卡斯s00001 1000 500 1500
我想做的是提取1500并将它赋值给变量cur_bal。我可以使用以下代码访问最后一行制表符的loc
iobj.seekg(-1, ios::end);
int j = iobj.tellg();
iobj.seekg(0, ios::beg);
for (int i = 0; i <= j; i++)
{
int p = toascii(iobj.get());
if (p == 9) // succesfully finding tab char by ascii code
{
tab++;
switch (tab % 4)
{
case 1:
pos[0] = (iobj.tellg()); //1st tab position
break;
case 2:
pos[1] = (iobj.tellg()); //2nd tab position
break;
case 3:
pos[2] = (iobj.tellg()); //3rd tab position
break;
case 0:
pos[3] = (iobj.tellg()); //4th tab position
break;
}
}
else if (p == 10) succesfully finding newline char by ascii code
{
line++;
pos[4] = (iobj.tellg()); //'n' position
for (int i = 0; i < 5;i++)
{
if (i == 4)
pos[i] = pos[i] - line - 1; /* position of 'n' is shifted by
no of line found +1. so corrected the value.*/
else
pos[i] = pos[i] - line; /* position of tab is shifted
by no of line found. so corrected the value. */
}
}
}
接下来我要做的就是我遇到问题的地方。
{int s = pos[3], e = pos[4]; //position in b/w which value of bal is placed in file
char x[6];
iobj.seekg(pos[3],ios::beg); // this is not take the pointer to desired placed.
iobj.seekg(pos[3]); //Nor did this.
iobj>>x[0]; // and this is also not transferring any char or integer to the char array. nor did it do in next statements.
iobj>>x[1];
iobj>>x[2];
iobj>>x[3];
cout << x<<endl;
}
请帮忙,我实在想不明白。还有一件事,我在visual studio中没有使用c++/clr模式,因为我使用的是c和c++库。
我觉得你太努力了。
// skip header
std::string header
std::getline(iobj, header);
// read to end of file
std::string name;
int acct_no, l_bal, tnx, bal, curr_bal = 0;
while (iob >> name >> acct_no >> l_bal >> tnx >> bal)
{
curr_bal = bal;
}
// current balence is the last balence read in
cout << "current labance is " << curr_bal << "n";
未经测试的代码,它假设文件格式正确,并且名称不包含空格。所以你可能需要一些更复杂的东西,但是你的代码对于一个简单的问题来说太复杂了。
另一方面,当前余额是一个如此重要的概念,如果您只能通过读取整个文件来检索它,那么您的文件格式就很差。
一种方法是获取最后一行,然后读入。这不是最有效的方法,但可能会让你开始:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
string getLastLine(istream& is) {
string line;
while (is >> std::ws && getline(is, line))
;
return line;
}
int main() {
ifstream iobj("test.txt");
string line = getLastLine(iobj);
string name, ac_no;
int l_bal, tnx, cur_bal;
stringstream ss(line);
ss >> name >> ac_no >> l_bal >> tnx >> cur_bal;
cout << cur_bal << endl;
return 0;
}
相关文章:
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 如何创建一个QTableWidgetItem,用长文本右对齐,左边有省略号
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何根据单词在文本中出现的概率输出单词
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端