creating empty txt file - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

creating empty txt file

sufs_LNDsufs_LND Posts: 23Member
How do I create an empty txt file containing 0Kb? Or how do I clear one txtfile so that it contains 0 kb?

Comments

  • MT2002MT2002 Posts: 1,444Member
    [b][red]This message was edited by MT2002 at 2007-3-19 19:59:17[/red][/b][hr]
    : How do I create an empty txt file containing 0Kb? Or how do I clear one txtfile so that it contains 0 kb?
    : [blue]
    Just open it:[/blue][code]
    std::fstream file ("MyFile.txt");[/code]
    [blue]
    If the file exists, it will be opened. If it doesnt exist,
    Windows will create it for you.

    If you open the file, then close it (Without writing anything to
    it), the file is effectively cleared :-)

    *edit: Fixed end tag[/blue]
  • sufs_LNDsufs_LND Posts: 23Member
    : : How do I create an empty txt file containing 0Kb? Or how do I clear one txtfile so that it contains 0 kb?
    : : [blue]
    : Just open it:[/blue][code]
    : std::fstream file ("MyFile.txt");[/code]
    : [blue]
    : If the file exists, it will be opened. If it doesnt exist,
    : Windows will create it for you.
    :
    : If you open the file, then close it (Without writing anything to
    : it), the file is effectively cleared :-)[blue]
    :
    Im trying to create a program which creates empty .txt files like this 1.txt 2.txt 3.txt 4.txt etc..

    But I cant get the loop to work. Any idea?
  • sufs_LNDsufs_LND Posts: 23Member
    : : : How do I create an empty txt file containing 0Kb? Or how do I clear one txtfile so that it contains 0 kb?
    : : : [blue]
    : : Just open it:[/blue][code]
    : : std::fstream file ("MyFile.txt");[/code]
    : : [blue]
    : : If the file exists, it will be opened. If it doesnt exist,
    : : Windows will create it for you.
    : :
    : : If you open the file, then close it (Without writing anything to
    : : it), the file is effectively cleared :-)[blue]
    : :
    : Im trying to create a program which creates empty .txt files like this 1.txt 2.txt 3.txt 4.txt etc..
    :
    : But I cant get the loop to work. Any idea?
    :

    Thx for the help,
    it worked with the follwoing code:
    [code]
    int main()
    {
    int teller = 0;
    while (teller <= 400) {
    char file[10];
    cout << teller;
    teller++;
    sprintf( file, "%d", teller);
    char fileending[5]=".txt";
    char filename[10];
    strcpy (filename,file);
    strncat (filename,fileending,5);

    ifstream myfile(filename, ios::out);
    }
    }
    [/code]

  • MT2002MT2002 Posts: 1,444Member
    : : : : How do I create an empty txt file containing 0Kb? Or how do I clear one txtfile so that it contains 0 kb?
    : : : : [blue]
    : : : Just open it:[/blue][code]
    : : : std::fstream file ("MyFile.txt");[/code]
    : : : [blue]
    : : : If the file exists, it will be opened. If it doesnt exist,
    : : : Windows will create it for you.
    : : :
    : : : If you open the file, then close it (Without writing anything to
    : : : it), the file is effectively cleared :-)[/blue]
    : : :
    : : Im trying to create a program which creates empty .txt files like this 1.txt 2.txt 3.txt 4.txt etc..
    : :
    : : But I cant get the loop to work. Any idea?
    : :
    :
    : Thx for the help,
    : it worked with the follwoing code:
    : [code]
    : int main()
    : {
    : int teller = 0;
    : while (teller <= 400) {
    : char file[10];
    : cout << teller;
    : teller++;
    : sprintf( file, "%d", teller);
    : char fileending[5]=".txt";
    : char filename[10];
    : strcpy (filename,file);
    : strncat (filename,fileending,5);
    :
    : ifstream myfile(filename, ios::out);
    : }
    : }
    : [/code]
    [blue]
    While the code is correct, its design could be better :-)
    The code isnt C++, Its a mixture of C and C++ (or C+) Which is
    coinsidered bad practice.

    I recommend taking a look at std::string and getting rid of
    sprintf(), if possible.

    Also, main [b]must[/b] return a value (usually 0 for success)
    to be ANSI compilient.

    Just some ideas :-)[/blue]
    [/blue]
  • LundinLundin Posts: 3,711Member
    : : : : : How do I create an empty txt file containing 0Kb? Or how do I clear one txtfile so that it contains 0 kb?
    : : : : : [blue]
    : : : : Just open it:[/blue][code]
    : : : : std::fstream file ("MyFile.txt");[/code]
    : : : : [blue]
    : : : : If the file exists, it will be opened. If it doesnt exist,
    : : : : Windows will create it for you.
    : : : :
    : : : : If you open the file, then close it (Without writing anything to
    : : : : it), the file is effectively cleared :-)[/blue]
    : : : :
    : : : Im trying to create a program which creates empty .txt files like this 1.txt 2.txt 3.txt 4.txt etc..
    : : :
    : : : But I cant get the loop to work. Any idea?
    : : :
    : :
    : : Thx for the help,
    : : it worked with the follwoing code:
    : : [code]
    : : int main()
    : : {
    : : int teller = 0;
    : : while (teller <= 400) {
    : : char file[10];
    : : cout << teller;
    : : teller++;
    : : sprintf( file, "%d", teller);
    : : char fileending[5]=".txt";
    : : char filename[10];
    : : strcpy (filename,file);
    : : strncat (filename,fileending,5);
    : :
    : : ifstream myfile(filename, ios::out);
    : : }
    : : }
    : : [/code]
    : [blue]
    : While the code is correct, its design could be better :-)
    : The code isnt C++, Its a mixture of C and C++ (or C+) Which is
    : coinsidered bad practice.
    :
    : I recommend taking a look at std::string and getting rid of
    : sprintf(), if possible.
    :
    : Also, main [b]must[/b] return a value (usually 0 for success)
    : to be ANSI compilient.
    :
    : Just some ideas :-)[/blue]
    : [/blue]
    :


    I think it is a bit harsh to call it bad practice. Sure sprintf() is bad practice, in both C and C++, since it is a sluggish function which is only motivated in case of float numbers.
    But strcpy() and strcat() have no equivalent functions in C++. The string class is only an option if dynamic allocation is acceptable. The OP doesn't mention his platform so we don't know that.
Sign In or Register to comment.