• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Read from one file and wirte to another...

The preblem is quite simple...or is it....
I have one source file. Let's name it SOURCE.DAT. It is written in CodePage 1250 (Central European), so it contains localized chars (š,č,ž, and such crap). I would just like to open this file, read line by line and save it to DESTINATION.DAT file and preserve those localized chars. I tried it with Encoding and all possible options (UTF8 etc..), but when I read a line to string those chars are immediately converted to UNICODE(Encoding.Default) or even NOTHING(Encoding.UTF8). Is there a way to simply take the line from file as it is and put it in another file without all that crapy conversions?
0
ssdjgru1
Asked:
ssdjgru1
  • 4
  • 3
  • 3
1 Solution
 
Thandava VallepalliCommented:
can you paste ur code.... here
0
 
Thandava VallepalliCommented:
0
 
Thandava VallepalliCommented:
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Meir RivkinFull stack Software EngineerCommented:
hi ssdjgru1,

i have a text file which contains different charcters like this:
<----------
sdbvsa
&#1491;&#1512;&#1497;&#1506;&#1490;&#1499;&#1497;&#1506;&#1491;&#1490;&#1499;
erwünscht
Fußball
&#1052;&#1091;&#1079;&#1077;&#1081; &#1080;&#1089;&#1090;&#1086;&#1088;&#1080;&#1080; &#1082;&#1086;&#1084;&#1087;&#1100;&#1102;&#1090;&#1077;&#1088;&#1086;&#1074;
musée d'Orsay célèbre
desempeñaron en la evolución
---------->


i used StreamReader and StreamWriter and it copyied the file just fine.
can u post your code?
0
 
Meir RivkinFull stack Software EngineerCommented:
it doesnt show all the characters here but i've added the same thing in my code.

0
 
ssdjgru1Author Commented:
My code:
//************
TextReader trSF = new StreamReader("source.dat",Encoding.UTF8);      
TextWriter twSF = new StreamWriter("temp.dat",false,Encoding.UTF8);
while ((datLine = trSF.ReadLine())!= null)
{
               data = datLine.Substring(lastSepIdx,currSepIdx-lastSepIdx);
               recombination = recombination + data;
               twSF.WriteLine(recombination);
}
trSF.Close();
twSF.Close();
//************
If you want  source.dat I can mail it for testnig!
0
 
Meir RivkinFull stack Software EngineerCommented:
email me the source.dat to:      sedgwick@gmail.com
0
 
ssdjgru1Author Commented:
My problem is apperantly in strings...when I create string:
string test = "HELLO";
and write it to file with streamWriter like:
twSF.WriteLine(test);

...when I open the file I have written and look at it i hex mode I see, that all chars are written with two bytes. How to write it in single byte notation???(Grrrr....;)

0
 
Meir RivkinFull stack Software EngineerCommented:
it took me sometime but i finally got it:

string strLine;
      
                  Encoding enc = Encoding.GetEncoding(1252);

                  using (StreamWriter sw = new StreamWriter("c:\\dest.dat"))
                  {
                        using (StreamReader sr = new StreamReader("c:\\source.dat", enc))
                        {
                              strLine = sr.ReadToEnd();
                              sw.Write(strLine);
                        }
                  }

the charset value was wrong and i was looking for something that can display the characters u've sent.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtextencodingclassgetencodingtopic2.asp

0
 
ssdjgru1Author Commented:
hmmm...the problem still persists ;)
the destination file is written with right chars, but it still has 2 byte per character notation (look at it with hex editor), while the source is 1 byte per character - it has to be the same notation, because the data is then exported to some hand held device which has no idea what unicode is.
Thank you for your effort sedgwick!
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now