【问题标题】:function to stream certain name and number from txt file从 txt 文件中流式传输某些名称和数字的功能
【发布时间】:2020-12-07 08:37:34
【问题描述】:

我创建了一个名为 clientFile.txt 的文件 该文件包含此clientFile.txt。内容:

abcd abcd 123 1607325695
A AI 123 1607327861

下面的函数应该在函数调用时显示文本文件的内容。

void displayFile()
{
    string str1;
    string str2;
    string fileN = "clientFile.txt";
    ifstream myfilein;
    myfilein.open("clientFile.txt");
    double balance;
    int numAct = 0;
    time_t transTime;
    if(!myfilein){
        cerr << "FIle could not be opened" << endl;
        exit(1);
    }
    while(myfilein >> str1 >> str2 >> balance >> transTime){
        cout << setw(15) << str1 << ' ' << setw(15) << str2 << ' '
        << setw(7) << balance << "  " << ctime(&transTime);
        numAct++;
    }
    myfilein.close();
    cout << "Number of records in the file " << fileN << ": " << numAct << endl;
}

但是当我在这个 int main 上调用它时:

int main(){
    ofstream myfileout;
    ifstream myfilein;
    string firstName,lastName;
    int i=0;
    string FN[1000],LN[1000];
    
    double actBalance,AB[1000];
    time_t currentTime,CT[1000];
    myfilein.open ("clientFile.txt");
    void displayFile();
    if(myfilein.fail()){
        cout<<"Creating new files"<<endl;
        myfileout.open ("clientFile.txt",ios::out);
        myfileout.close();
        exit(1);
    }
    while(myfilein>>firstName>>lastName>>actBalance>>currentTime){
        FN[i]=firstName;
        LN[i]=lastName;
        AB[i]=actBalance;
        CT[i]=currentTime;
        i++;
    }
    myfileout.open("clientFile.txt",ios::app);
    cout<<"Enter first name, last name, and balance:"<<endl;
    while(cin>>firstName>>lastName>>actBalance){
         createRecord(myfileout, firstName,lastName,actBalance);
    
    }
    void displayFile();
    myfileout.close();
}

它没有显示任何文件内容。 你们能帮帮我吗? 我正在使用带有 TDM-GCC 4.9.2 64 位版本的 dev c++

【问题讨论】:

  • 强烈建议使用stuct 保存每个客户的信息,然后使用std::vector&lt;struct name&gt; 而不是单独的数组来处理由索引协调的每一位信息(脆弱)然后你可以重载ifstream 和@987654328 @ 并使用单个 &gt;&gt; 处理输入。
  • 这是一个错字:你声明了displayFile,而不是在main()的末尾调用它。

标签: c++ function fstream


【解决方案1】:

在 main() 的最后几行你只需要改变

void displayFile();

displayFile();

因为void 是在声明一个函数,而不是调用它。

【讨论】:

  • 错别字一般由comment + VTC处理。
猜你喜欢
  • 2010-12-29
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-05
  • 1970-01-01
  • 1970-01-01
  • 2014-03-10
相关资源
最近更新 更多