CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: A file problem

  1. #1
    Join Date
    Jul 2006
    Posts
    24

    Arrow A file problem

    The code below is supposed to open an existing file to read, but when you enter a nonexisting file it creates that file! Why does this happen and how can I solve this problem.
    Code:
    ifstream inFile;
    
    inFile.open("u.txt" );
    Thanks...

  2. #2
    Join Date
    Aug 2005
    Posts
    478

    Re: A file problem

    I doubt it with an ifstream like that. From: http://wwww.dinkum.com/manuals/defau...e=fstream.html

    Code:
        explicit basic_ifstream(const char *filename,
            ios_base::openmode mode = ios_base::in);
    
        void open(const char *filename,
            ios_base::openmode mode = ios_base::in);
    And here's info about all the openmode's: https://www.dinkumware.com/manuals/?...#filebuf::open

    Quote Originally Posted by A person who is very smart
    The member function endeavors to open the file with filename filename, by calling fopen(filename, strmode). Here strmode is determined from mode & ~(ate | binary):

    * ios_base::in becomes "r" (open existing file for reading).
    * ios_base::out or ios_base::out | ios_base::trunc becomes "w" (truncate existing file or create for writing).
    * ios_base::out | ios_base::app becomes "a" (open existing file for appending all writes).
    * ios_base::in | ios_base::out becomes "r+" (open existing file for reading and writing).
    * ios_base::in | ios_base::out | ios_base::trunc becomes "w+" (truncate existing file or create for reading and writing).
    * ios_base::in | ios_base::out | ios_base::app becomes "a+" (open existing file for reading and for appending all writes).

    If mode & ios_base::binary is nonzero, the function appends b to strmode to open a binary stream instead of a text stream. It then stores the value returned by fopen in the file pointer fp. If mode & ios_base::ate is nonzero and the file pointer is not a null pointer, the function calls fseek(fp, 0, SEEK_END) to position the stream at end-of-file. If that positioning operation fails, the function calls close(fp) and stores a null pointer in the file pointer.
    In 'r', the file must exist. It will not create it.

  3. #3
    Join Date
    Dec 2005
    Location
    Prague, Czech Republic
    Posts
    208

    Re: A file problem

    Also you don't have to call open explicitely if you don't need to, you can pass the filename and flags to constructor of ifstream object:

    Code:
      ifstream inFile("u.txt", ios::in);

  4. #4
    Join Date
    Jul 2006
    Posts
    24

    Thumbs down Re: A file problem

    But it happens I can't understand what is going on

  5. #5
    Join Date
    Aug 2005
    Posts
    478

    Re: A file problem

    Code:
    #include <fstream>
    #include <iostream>
    
    int main()
    {
    	std::ifstream in("thisdoesnotexist.txt");
    	if(!in)
    	{
    		std::cout << "Failed. If it now exists, then "
    			"please give compiler / OS details." << std::endl;
    	}
    
    	in.clear();
    	in.open("thisdoesnotexist.txt");
    
    	if(in)
    	{
    		std::cout << "That is so so wierd. Definitely tell " \
    			"us compiler / OS details" << std::endl;
    	}
    }
    You heard the man. Move out!

  6. #6
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: A file problem

    Quote Originally Posted by babyboomer
    The code below is supposed to open an existing file to read, but when you enter a nonexisting file it creates that file! Why does this happen and how can I solve this problem.
    Code:
     
    ifstream inFile;
     
    inFile.open("u.txt" );


    Thanks...
    To Calculator
    why Not main return 0 here on SuccessFully Completion .and Where you Close the File.

    Noe Come to Main Question
    Have a look on this Link to understand How to Do this in a Efficient Manner
    http://www.codeguru.com/forum/showpo...84&postcount=2

    Thanx
    Last edited by humptydumpty; July 30th, 2006 at 11:24 PM.

  7. #7
    Join Date
    Aug 2005
    Posts
    478

    Re: A file problem

    In C++, return 0 is not a requirement. Also, the ifstream's destructor is called automatically, which closes the file. His problem is that opening an ifstream that does not exist seems to create the file, which is not standard behavior.

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: A file problem

    Doing a small test under an old version of Visual C++ (ver 5),
    it looks like if you use:

    Code:
    #include <fstream.h>
    instead of the standard header

    Code:
    #include <fstream>
    The file is created, even if it does not exist. I think there is a no create
    option under fstream.h , but I do not remember what it is called.

    EDIT : found it ...

    Code:
    in.open("thisdoesnotexist.txt",ios::nocreate); // non-standard


    You should use the standard headers as shown by calculator.
    Last edited by Philip Nicoletti; July 31st, 2006 at 12:28 PM.

  9. #9
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: A file problem

    Quote Originally Posted by Calculator
    In C++, return 0 is not a requirement. Also, the ifstream's destructor is called automatically, which closes the file. His problem is that opening an ifstream that does not exist seems to create the file, which is not standard behavior.
    There is no hard and fast rule .but for writing a Program in a Efficient manner all of this is compulsory.have a look here
    Code:
    /* Include the necessary libraries for a basic program
    Because this program is intended for C++ ,you should include the conio.h library that has the getch() function. 
    */
     
    #include <iostream>
    #include <conio>
    using namespace std;
    #pragma hdrstop
    //---------------------------------------------------------------------------
    #pragma argsused
    /* Every C++ function must have a main() function
    The current C++ Standard recommends that the main() function 
    return a value. In this case, it will return an integer.
    The main() function is the entry point of the program.
    This version of main() takes two arguments. Sometimes the	 
    	arguments are not used at all, as is the case in this program.
    */
    int main(int argc, char* argv[])
    {
    	// Display a simple sentence to the user
    	 cout << "Welcome to the wonderful world of C++ Programming!!!\n";
    	 /* Because we are using C++, we will explicitly	 
    			prompt the user	to press an key in order to stop the 
    		 program. Upon pressing a key, we will call the getch() 
    		 function to receive it 
    	 */
    	 cout << "\nPress any key to continue...";
    	 getch();
    	 // The program went alright, so return 0
    	return 0;
    }

    Thanx

  10. #10
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: A file problem

    The real gap in the library is that you cannot open a file in one go to read and write, creating the file if it doesn't exist but not truncating it if it does.

    The only way to do that is open for append, then close then open again for read and write.

  11. #11
    Join Date
    Apr 2006
    Location
    Nr Cambridge, UK
    Posts
    263

    Re: A file problem

    humptydumpty: <conio.h> is a non-standard header (not usually available on Unix for example), and I've never come across <conio>. Don't use them in portable code. In addition, what does your posted code have to do with "writing a Program in a Efficient manner"?

  12. #12
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: A file problem

    agreed .that's a Mistake. Never use conio.h use conio .but i forget to mention above example is in case of borland C++.Anyway where you found <conio.h> in my code. Because i am unable to see it.

    Thanx
    Last edited by humptydumpty; August 2nd, 2006 at 12:52 AM.

  13. #13
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: A file problem

    Quote Originally Posted by humptydumpty
    Never use conio.h use conio
    That is the opposite.
    conio.h is supported by several compilers under Windows and DOS.

    Here, you rely on the fact that Borland C++ automatically adds ".h" if it is omitted and there is no matching file.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured