Click here to Skip to main content
15,891,902 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, first my question above my sound stupid anyways i create a simple app that can produce a text file. What i'm doing is that i'm inserting a bunch of words in a single string by using concatenation then call in in
File.Write(File);
.

my problem is how can i repeat the process of inserting the words in a string again and again then call it to File.Write();, because i don't want to produce a thousand text file. here is my code.


string space = "\r\n";
    string UNB = "UNB+UNOA:2+MNL03+WANHAI+20200413:0000+11011" + "'" + space;
    string UNH = "UNH+11011+CODECO:D:95B:UN:ITG13" + "'" + space;
    string BGM = "BGM+34+20200413+9" + "'" + space;
    string NAD = "NAD+CF+WHL:160:166" + "'" + space;
    string EQD = "EQD+CN+WHLU0327099+22GI:102:5+++4" + "'" + space;
    string DTM = "DTM+" + dtmDO.dtQualifier + ":" + dtmDO.dtPeriod + ":" + dtmDO.dtPeriodFormatQualifier + "'" + space;
    string LOC = "LOC+11+PHBTG:139:6" + "'" + space;
    string CNT = "CNT+16:1" + "'" + space;
    string UNT = "UNT+10+11011" + "'" + space;
    string UNZ = "UNZ+4+11011" + "'" + space;

    string final = UNB + UNH + BGM + NAD + EQD + DTM + LOC + CNT + UNT + UNZ;
    StreamWriter File = new StreamWriter("Reden.edi");
    File.Write(File);
    File.Close();


What I have tried:

i tried Insert method in string but no it's not what i want but i this i'll try the add method but i don't any next step.


Hi, here is my solution on using the
File.AppendAllText
method


DTM dtmDO = new DTM();
            string space = "\r\n";
            int r = 100;
            StreamWriter Files = new StreamWriter("Reden.edi");
            Files.Close();
            string UNB = "UNB+UNOA:2+MNL03+WANHAI+20200413:0000+11011" + "'" + space;
            string UNH = "UNH+11011+CODECO:D:95B:UN:ITG13" + "'" + space;
            string BGM = "BGM+34+20200413+9" + "'" + space;
            string start = UNB + UNH + BGM;
            File.AppendAllText("Reden.edi", start);
            for (int i = 0; i < r; i++)
            {         
                string NAD = "NAD+CF+WHL:160:166" + "'" + space;
                string EQD = "EQD+CN+WHLU0327099+22GI:102:5+++4" + "'" + space;
                string DTM = "DTM+" + dtmDO.dtQualifier + ":" + dtmDO.dtPeriod + ":" + dtmDO.dtPeriodFormatQualifier + "'" + space;
                string LOC = "LOC+11+PHBTG:139:6" + "'" + space;
                string final = NAD + EQD + DTM + LOC;
                File.AppendAllText("Reden.edi", final);
            }     
            string CNT = "CNT+16:100" + "'" + space;
            string UNT = "UNT+10+11011" + "'" + space;
            string UNZ = "UNZ+4+11011" + "'" + space;
            string end = CNT + UNT + UNZ;
            File.AppendAllText("Reden.edi", end);         
            Files.Close();


And it works thanks to all. :)
Posted
Updated 2-Jul-20 19:37pm
v3

If I got you then you may find the StringBuilder Class (System.Text) | Microsoft Docs[^] interesting.
 
Share this answer
 
Comments
Maciej Los 7-Jul-20 5:33am    
5ed!
CPallini 7-Jul-20 13:37pm    
Thank you again and again (and again) :-)
Two things:
1) The code you show writes an unknown string to the file: you assemble a string in final but write the content of sb:
C#
string final = UNB + UNH + BGM + NAD + EQD + DTM + LOC + CNT + UNT + UNZ;
...
File.Write(sb);


2) There are several ways to do what you want. One - as suggested by CPallini - is to use a StringBuilder, which is always a better idea than string concatenation. StringBuilder is an "expandable string" which means it uses less memory that multiple concatenations (remember that strings are immutable, so each time you add two strings, you produce a third.
This code:
C#
string final = UNB + UNH + BGM + NAD + EQD + DTM + LOC + CNT + UNT + UNZ;
generates a number of intermediate strings, each increasing in length as it goes.

A better solution might be to just use the File.AppendAllText Method (System.IO) | Microsoft Docs[^] to add each term when you need:
C#
string final = UNB + UNH + BGM + NAD + EQD + DTM + LOC + CNT + UNT + UNZ;
...
File.AppendAllText("Reden.edi", final);
 
Share this answer
 
Comments
Reden Rodriguez 3-Jul-20 1:22am    
string builder is good but it will throw an OutOfMemoryException i guess the best solution is to open the file again and write on it. Thanks mates for understanding my question.
OriginalGriff 3-Jul-20 3:14am    
If your StringBuilder is throwing "out of memory" then there is something very wrong with the code - it can use up to 4GB of space!

Just how big is your log file? :OMG:
Maciej Los 7-Jul-20 5:35am    
5ed!

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