Question regarding opening files

This is a discussion on Question regarding opening files within the C++ Programming forums, part of the General Programming Boards category; I'm trying to open a file to overwrite data on it but I'm getting some errors, here's my code Code: ...

  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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    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
    265
    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, 10: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, 10:46 AM

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