9 Replies - 515 Views - Last Post: 11 January 2013 - 01:23 PM Rate Topic: -----

#1 Sahle123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-January 11

Problem. How do you create new files in a file.

Posted 10 January 2013 - 06:31 PM

This question has been bothering me for a while and I have not found any concrete examples or helpful answers as to do this. Currently, I am making a program that does very simple file manipulation.

I remember reading on a forum post on cplusplus.com about how to do this. The poster suggested using system(). Normally I would take that answer and work with it, but I've heard so many bad things about the system() function.

Would anyone be so kind enough to furnish me with an example?

Thank you. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Problem. How do you create new files in a file.

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3620
  • View blog
  • Posts: 11,276
  • Joined: 05-May 12

Re: Problem. How do you create new files in a file.

Posted 10 January 2013 - 06:46 PM

We don't even know what you are trying to do, so how can we furnish you an example?

My take on this is that if the file manipulation is so simple, then do it using straight C or C++. There is no need to take on the overhead of using system() and taking on the additional dependency that the program, batch file, or shell script that you intended to call using system() exists.
Was This Post Helpful? 0
  • +
  • -

#3 Sahle123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-January 11

Re: Problem. How do you create new files in a file.

Posted 10 January 2013 - 06:55 PM

Maybe this might help then.

void check_directory()
{
     fstream file_directory;
     string default_directory("C:/Users/");

     file_directory.open(default_directory.c_str()), std::ios::in | std::ios::out);

     if(file_directory.is_open())

}


Sorry, I hit enter on accident and sent that one early...

This post has been edited by macosxnerd101: 10 January 2013 - 06:58 PM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3620
  • View blog
  • Posts: 11,276
  • Joined: 05-May 12

Re: Problem. How do you create new files in a file.

Posted 10 January 2013 - 07:01 PM

Are you trying to see if a file or directory exists?
Was This Post Helpful? 0
  • +
  • -

#5 Sahle123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-January 11

Re: Problem. How do you create new files in a file.

Posted 10 January 2013 - 07:06 PM

Maybe this might help then.

void check_directory()
{
fstream file_directory;
string user_temp;
stirng default_directory("C;/Users/users_directory.txt");

file_directory.open(default_directory.c_str()), std::ios::in | std::ios::out);

if(file_directory.is_open())
{
file_directory >> WORKING_DIR;

/* If the file is empty */
if(WORKING_DIR == "")
{
cout << "Please type the FULL path of where you want your files to be stored." << endl;
getline(cin, WORKING_DIR);
file_directory << WORKING_DIR;
}
}
file_directory.close();
}



I want to make so that this function will read from a certain spot (C:\Users\users_directory.txt) and from the txt file, get the file of where the user wants to save his files. After that, for the remainder of the program, all files will be saved in that specified place.

I hope that was clear and made full sense.

Also, My last post was a mistake, I didn't put in my full code.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3620
  • View blog
  • Posts: 11,276
  • Joined: 05-May 12

Re: Problem. How do you create new files in a file.

Posted 10 January 2013 - 07:43 PM

I think it's just a matter of cleaning up the code.

string PromptForUserDirectory()
{
    string dirName;

    cout << "Please type the FULL path of where you want your files to be stored." << endl;
    getline(cin, dirName);
    return dirName;
}

void EnsureUserDirectory()
{
    fstream dirFile("C:/Users/users_directory.txt");

    if (dirFile)
    {
        string workingDir;

        dirFile >> workingDir;
        if (workingDir.length() == 0)
        {
            dirFile << PromptForUserDirectory();
        }
    }
}



Let the constructor do the open.
No need for the constant string that you were just going to call c_str() from anyway.
Use the default parameters for the file open mode, no need to pass them in.
Use the ! operator to check if the file opened successfully.
Break out the prompting for the user directory.
No need to call close() since the destructor will take care of it.

This post has been edited by Skydiver: 10 January 2013 - 07:44 PM

Was This Post Helpful? 1
  • +
  • -

#7 Sahle123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-January 11

Re: Problem. How do you create new files in a file.

Posted 11 January 2013 - 09:35 AM

Oh. Thank you for cleaning up my code a bit :) The style does look nicer, but I am still faced with the same problem: How do you explicitly create a file?

I know that when you use to constructor for fstream, the file is created, but for some strange reason, when I call fstream and give it both ios::in and ios::out, the file never seems to be created...

In layman's terms: Can a I create a file without using the fstream constructor or fstream.open()?
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3620
  • View blog
  • Posts: 11,276
  • Joined: 05-May 12

Re: Problem. How do you create new files in a file.

Posted 11 January 2013 - 10:07 AM

Basically the default flags for fstream doesn't let you create a file if it doesn't exist. You'll need to first try read access (using ios::in). If the file fails to open, then the file doesn't exist, so you'll need to re-open using ios::out.

If you used C fopen() instead of C++ fstream's, you would have more alternatives.

This post has been edited by Skydiver: 11 January 2013 - 10:09 AM

Was This Post Helpful? 1
  • +
  • -

#9 Sahle123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-January 11

Re: Problem. How do you create new files in a file.

Posted 11 January 2013 - 01:16 PM

Lastly, sorry to keep bringing this up,

is it possible to create a new folder with fstream?

This post has been edited by Sahle123: 11 January 2013 - 01:16 PM

Was This Post Helpful? 0
  • +
  • -

#10 Sahle123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-January 11

Re: Problem. How do you create new files in a file.

Posted 11 January 2013 - 01:23 PM

A-hah, I have found out that their is a library for this (direct.h) and you use the function mkdir() to create it.

Forget my last post then. This will do for me :)

Thank you again Skydiver for the hasty replies.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1