|
OK, been there. First read (part of) the file in binary, looking for \n and \r, and make up your mind. Then start using TextReader and TextWriter.
BTW: what if the input file's NewLine is inconsistent?
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
Luc Pattyn wrote: BTW: what if the input file's NewLine is inconsistent?
Then...I start to cry.
As I would have to go through each line, then look for the appropriate EOL terminators,
then use that EOL for the modified file.
I thought this would be easy!
Where are those tissues...
"If you can't explain it simply, you don't understand it well enough" ~ Albert Einstein
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." ~ Paul Neal "Red" Adair
Now reading: 'The Third Reich', by Michael Burleigh
|
|
|
|
|
it gets easy once you have all the requirements in front of you.
are you also considering ANSI/UTF8/Unicode, and the byte-order-mask?
TextReader may happily skip over those...
(and what if the original doesn't support the kind of change you want to apply?)
Have a look at StreamWriter, it takes an Encoding...
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
Yeah, well, currently I have a StreamReader on the source file, and then a StreamWriter on the 'modified' file.
I've set the StreamWriter to have the same Encoding as the StreamReader.
As far as the 'byte-order-mask' goes, I have no idea what that is.
"If you can't explain it simply, you don't understand it well enough" ~ Albert Einstein
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." ~ Paul Neal "Red" Adair
Now reading: 'The Third Reich', by Michael Burleigh
|
|
|
|
|
the BOM is an optional 2- or 3-byte code prefixed to the actual text, and indicating the file is encoded using UTF8 or some kind of Unicode; have a look at some text files with a hex viewer.
You could use Notepad (watch the bottom part of the Save File Dialog) to force a particular encoding, at least since Vista.
Most classes deal with BOM automatically, without telling you. If you're not careful, you might add or remove the BOM without even realizing.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
Woah, I never knew that.
I shall google hence forth and see what I can find.
Thanks for the information Luc.
"If you can't explain it simply, you don't understand it well enough" ~ Albert Einstein
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." ~ Paul Neal "Red" Adair
Now reading: 'The Third Reich', by Michael Burleigh
|
|
|
|
|
The XP version of notepad has it too
|
|
|
|
|
If you knew what sort of system they were using to create the file you might be able to code around that.
New Line info[^]
Possibly the best explanation I have ever seen for what the heck happened with new lines.
|
|
|
|
|
Thanks, yeah I've had a peruse through that link as well - very interesting.
The trouble is the files could come from any system, I've got some files from a UNIX system, but have run the modification functionality on windows file as well.
I, stupidly, thought that it would be a simple thing to do...
"If you can't explain it simply, you don't understand it well enough" ~ Albert Einstein
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." ~ Paul Neal "Red" Adair
Now reading: 'The Third Reich', by Michael Burleigh
|
|
|
|
|
1.21 Gigawatts wrote: The trouble is the files could come from any system, I've got some files from a UNIX system, but have run the modification functionality on windows file as well.
Dear lord I am so sorry.
Good luck.
|
|
|
|
|
Ignoring the issue of byte-order marks, you could try this:
1. Open the file with a StreamReader
2. Read the first line
3. Compare StreamReader.BaseStream.Position with line length
This may be able to distinguish between \n and \r\n but of course doesn't really help if you could have \r.
As an (simpler) alternative, is there some way to make WinMerge ignore line-ending changes?
|
|
|
|
|
Gideon Engelberth wrote: As an (simpler) alternative, is there some way to make WinMerge ignore line-ending changes?
Yeah there is, but it's hiding the problem, rather than finding the solution to it.
I don't want to use the wrong EOL terminator for a UNIX file and then that system not being able to read the file correctly.
But as far as the BaseStream.StreamPosition goes, I hadn't thought of that.
Thanks for the info.
"If you can't explain it simply, you don't understand it well enough" ~ Albert Einstein
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." ~ Paul Neal "Red" Adair
Now reading: 'The Third Reich', by Michael Burleigh
|
|
|
|
|
generally speaking, if you are writing an install program, how would you ensure that a computer has a required installation prerequisite (i.e. MySql)?
in development, i add a reference to a DLL to get the MySQL libraries. in an installation environment, would the install program look for that DLL (through COM for example), or would I need to include the required DLL with the install?
|
|
|
|
|
HI All!
i have a panel on which i am drwaing a image control on the center of panel, now when i zoom the image i am resizing the control's left, top , height and width, now when i get negative left and top values the image gets cropped from top Image gets cropped from the top but the bottom scroll bars appears.. is there any way so that when i set the values of my image control to negative(i.e left and top) still the scroll bars appear for that control!!
Thanks in Advance
Samir
|
|
|
|
|
Hello,
i am very new to C# so please be patient.
i am printing orders in my app.
so i have a button which creates the printjob.
the paper will be written in the Printpage-Event.
now i want to have a function that gets th order-object as a parm
and print it on the paper.
the reason is, that an order must be printed on severall programm modules.
private void btnDruck_Click(object sender, EventArgs e)
{
PrintDocument Druckjob = new PrintDocument();
PrintDialog Druckermenue = new PrintDialog();
PrintPreviewDialog Druckpreview = new PrintPreviewDialog();
Druckjob.DocumentName = "Rechnung";
Druckjob.PrintPage += new PrintPageEventHandler(Druckjob_PrintPage);
Druckpreview.Document = Druckjob;
Druckermenue.Document = Druckjob;
if (Druckermenue.ShowDialog() == DialogResult.OK)
{
Druckjob.Print();
}
Druckjob.PrintPage -= new PrintPageEventHandler(Druckjob_PrintPage);
}
void Druckjob_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
StringFormat lineFmtRight = new StringFormat();
lineFmtRight.Alignment = StringAlignment.Far;
g.PageUnit = GraphicsUnit.Millimeter;
g.Dispose();
}
bye jo
|
|
|
|
|
Can any body tell me with an example, how loose coupling is implemented in c# in a such a way that if a class is implementing some interface and if we change something in that class then consumer class doesn't gets effected and without using interface how all the classes consumer class is changed if we didnot implemen using interface.
|
|
|
|
|
Only if you tell me, with an example, of how high cohesion is implemented in c# so that the module implementing the interface is written so it encapsulates only its own duties an does not effect any of the parameters passed to it.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
|
|
|
|
|
|
Can any body tell me with an example, how loose coupling is implemented in c# in a such a way that if a class is implementing some interface and if we change something in that class then consumer class doesn't gets effected and without using interface how all the classes consumer class is changed if we didnot implemen using innterface.
|
|
|
|
|
Hi,
I would like to achieve the following. I want to use c# code in an if statement where if the last 2 digits are equal to "xx". How would I approach this.
I appreciate the feedback, thank you in advance.
suprsnipes
|
|
|
|
|
Hi,
your question isn't very clear. If the variable is an integer, all it takes is if ((var % 100) == x) ... . If the value could be negative, you might need additional code.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
You could try
if(Number.ToString().EndsWith("xx"))
{
}
Although it wont be as efficient as using a mod operator
|
|
|
|
|
Would that even reliably work? I mean:
(quote from MSDN)
The return value is formatted with the general numeric format specifier ("G") and the NumberFormatInfo object for the current culture.
What if under some cultures something other than 0-9 is used, or if they put something at the end, or the middle, or whatever? Do such cultures exist?
I don't know, but using mod sure is a lot safer..
|
|
|
|
|
never convert to string if you don't have to; it is inefficient, creates unnecessary objects, and may be error-prone (e.g. when regional settings may influence it, or when checking for a type match).
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
How would I go about finding the nearest number ending in 'xx' to another number?
|
|
|
|