file

This is a discussion on file within the C++ Programming forums, part of the General Programming Boards category; 1. Code: #include <iostream> #include <fstream> using namespace std; void f(const char *fname) { ifstream fin(fname,ios::in | ios::binary); if(!fin) cout ...

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    file

    1.
    Code:
        #include <iostream>
        #include <fstream>
        using namespace std;
    
        void f(const char *fname)
        {
               ifstream fin(fname,ios::in | ios::binary);
               if(!fin)
                      cout << "File not opened";
        }
    
         int main()
         {
                system("notepad ABC");
       
                f("ABC");
    
                system("pause");
          }
    When the notepad opens the file something is written into the file
    and saved.But when f() opens the file the output shown : File not opened.How can i open the file from f()?
    (Tried on Dev C++ 4.9.9.2 and Code Blocks 1.02rc)

    2. Is _unlink for removing a file from C++ outdated as i need to include io.h for it 2 work?How can i do so using new headers?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    'ABC" is probably not the name of the file -- try "ABC.TXT" -- and if it was not saved in your program's current working directory then you will have to specify the full path, such as "C:\\MYDIR\\ABC.TXT"

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    avoid using system() calls

    it's potentially dangerous - who's to say I don't create a virus that replaces pause.exe with my own pause.exe (the virus). Your program would be happily spreading my virus and you would never know =)

    and why are you opening the file as binary when you wrote it as plain text?

    and if you use ifstream ios::in is assumed.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 09:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21