Thread: file handling open function error

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    3

    file handling open function error

    Hey there,

    I have an error in file handling. When i use open function with ios::binary mode, it says-

    no matching function for call to `std::basic_fstream<char, std::char_traits<char> >:pen(const char[8], bool)'

    here are the codes. 2 files, one for entering data into file, one for deleting records.

    the following is for writing records to file

    Code:
    #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;
    }

    the following is for deleting records
    Code:
    #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;
    }
    Please help,

    thank you

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Hi! You may remember me from Stack Overflow but the answer is still: logical or is two pipes, but this isn't a logical or, but a bitwise or, which is just a single pipe. You can see an example of such single pipes here: std::basic_filebuf:pen - cppreference.com

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    3
    i found the error, you were correct, sorry. | is what is to be put, not ||. But there is one more problem. the program is working, but after i enter the records with the 1st code, and then i execute the 2nd code for deletion, and enter a record i want to delete(I have entered that record earlier), it says not found. Also, i go to my folder and open the EMP file to find it empty.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You've got a lot of ancient-style headers (like fstream.h instead of fstream etc). Anyway, your write program isn't writing a file for me (well, it is, but it's all zeroes). Changing it to "ofstream f;" works, depressingly.

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    3
    I don't know about the ancient part, but this is in my syllabus and i must follow it. Changing it to ofstream makes no difference bro.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Arjun Bhat View Post
    I don't know about the ancient part, but this is in my syllabus and i must follow it. Changing it to ofstream makes no difference bro.
    Did you change the logical ORs (||) to bitwise ORs (|) ?

    Oh, and don't call me bro.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Arjun Bhat View Post
    Code:
    fflush(stdin);
    Wrong! and especially wrong to mix C (stdin) and C++ (cin) style file IO.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Arjun Bhat View Post
    I don't know about the ancient part, but this is in my syllabus and i must follow it. Changing it to ofstream makes no difference bro.
    I don't have the pre-(standard)C++ fstream.h and iostream.h etc headers, so I don't know if things worked differently back then; they probably did, but I don't know what the differences are.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File handling error.
    By Sne9x in forum C++ Programming
    Replies: 7
    Last Post: 04-15-2012, 08:10 AM
  2. error in creat file by using 'open' function
    By liuguobiao in forum Linux Programming
    Replies: 5
    Last Post: 03-23-2009, 10:59 AM
  3. error handling in recursive function.
    By broli86 in forum C Programming
    Replies: 4
    Last Post: 06-23-2008, 02:11 PM
  4. Error handling and file ouput
    By cunnus88 in forum C++ Programming
    Replies: 2
    Last Post: 02-03-2008, 10:04 AM
  5. file handling and function problem.
    By prepare in forum C Programming
    Replies: 7
    Last Post: 10-09-2004, 02:26 AM

Tags for this Thread