File

This is a discussion on File within the C++ Programming forums, part of the General Programming Boards category; Hi I want to open a file like: FILE *fp; fp=fopen("c:\leite.txt","w+"); My question is if i want to open the ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    43

    File

    Hi
    I want to open a file like:

    FILE *fp;
    fp=fopen("c:\\leite.txt","w+");

    My question is if i want to open the file like this;

    Ansistring FileName;
    FileName="leite.txt"

    fp=fopen("c:\\"+FileName,"w+");

    Something like this, i want to use outside AnsiString variable and then use it on fopen how i can do it???

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Well with a std::string, it would be

    std::string filename = "file.txt";
    std::string = "c:\\" + filename;
    fp = fopen( filename.c_str(), "w+" );

    Does your ansiString come with a suitable method to return a const char * equivalent?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    43
    Hi
    i did something like this but dont work:

    FILE *fp;

    AnsiString FileName;

    FileName=Edit1->Text;
    FileName="c:\\"+FileName;

    fp=fopen(FileName.c_str(),"w+");
    fclose(fp);

    What i have to change??? Help..... Do i have to use AnsiString and make it Char???

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Leite33
    Hi
    i did something like this but dont work:
    be more specific

    what interface AnsiString provides?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > Does your ansiString come with a suitable method to return a const char * equivalent?
    Hint - this means you should go RTFM and find out what the API of ansiString is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    It's been a while since I used C++ Builder, but I believe AnsiString provided the c_str() function which returns a const char *. You shuold be able to press F1 and check what functions it provides to get a const char * back.

    If that isnt the problem then you have to be more clear about what "doesnt work".
    Just saying something doesnt work helps us nothing. it doesnt compile? the program crashes? no file is generated? what?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 09:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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