Click here to Skip to main content
15,906,567 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
How would I use the streamwriter to write a line with a fixed number of elements
and a space in between
For example here would be a text file to be opened:
MKWVTFISLLLLFSSAYSRGESHAGCEKSLHTLFGDELCKYLYEIAR
would read:


MKWVTFISLL LLLFSSAYSR
ESHAGCEKSL HTLFGDELCK
YLYEIAR

I have so far

string fileContents = File.ReadAllText(file, Encoding.ASCII); //Sequence database
string line = "";
char[] sequenceCount = fileContents.ToCharArray();
int last = sequenceCount.Length;

for (int i = 0; i < 10; i++)
{
line += Convert.ToString(sequenceCount[i]);
}
Posted

1 solution

No, you did not explain what do you want to write. What to do with line boundaries? Where to put ends of line on output? Where to put blank space? What is the sense of all this activity?

Let's forget it for now.
This is not how people write working code.

Do not immediate constants. In particular, don't use "", use string.Empty. Don't use immediate constant "10", make it a parameter of some function of constructor or explicit constant.

You don't need ToCharArray, it makes no sense, because sequenceCount[i] can be written as fileContents[i].

Nothing is so bad for performance as string concatenation operator "+" in a loop. This is because strings are immutable. When you append something to string, a brand new string object is created and the new reference is assigned to the variable on left of the assignment operator. Is causes copy of the whole string every time a string operation is called with ever growing string contents. This is very ineffective. You need to use System.Text.StringBuilder and its method Append or AppendFormat instead. See http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=VS.100).aspx[^].

The method Convert.ToString makes no sense. A character can be appended to a string. From the previous paragraph you can see that you should use System.Text.StringBuilder.Append which also accepts character.

Should I continue? …Keep trying.
Any follow-up questions are welcome.

—SA
 
Share this answer
 
Comments
RakeshMeena 17-Jun-11 0:18am    
Agree with you! My 5
Sergey Alexandrovich Kryukov 17-Jun-11 1:07am    
Thank you, Rakesh.
--SA
Abdullatif M. Abu Al Rub 17-Jun-11 0:39am    
do you expect every body to be a "proffessional" developer like "you"?
he asks for answers NOT for advices about how writing code!
Sergey Alexandrovich Kryukov 17-Jun-11 1:06am    
Well, thank you for your opinion.

I don't think what you mention is relevant or practical.

Is being a non-professional an excuse? Your reply is counter-productive. Let OP decide what is helpful and what is not. I'm helping people in programming, only those who is able to get help (otherwise the whole activity makes no sense, is it?). This is not the same as answering all question, which may have no sense, contain all kinds of mistakes, misconceptions, etc. Answering "just the question" is very ineffective in sharing any knowledge.

Also, please pay attention for the lack of clear explanation of the OP's goal. If you understood it better then I did, you could be very helpful by explaining to me what should be achieved. However, there is not sense in doing so prior to fixing of the problems I pointed out.

Actually, fixing those problem is more important then solving the problem itself, as this problem is very peculiar, but the coding problems I suggest to fix are very common and present a blocker for many other applications.

I hope my comments can help you to understand the situation with this post more clearly.

Thank you again, respect,
--SA
Sergey Alexandrovich Kryukov 17-Jun-11 1:12am    
Also, I would like to explain that I am not a professional. Not in any field of activity which uses professional approach.

So, once again, being not a professional can not serve as an excuse for any kind of mistakes, sloppy work, low quality or other kinds of deficiency. If you do any sort of work, do it properly, no matter who you are.

--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900