I recently started learning c# So I got a school assignment to make a German grammar-help-tool. It's a group assignment my task is:
The application loads prefabricated sentences from a file, and randomly makes small changes to these sentences. For example, commas can be deleted, ies
can be replaced by i
, s
can be replaced by ss
, and so on. The user is presented with the changed sentence, and it is up to him to find the error.
I tried using the string. Replace and streamReader/Writer, but it didn't work.
StreamReader reader = new StreamReader(File.OpenRead(@"C:\Users\kamil\OneDrive\Dokumente\test grammatik.txt"));
string fileContent = reader.ReadToEnd();
reader.Close();
fileContent = fileContent.Replace("tier", "aff");
StreamWriter writer = new StreamWriter(File.OpenWrite("C:\test grammatik.txt"));
writer.Write(fileContent);
writer.Close();
the sentance in the txt file is: "Ein grosses Tier" and I wanted to replace the "Tier" with "Aff" and it's what i expected and I got:
System.IO.IOException: 'The filename, directory name, or volume label syntax is incorrect. : 'C:\Users\kamil\source\repos\test\bin\Debug\netcoreapp3.1\ est grammatik.txt''
in the Line:
StreamWriter writer = new StreamWriter(File.OpenWrite("C:\test grammatik.txt"));
[link to my file]https://www.dropbox.com/s/p0r3us5o0ik0oju/test.zip?dl=0
CodePudding user response:
The issue is with this line
StreamWriter writer = new StreamWriter(File.OpenWrite("C:\test grammatik.txt"));
you need to either make "C:\\test grammatik.txt"
or @"C:\test grammatik.txt"
I would not either suggest you write directly to c: root, you might end up getting permission issues, therefore make a temp folder to work on.
In addition as @Panagiotis Kanavos mentioned in a comment, a single backslash is treated as a tab, you can overcome this by a double backslash or adding @ symbol as mentioned in my answer.
CodePudding user response:
The replace function asks for chars, not strings
public String Replace(char oldChar, char newChar);
in most languages strings are defined with " ", in the other hand chars use ' '
Good luck with your hw