如何在visual studio 2013中使用c++从文本文件中获取整数

How to fetch the intergers from a text file using c++ in visual studio 2013

本文关键字:c++ 文本 文件 整数 获取 visual studio 2013      更新时间:2023-10-16

我正在为我的项目制作一个小型会计控制台应用程序。显然我在文件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;
}