?
Solved

Read from one file and wirte to another...

Posted on 2005-03-22
10
Medium Priority
?
209 Views
Last Modified: 2010-04-16
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
Comment
Question by:ssdjgru1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 14

Expert Comment

by:Thandava Vallepalli
ID: 13601675
can you paste ur code.... here
0
 
LVL 14

Expert Comment

by:Thandava Vallepalli
ID: 13601698
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 42

Expert Comment

by:sedgwick
ID: 13602836
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
 
LVL 42

Expert Comment

by:sedgwick
ID: 13602871
it doesnt show all the characters here but i've added the same thing in my code.

0
 

Author Comment

by:ssdjgru1
ID: 13609140
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
 
LVL 42

Expert Comment

by:sedgwick
ID: 13609374
email me the source.dat to:      sedgwick@gmail.com
0
 

Author Comment

by:ssdjgru1
ID: 13609383
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
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 13611026
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
 

Author Comment

by:ssdjgru1
ID: 13611203
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] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question