Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C++ /me gots it and needs a bit o help!

  • 10-06-2001 04:35PM
    #1
    Closed Accounts Posts: 822 ✭✭✭


    ok i just want to find out basic stuff..
    how to open a text file called "hi.txt"
    and how to delete it with the address something like : C:\windows\desktop\folders\hi.txt etc..

    any other useful pointers would be welcome..
    thanks wink.gif


Comments

  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    //Hi,

    //Ok to open a file in C/C++ do the following

    FILE openFile(char *filename)
    {
    FILE fp;
    if((fp = fopen(filename, "r+")) != NULL)
    return fp;
    else
    return NULL;
    }

    /* the above function will open a specified file for reading + writing if successful. To write to the file you can simply use
    */

    fprintf(fp, "My name is: %s", myName);

    // and to read

    fscanf(fp, "%s", input);

    /* All the above functions apart from my own are defined in <stdio.h>, I know that is an ANSI C & not C++ function library, but it doesn't matter it will work anyway.

    To delete a file is simple, all you have to do is call the remove() method defined in <stdio.h> and pass it the path to the file. NOTE: you probably already know this but just remember that the '\' character is an escape character and you will need "\\" to represent a '\' in C,C++ & Java.

    Hope that helps

    ;-phobos-) */
    */



    [This message has been edited by phobos (edited 10-06-2001).]


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    FILE address must be held in FILE pointers, and also the memory has to be new'd (in c++) in order that it doesn't get returned to the OS when the function ends. I was going to do a longer reply but I sneezed and hit the clear fields button by mistake so I cut it short, sorry if it appears *****y, it's not meant to.


  • Closed Accounts Posts: 822 ✭✭✭Kastro


    yup thanks a lot,
    very useful smile.gif
    1 thing i forgot is to ask how you shut down the puter..!


Advertisement