SomeRandomOnlineDev
Member
- 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();
}