File Input/Output Path Directory

This is a discussion on File Input/Output Path Directory within the C++ Programming forums, part of the General Programming Boards category; Hmm i can't believe I'm having problems with this... but anyways: void Tmain_form::write_to_file() { ofstream filename("chris.txt"); //opens file chris.txt filename ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    Question File Input/Output Path Directory

    Hmm i can't believe I'm having problems with this...

    but anyways:

    void Tmain_form::write_to_file()
    {
    ofstream filename("chris.txt"); //opens file chris.txt
    filename << "A"; //writes 'A' to chris.txt
    filename.close(); //closes file.
    }

    code above saves the letter 'A' to a file named 'chris.txt'.
    But it saves the file 'chris.txt' in the same directory where the .cpp file is saved.

    i want to be able to declare the path like below:

    void Tmain_form::write_to_file()
    {
    ofstream filename("c:\program files\chris.txt");
    filename << "A";
    filename.close();
    }

    but the above code doesn't work. It just saves in the dir where the .cpp is.... I've also tried:

    void Tmain_form::write_to_file()
    {
    AnsiString path = "c:\program files\chris.txt";
    ofstream filename(path);
    filename << "A";
    filename.close();
    }

    but i get an error cause i'm guessing the function

    ofstream filename();

    can't pass an AnsiString variable.. but i tried char and that doesn't work either... anyone help me out with this?

  2. #2
    Unregistered
    Guest
    Try this;

    ofstream filename("c:\\program files\\chris.txt");

  3. #3
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230

    or this...

    or try this:
    Code:
    ofstream filename("c://program files//chris.txt");  // Forward slashes
    That should work

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    thanx man that was the problem

    Thanks yah fixed the problem... forgot all about the backslash being a character escape key....

    one more question for anyone who can answer it...

    void Tmain_form::write_to_file()
    {
    ofstream filename("c:\\chris.txt");
    filename << "A";
    filename.close();
    }

    works but.....

    void Tmain_form::write_to_file()
    {
    AnsiString path = "c:\\chris.txt";
    ofstream filename(path);
    filename << "A";
    filename.close();
    }

    i've tried declaring path as type 'String', 'Char', 'AnsiString'
    and none seem to work... something i'm doing wrong?

    i also tried cause i running outta ideas:

    void Tmain_form::write_to_file()
    {
    AnsiString path = "c:\\chris.txt";
    ofstream filename("\"" + path + "\"");
    filename << "A";
    filename.close();
    }

  5. #5
    Unregistered
    Guest
    the filename passed to a stream needs to be a c_style string (that is a null terminated char array) OR something that is automatically converted into a c_style string. The latter requires that the () operator (aka conversion operator) be overloaded for the string class you are trying to use. the string class in STL has such an operator. The other string class probably don't.

    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
    string filename = "c:\\Borland\\myProgram\\data.txt"
    ofstream fout(filename);
    //etc.
    }

    the above should work.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    hmmm

    like this right?

    void Tmain_form::write_to_file()
    {
    String file = "c:\\bob.txt";
    ofstream filename;
    filename.open(file);
    filename << "A";
    filename.close();
    }

    how come i get errors:

    [C++ Error] Functions.h(64): E2034 Cannot convert 'string' to 'const char *'

    [C++ Error] Functions.h(64): E2342 Type mismatch in parameter 's' (wanted 'const char *', got 'string')

    i'm new to programming so i'm not quite sure how to fix this error..

  7. #7
    Unregistered
    Guest
    the only string class that I know for sure has the automatic conversion is the string class in STL. If you have the line

    #include <string>

    in the list of includes, then you should be using the STL string class. If you don't have that line or if you are using any other string class then the automatic conversion is up for grabs. I do know that almost every string class I have encountered has a member function to retrieve the c_style string upon which it is based. Often the function looks something like this:

    stringInstance.c_str();

    If String is a typedef for AnsiString then the c_str() function should be available, eventhough I don't about automatic conversion. Here's how to use the c_str() method.

    AnsiString filename = "data.txt";
    ofstream fout;
    fout(filename.c_str());

    Without knowing where the String class is coming from it is hard to say exactly why it isn't working.

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    wa whoo oh baby

    ah that last post hit the spot... thnx... exactly what was needed.. you just saved me hours of frusteration....

    c_str() function call was the hero thnx again....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM

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