Thread: Question regarding opening files

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    55

    Question regarding opening files

    I'm trying to open a file to overwrite data on it but I'm getting some errors, here's my code

    Code:
            ifstream file("asdf.txt"); 
    	fstream file2("qwerty.txt", fstream::trunc); 
    
    	if(!file)
    		cout << "File not found";
    	if(!file2)
    		cout << "File not found";
    I was using the mode app, and it worked just fine, but I wanna overwrite on the file so I tried using trunc mode and I keep getting File(2) not found, help?

    EDIT: I found out that I can overwrite by not adding a second parameter, but I would still like to know why trunc is not working
    Last edited by thefeedinghand; 09-05-2010 at 12:24 PM.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Edit: messaged in error.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Just using the out mode should truncate the file if it exists, and create it if it doesn't (I'm pretty sure)

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    ios::trunc has always worked for me. Besides, I believe if you don't specify a mode, it just creates the file anyway.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    68
    try dat..
    Code:
         ifstream file("asdf.txt"); 
    	ofstream file2("qwerty.txt", ios::trunc); 
    
    	if(!file)
    		cout << "File not found";
    	if(!file2)
    		cout << "File not found";

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    12
    Quote Originally Posted by smasherprog View Post
    try dat..
    Code:
         ifstream file("asdf.txt"); 
    	ofstream file2("qwerty.txt", ios::trunc); 
    
    	if(!file)
    		cout << "File not found";
    	if(!file2)
    		cout << "File not found";
    +1
    perfect solution

    will work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem opening blank files with ifstream.
    By Sclorch in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2009, 11:43 AM
  2. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 06:55 AM
  3. Notepad++ opening ActionScript files
    By sean in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-26-2008, 03:49 PM
  4. Beginner Question: Multiple source files
    By ironfistchamp in forum C++ Programming
    Replies: 8
    Last Post: 07-16-2006, 02:19 PM
  5. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM