Is this the way to create a new file?

  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    File
Click For Summary
To create a file only if it does not exist, the program should first attempt to open the file in input mode using ios::in. If the operation fails, indicating the file does not exist, it can then be opened in output mode with ios::out to create the file. Using ios::app can help prevent overwriting existing data by appending to the file instead. It's important to check the failbit state flag to determine if the file was successfully opened. The discussion also touches on using different methods to read and write std::strings and the need for careful handling of file operations to avoid race conditions.
  • #31
Vanadium 50 said:
What is very good is that the constructor cannot fail. This is not true if the constructor is opening and reading files. Why does this matter? Because a constructor can't return a status word saying "Hey, there was a problem and I wasn't even created." One needs to throw an exception, and the OP is not got that far. Normally, one would say "do it this way now, and you'll see why this is a good habit to get into after a few chapters", but the OP is tempermentally unsuited to accept this kind of advice - indeed, has rejected it.
It is hard to take any advice when someone never actually advice, only using sarcasm and put down remark of me to someone else instead directly to me.
 
Last edited:
  • Skeptical
Likes jbriggs444
Technology news on Phys.org
  • #32
pbuk said:
An example of something you should be doing in the constructor is storing the name of the file you want to work with - at the moment you have this hard coded in 3 different places in your code. Instead I would do something like this:

C:
class Directory {

  private:
    // The name (actually the path) of the file to store the directory.
    std::string _filename = "Directory.dat";

  public:
    // Constructors.
    Directory();
    Directory(std::string &filename);
}
And then your main can do something like this:
C:
int main() {
  // Use the default file.
  Directory directory = Directory();

  // Use a different file for testing.
  // @TODO set this from a command line argument.
  Directory directory = Directory(std::string("test-directory.dat"));

  directory.open();
  directory.add(std::string("John Doe"), std::string("+1-202-555-1234"));
  directory.sort();
  directory.save();
}
Thanks for the advice.

So far, what I am doing is only the bare backbone of the program, I might even use literal array[31] just be lazy to start and fix it later. I actually have plan to use char fileAr[]="file.dat" or even char[] fileAr="C:\\desktop\\myFolder\\file.dat" like you described so when in main(), I actually pass as argument to the Specification file to open different files and read the file called by the main program. That is the member function can open different files, in different directories given by the main program. That is the member function is portable to be used in other programs for totally different purpose. All I have to specify is the name of the file, the data type ( different ADT) and what vector to store in. The member function will open the specified file, store and return the content in the vector.

But I am long ways off from this. So far, I am happy to nail down the back bone, that is using the Specification file to open file, store in vector, I can add elements. The rest should be easier.

Thanks
 
  • #33
yungman said:
It is hard to take any advice when someone never actually advice, only using sarcasm and put down remark of me to someone else instead directly to me.

First, the only proper reaction to that is not suitable for a family forum.

Second, let's take a look at past ignored advice:

Message 27 in Questions on Vectors.
Message 29 in Questions on Vectors.
Message 31 in Questions on Vectors.
Message 37 in Questions on Vectors.
Message 46 in Questions on Vectors.
Message 6 in Help with C++ On Strings
Message 12 in Help with C++ On Strings
Message 13 in Help with C++ On Strings
Message 15 in Help with C++ On Strings
Message 22 in Help with C++ On Strings
Message 28 in Help with C++ On Strings
Message 49 in Help with C++ On Strings
Message 63 in Help with C++ On Strings
Message 66 in Help with C++ On Strings
Message 114 in Help with C++ On Strings
Message 115 in Help with C++ On Strings
Message 116 in Help with C++ On Strings
Message 117 in Help with C++ On Strings

Some are mine, some are other people's. I could go on, but I think that makes the point well enough.
 
  • #34
Vanadium 50 said:
First, the only proper reaction to that is not suitable for a family forum.

Second, let's take a look at past ignored advice:

Message 27 in Questions on Vectors.
Message 29 in Questions on Vectors.
Message 31 in Questions on Vectors.
Message 37 in Questions on Vectors.
Message 46 in Questions on Vectors.
Message 6 in Help with C++ On Strings
Message 12 in Help with C++ On Strings
Message 13 in Help with C++ On Strings
Message 15 in Help with C++ On Strings
Message 22 in Help with C++ On Strings
Message 28 in Help with C++ On Strings
Message 49 in Help with C++ On Strings
Message 63 in Help with C++ On Strings
Message 66 in Help with C++ On Strings
Message 114 in Help with C++ On Strings
Message 115 in Help with C++ On Strings
Message 116 in Help with C++ On Strings
Message 117 in Help with C++ On Strings

Some are mine, some are other people's. I could go on, but I think that makes the point well enough.
Oh yeh, I have things to say definitely NOT suitable for this forum. Out of respect to this forum AND the people that actually giving a lot of time to help me ( NOT YOU), I don't want to say. I don't think either one of us want to go down this road.

I think it's better you put me on ignore, not asking you to help me, just don't mention me, and I stay out of your way. This is an education forum, this forum is better than this.
 
Last edited:

Similar threads

  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 70 ·
3
Replies
70
Views
5K
Replies
10
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
1
Views
5K
  • · Replies 65 ·
3
Replies
65
Views
7K
  • · Replies 32 ·
2
Replies
32
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 12 ·
Replies
12
Views
2K