How is this for a basic file creation function?

Joined
Oct 23, 2023
Messages
6
Programming Experience
Beginner
C#:
void createFile() {
                /*
                 * This function creates 3 variables, fileName, an array called fileData, and finalFileData
                 * It asks the user to name the file it will create
                 * we then create a counter variable, it count the lines and position of the fileData array
                 * now, we loop asking the user to enter a line of file contents (and add a new line character to the end of the string)
                 * if the user adds a word containing "\EOF" (something that someone is unlikely to actually need to write)
                 * we assume that that is the end of the file, and break out of the loop
                 * and then we loop through the fileData array
                 * and insert each string to the end of the finalFileData variable
                 * we use this variable for use in the actual file creation
                 * Hopefully this isn't buggy as hell!
                 */
                string fileName;
                string[] fileData = { };
                string finalFileData = "";

                Console.Write("Enter the name of the file: ");
                fileName = Console.ReadLine();

                Console.Clear();
                int counter = 0;

                while (true) {
                    fileData[counter] = Console.ReadLine() + "\n";
                    if (fileData[counter].Contains("\\EOF")) {
                        break;
                    }
                    counter++;
                }


                for (int i = 0; i < fileData.Length; i++) {
                    finalFileData.Insert(finalFileData.Length - i, fileData[i]);
                }

                File.Create(fileName);
                File.WriteAllText(fileName, finalFileData);
                Console.Clear();
                Console.WriteLine("File created!");
                Console.ReadLine();
            }
 
Please do not just dump code in post.

The title of a thread is supposed to be a summary of your problem. In body of the post, provide a more detailed explanation of what you are trying to do, what problem you are running into, and what you have done to try to solve the problem, and then present the code that has the problem.

People in this forum will typically invest time in helping you if you invest time in asking your question, and making it as easy as possible for others to try to help you. If it feels like you don't really care, it's more likely that others won't care either.
 
That is actually a pretty terrible way to do things.

Why do you need to temporarily store all the input lines into an array? How do you know how big the array should be ahead of time (see your line 50). Why are not you allocating an array big enough? Probably should use a list instead of array.

If you know number of lines due to incrementing your counter variable, then your loop at line 33 should be based on that counter value. But then if you knew ahead of time how many lines you needed, then you can correctly use the array Length and not even need the counter. Or if you were simply using a list, then you would just use the Count of the list.

As you are getting the input, why are you using Contains("\\EOF"). What happens if the user enters "Hello, World \EOF"? Does that mean that you won't write out "Hello, World"?

But what a waste of memory: first you get all the lines into an array or a list, and then you turn around and append all the lines into a single string. So you now have two copies of the data: one in the array or list, and another in the the giant string.

Why waste all that energy and effort? Why not just write the user's input lines directly into the file? In pseudo code:
C#:
get filename
open a stream writer to the file
while true do
    get input line
    if input line is end of input special string
        break out of loop
    write input line to stream writer
close the stream writer
 
Please do not just dump code in post.

The title of a thread is supposed to be a summary of your problem. In body of the post, provide a more detailed explanation of what you are trying to do, what problem you are running into, and what you have done to try to solve the problem, and then present the code that has the problem.

People in this forum will typically invest time in helping you if you invest time in asking your question, and making it as easy as possible for others to try to help you. If it feels like you don't really care, it's more likely that others won't care either.

Oh okay, sorry for any trouble I caused. I think I partially misunderstood the purpose of the site, is there a way to remove posts?
 
You are in the right forum. The issue is that there is a certain level of netiquette that has to be followed when posting to a forum as opposed to posting to Instagram, Twitter, or other forms of social media. In programming forums, you are still expected to put a title that summarizes your issue, and then have a detailed explanation as well as the code. People in social media have gotten used to make the title like a caption, and then just splatting an image or text with very little explanation.
 

Latest posts

Back
Top Bottom