Wrong encoding, output file gives � instead of normal char - c#

I want to make a program that searches a file for desired chars in words (letters č ć ž š), replaces them with c z s etc. and saves the file. In my attempt, however, I get some stupid signs, so that means it opens the file wrongly. When I try to add encoding.unicode it gives me errors (shown below). And one more question, how do I make a program which opens files by dragging them in an .exe file.
Error 3 The best overloaded method match for
'System.IO.File.Open(string, System.IO.FileMode,
System.IO.FileAccess)' has some invalid
arguments C:\Users\Vulisha\AppData\Local\Temporary
Projects\ConsoleApplication1\Program.cs 14 59 ConsoleApplication1
Error 4 Argument '3': cannot convert from 'System.Text.Encoding' to
'System.IO.FileAccess' C:\Users\Vulisha\AppData\Local\Temporary
Projects\ConsoleApplication1\Program.cs 14 122 ConsoleApplication1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (StreamReader stream = new StreamReader(File.Open(#"C:\Users\Vulisha\Desktop\titl.txt", FileMode.Open)))
{
string fileText = stream.ReadToEnd();
// Do your replacements
fileText = fileText.Replace(#"č", #"c");
fileText = fileText.Replace(#"ć", #"c");
fileText = fileText.Replace(#"š", #"s");
fileText = fileText.Replace(#"ž", #"z");
fileText = fileText.Replace(#"đ", #"d");
fileText = fileText.Replace(#"Č", #"C");
fileText = fileText.Replace(#"Č", #"C");
fileText = fileText.Replace(#"Š", #"S");
fileText = fileText.Replace(#"Ž", #"Z");
fileText = fileText.Replace(#"Đ", #"D");
using (StreamWriter writer = new StreamWriter(File.Open(#"titl.txt", FileMode.Create)))
{
// You do a create because the new file will have less characters than the old one
writer.Write(fileText);
}
}
}
}
}

You need to be more careful with placement of parentheses. You need
new StreamWriter(File.Open(#"titl.txt", FileMode.Create), Encoding.Unicode)
but you wrote
new StreamWriter(File.Open(#"titl.txt", FileMode.Create, Encoding.Unicode))
See the difference?

Related

Read a file into a string in C#

I am trying to read a file into a string which then I will send to another application.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FileApplication
{
class Program
{
static void Main(string[] args)
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("c:\text.txt"))
{
string line;
// Read and display lines from the file until
// the end of the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
}
I am getting the error:
The file could not be read:
Could not find file 'c:\users\andymarkmn\documents\visual studio 2015\Projects\FileApplication\FileApplication\bin\Debug\text.txt'.
I have tried putting the file in bin debug folders as well.
How to make sure the code works ?
EDIT: As suggested, I tried using the different ways.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FileApplication
{
class Program
{
static void Main(string[] args)
{
string filepath = "c:\\text.txt";
try
{
string lines = File.ReadAllText(filepath);
Console.Write(lines);
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
}
I still get the error:
The file could not be read:
Could not find file 'c:\text.txt'.
You have accidentally used an unwanted escape sequence in your filename string.
new StreamReader("c:\text.txt")
should be
new StreamReader(#"c:\text.txt")
Otherwise \ gets treated as an escape character, at \t is a tab character. This leaves an unexpected result, and the wrong path for the file.
# instructs the compiler to ignore any escape characters in the string.
"c:\text.txt" will not work as \ is an escape character.
use #"c:\text.txt" or "c:\\text.txt"
When StreamReader is given a non-qualified path as a parameter, it will look for the file in the application's working directory as you have done:
using (StreamReader sr = new StreamReader("c:\text.txt"))
If the file isn't located there, probably you should give StreamReader a fully qualified path:
using (StreamReader sr = new StreamReader(#"c:\text.txt"))
//or...
using (StreamReader sr = new StreamReader("c:\\text.txt"))
Try:
using (StreamReader sr = new StreamReader(#"C:\text.txt"))
If you use c:\text, C# considers that the string has a tabulador between C: and text.txt.

How to store the file in a string

I am trying to store this file in a string.but i don know how to do it??can you
please help me.This is my program
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace Sample_Program_For_CC_ utility
{
class Program
{
static void Main(string[] args)
{
TextReader tr = new StreamReader(#"C://Users//Darts//Desktop//sample//00004.txt");
Console.WriteLine(tr.ReadToEnd());
tr.Close();//
}
}
}
It's not really clear what's going wrong with the code you've given, but it's simpler to use File.ReadAllText. For eaxmple:
string file = #"C:\Users\Darts\Desktop\sample\00004.txt"
string text = File.ReadAllText(file);
You could just do:
string text = System.IO.File.ReadAllText(#"C://Users//Darts//Desktop//sample//00004.txt");
See http://msdn.microsoft.com/en-us/library/ezwyzy7b.aspx
string s = System.IO.File.ReadAllText( yourPathAndFile );
you can use ReadAllText
string fileContents = File.ReadAllText("c:\\filename.txt")
btw. you don't need # if you are using double backslash, and you have to use backslash and not slash
String theString = tr.ReadToEnd();
As .ReadToEnd() returns a string.
You can load the file contents in a string as follows.
string fileContents = tr.ReadToEnd();
FileStream FS = new FileStream(#"C://Users//Darts//Desktop//sample//00004.txt", FileMode.Open, FileAccess.Read);
TextReader tr = new StreamReader(#"C://Users//Darts//Desktop//sample//00004.txt");
Console.WriteLine(tr.ReadToEnd());
tr.Close();

C# letters like å not correctly shown in output file

I work in C# and this is my code:
Encoding encoding;
StringBuilder output = new StringBuilder();
//somePath is string
using (StreamReader sr = new StreamReader(somePath))
{
string line;
encoding = sr.CurrentEncoding;
while ((line = sr.ReadLine()) != null)
{
//make some changes to line
output.AppendLine(line);
}
}
using (StreamWriter writer = new StreamWriter(someOtherPath, false))//encoding
{
writer.Write(output);
}
In the file which is on somePath, I have Norwegian characters like å. But, on the file in someOtherPath I get question marks instead of them. I think it's an encoding problem, so I tried getting the input file encoding and to grant it to the output file. It had no results. I tried opening the file with Google Chrome and grant it every possible encoding but the letters weren't the same as in the input file.
StreamReader can only make guesses with regards to certain encodings. Ideally, you should find out what the encoding of the file really is, then use that to read the file. What created the file, and what allows you to read it correctly? Does the latter program expose which encoding it's using? (For example, it may be using something like Windows-CP1252.)
I would personally recommend using UTF-8 as your output encoding if you can, but it depends on whether you're in control over whatever's then reading the output.
EDIT: Okay, now I've seen the file, I can confirm it's not UTF-8. The word "direktør" is represented as these bytes:
64 69 72 65 6b 74 f8 72
So the non-ASCII character is a single byte (F8) which is not a valid UTF-8 representation of a character.
It could be ISO-Latin-1 - it's not clear (there are multiple encodings which would match). If it is, you can use:
Encoding encoding = Encoding.GetEncoding(28591);
using (TextReader reader = new StreamReader(filename, encoding))
{
...
}
(Alternatively, use File.ReadAllLines to make life simpler.)
You'll need to separately work out what output encoding you want.
EDIT: Here's a short but complete program which I've run against the file you provided, and which has correctly converted the character to UTF-8:
using System;
using System.IO;
using System.Text;
class Test
{
static void Main()
{
Encoding encoding = Encoding.GetEncoding(28591);
StringBuilder output = new StringBuilder();
using (TextReader reader = new StreamReader("file.html", encoding))
{
string line;
while ((line = reader.ReadLine()) != null)
{
output.AppendLine("Read line: " + line);
}
}
using (StreamWriter writer = new StreamWriter("output.html", false))
{
writer.Write(output);
}
}
}
Try this case to save your text:
using (StreamWriter writer = new StreamWriter(someOtherPath, Encoding.UTF8)) { ... }

Issue in writing special characters to Excel

I have a few reports that are exported to Excel. The problem is whereever there are special characters, it is being replaced by some funny symbols
For example, '-'(hyphen) was replaced by –...
Any help to solve the problem??
The most straight forward way is to encode the text file as UTF-8. I ran the following code, opened the resulting hyphen.txt file in Excel 2007 and it worked as expected:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var hyphen = "\u2010\r\n";
var encoding = Encoding.UTF8;
var bytes = encoding.GetBytes(hyphen);
using (var stream = new System.IO.FileStream(#"c:\tmp\hyphen.txt", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite))
{
stream.Write(bytes, 0, bytes.Length);
}
}
}
}
This is the code -- view at PasteBin.

How to append text to multiple files

I am not a programmer, but I am a researcher and I need to modify some files.
I have a number of text files with *.mol extension located in c:\abc\ directory . I need to append line containing following text "M END" to each file in this list. I tried following in C# but without any result:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
StreamWriter sw = new StreamWriter("c:\\abc\\*.mol", true);
sw.WriteLine("M END");
sw.Close();
}
}
}
Please, suggest the solution.
Thank You!
Would you be satisfied with this oneliner that you can put in any DOS batch (.bat) file:
FOR %%I IN (c:\abc\*.mol) DO ECHO M END>>%%I
foreach (string fileName in Directory.GetFiles("directory", "*.mol"))
{
File.AppendAllText(fileName, Environment.NewLine + "M END");
}
You'll need to loop through all the files matching that pattern and write to them individually. The StreamWriter constructor you're using only supports writing to an individual file (source).
You can get a list of files using:
string[] filePaths = Directory.GetFiles("c:\\abc\\", "*.mol");
You need to iterate over the files in the directory. DirectoryInfo / FileInfo makes it easy to do this. Also, since you want to append to the end, you need to seek the stream before writing your signature at the end.
Here's a solution that works solely at that location. You will need to add recursive support to descend into subdirectories if desired.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace appender
{
class Program
{
static void AppendToFile(FileInfo fi)
{
if (!fi.Exists) { return; }
using (Stream stm = fi.OpenWrite())
{
stm.Seek(0, SeekOrigin.End);
using (StreamWriter output = new StreamWriter(stm))
{
output.WriteLine("M END");
output.Close();
}
}
}
static void Main(string[] args)
{
DirectoryInfo di = new DirectoryInfo("C:\\abc\\");
FileInfo[] fiItems = di.GetFiles("*.mol");
foreach (FileInfo fi in fiItems)
{
AppendToFile(fi);
}
}
}
}

Categories