Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the code as below . I am getting an exception when the Filecontent size is about:
filecontent.Length 4821980 int .

The control automatically exits from the loop and starts looping from first again. I don't understand the issue. IS there any thing wrong in this.

I am doing this in order to write large number or records for the text file. Please correct me or suggest me an alternative for this.
--------------------------------------------------------------------
C#
string filecontent = string.Empty;
                int t = 0;
                for (int i = 0; i < filingTable.Rows.Count; i++)
                {
                                  
                    if (i != 0)
                        filecontent += System.Environment.NewLine;

                    filecontent += fixedString(filingTable.Rows[i][0].ToString(), 20);
                    filecontent += fixedString(filingTable.Rows[i][1].ToString(), 1);
                    filecontent += fixedString(filingTable.Rows[i][2].ToString(), 8);
                    filecontent += fixedString(filingTable.Rows[i][3].ToString(), 11);
                    filecontent += fixedString(filingTable.Rows[i][4].ToString(), 9);

                    filecontent += fixedString(filingTable.Rows[i][5].ToString(), 40);
                    filecontent += fixedString(filingTable.Rows[i][6].ToString(), 40);
                    filecontent += fixedString(filingTable.Rows[i][7].ToString(), 40);
                    filecontent += fixedString(filingTable.Rows[i][8].ToString(), 40);
                    filecontent += fixedString(filingTable.Rows[i][9].ToString(), 40);

                    filecontent += fixedString(filingTable.Rows[i][10].ToString(), 180);

                    filecontent += fixedNumber(filingTable.Rows[i][11].ToString().TrimEnd(), 12);

                    filecontent += fixedNumber(filingTable.Rows[i][12].ToString(), 6);
                }
Posted
Updated 10-Aug-10 8:27am
v2

1 solution

0) You should be using the StringBuilder object to buuilt the line of text.

StringBuilder sb = new StringBuilder();
sb.Append(fixString(blah blah));
sb.Append(fixString(blah blah));
sb.Append(fixString(blah blah));
...


1) You should be write a single line at a time.

2) You still might run into the problem because files cannot be an infinite size. BTW, a string can only be 4gb, max.
 
Share this answer
 
v2
Comments
Lima3 10-Aug-10 13:53pm    
Yep I started this and changed the code for string builder. Testing right now. Will keep posted for any issues.Thanks.
Abhishek Sur 10-Aug-10 15:39pm    
Yes, string is immutable while StringBuilder is mutable. So if you go on changing the size of string, it will create more and more memory location to store the same. So as John suggested, you should use StringBuilder always.
AspDotNetDev 10-Aug-10 16:44pm    
Reason for my vote of 5
This is what I would recommend.

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