簡體   English   中英

c ++中文件處理中的錯誤-沒有匹配函數可調用`std :: basic_fstream <char, std::char_traits<char> &gt; :: open(const char [8],bool)&#39;

[英]error in file handling in c++--no matching function for call to `std::basic_fstream<char, std::char_traits<char> >::open(const char[8], bool)'

我正在嘗試一個文件處理程序來輸入一條記錄,而在另一個程序中嘗試刪除一條記錄。 我似乎收到此錯誤-

沒有匹配的函數來調用`std :: basic_fstream> :: open(const char [8],bool)'

對於每個其中有ios :: binary聲明的f.open語句。

這是代碼

如果輸入記錄,則此

#include<iostream.h>
#include<fstream.h>

class employee
{
      int code;
      char name[20];
      char desig[15];
      float salary;

      public: void get_emp();
};

void employee:: get_emp()
{
     cout<<"Code--> ";
     fflush(stdin);
     cin>>code;
     cout<<"Name--> ";
     fflush(stdin);
     gets(name);
     cout<<"Designation--> ";
     fflush(stdin);
     gets(desig);
     cout<<"Salary--> ";
     fflush(stdin);
     cin>>salary;
}

int main()
{
    fstream f;
    char ch;
    f.open("EMP.dat", ios::binary||ios::app);
    employee emp;
    cout<<"Enter data:\n";
    do
    {
                 emp.get_emp();
                 f.write((char*)&emp,sizeof(emp));
                 cout<<"Entre more???(y/n)";
                 cin>>ch;
    }while((ch=='y')||(ch=='Y'));
    f.close();
    system("pause>null");
    return 0;
}

這是用於刪除記錄

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
struct employee
{
       int code;
       char name[20];
       char desig[15];
       float salary;
}emp;

int main()
{
    int xcode; //temporary declaration for employee code
    int flag = 0;
    fstream ef, tf; 
    //ef opened for reading, tf opened for transferring all records including modified      record 
    ef.open("EMP.dat", ios::binary|| ios::in);
    tf.open("TEMP.dat", ios::binary|| ios::out);
    cout<<"Enter employee code to delete:";
    cin>>xcode;
    while(ef)
    {
             if(!ef)
                exit(0);
             ef.read((char*)&emp, sizeof(emp));
             if(emp.code == xcode)
             {
                            flag = 1;
             }
             else
                tf.write((char*)&emp, sizeof(emp));
    }
    ef.close();
    tf.close();
    if(flag == 1)
       cout<<"Record deleted.";
    else
       cout<<"Not found.";
    fstream xf, yf;
    //tf opened for reading
    xf.open("TEMP.dat", ios::binary||ios::in);
    //ef opened for copying all records from TEMP.dat
    yf.open("EMP.dat",ios::binary||ios::out);
    while(xf)
    {
             if(!xf)
                exit(0);
             xf.read((char*)&emp, sizeof(emp));
             yf.write((char*)&emp, sizeof(emp));
    }
    xf.close();
    yf.close();
    system("pause");
    return 0;
}

請幫忙。 我考試需要它。

謝謝

按位OR是單管道(|),而不是雙管道。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM