fstream not working

This is a discussion on fstream not working within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use fstream, but I can't get it to work. Nothing happens at all. This is what I ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    40

    fstream not working

    I'm trying to use fstream, but I can't get it to work. Nothing happens at all. This is what I have.
    Code:
    ...
    ofstream testFile("test.txt");
    testFile << "test";
    testFile.close();
    ...
    Is a new file supposed to be created if there isn't already one? When it wasn't working, I created a new file just in case, but that didn't help any.

    No file is created, and when I put a file there by that name the file doesn't get the output.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You are using a relative path. The directory that the file is created in depends on the current directory at the time you run your program. Try it with an absolute path (like "C:/text.txt"). If that works, then you just have to figure out which directory the file is being created in when it is relative.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    40
    Thanks much. Your debug idea got me to use the computer's search feature, and I ended up finding that the file was being created in the directory my compiler is in, since I was running the file from the compiler's program.

    When I run the program itself rather than telling the compiler to run it, it does save to the proper location. So apparently I don't have much of a problem at all; I'll just have to keep that folder open and run it from there from now on.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 09:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  3. using fstream
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2006, 03:53 AM
  4. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

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