Is this the way to create a new file?

  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    File
Click For Summary

Discussion Overview

The discussion revolves around the creation and management of files in C++ programming, specifically focusing on how to create a file only if it does not already exist, and how to write and read strings from files. Participants explore various methods and share code snippets to illustrate their points.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes a method to check for file existence by attempting to open it in input mode and creating it in output mode if it fails.
  • Another participant suggests using ios::app to ensure that data is appended to the file without overwriting existing content.
  • There is a discussion about the failbit and goodbit flags in file streams, indicating whether a file operation succeeded or failed.
  • Some participants inquire about alternative methods for writing std::strings to files, expressing confusion over the use of the streaming operator and the limitations of getline with output streams.
  • One participant compares C++ file handling with VB.NET, mentioning a function that checks for file existence.
  • Concerns are raised about the behavior of input and output operations with std::string and c-strings, with participants sharing their experiences and observations.

Areas of Agreement / Disagreement

There is no clear consensus on the best method for file creation and management, as participants present various approaches and express differing opinions on the effectiveness and safety of these methods.

Contextual Notes

Participants note limitations in their understanding of C++ file handling, particularly regarding the differences between std::string and c-strings, and the behavior of input/output operations. There are also mentions of unresolved issues with specific code snippets and functions.

Who May Find This Useful

This discussion may be useful for C++ programmers looking to understand file handling, particularly those interested in creating files conditionally and managing string data in files.

  • #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   Reactions: 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
6K
  • · Replies 65 ·
3
Replies
65
Views
7K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K