Solved

Write multiple lines to txt file and start from specific postion on line

Posted on 2014-12-31
15
204 Views
Last Modified: 2015-01-04
I am writing a specific type of file from C# program and am trying to figure out how to acchomplish what I need.

What I need is the following -

There are multiple text boxes to enter data into such as firstname,middlename,lastname, ssn

I am looking to create a text file from this data which would have to be very specific for example.

I would create the text file and on the first line I would have"02C" then immediate following would have the first name then the middle name would start from the 20th position on the same line from the begining (which is where i cannot figure out some of it.)

then i would want it to create a second line in the text file that would show "03ABW" then immediately have the ssn

So the file in end would look like the following

"02CBOB             SMITH"
"03ABW123456789"

Notice there are 20 positions from the 1st to where "S" starts in Smith
0
Comment
Question by:desiredforsome
[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
  • 8
  • 6
15 Comments
 
LVL 34

Expert Comment

by:it_saige
ID: 40525907
Not saying that what you want cannot be accomplished as you could use tab delimiters.  But using tab delimiters presents a problem when a person tries to read the file as opposed to a parser (or program).  Is there any reason why you would not want to use Xml or a comma-delimiter?

-saige-
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40525910
Hi desiredforsome;

Using String.Format will do what you want.

// Test Data
String begining = "02C";
String firstName = "BOB";
String lastName = "SMITH";
String begining2 = "03ABW";
String ssn = "123456789";

string output = String.Format("{0}{1,-17}{2}\n{3}{4}", begining, firstName, lastName, begining2, ssn);

Open in new window

0
 

Author Comment

by:desiredforsome
ID: 40525942
Looking for something a little more dynamic if i need it to fill 20 spaces and can calulate based on if the string as 5 letters or 10 leters.
0
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 

Author Comment

by:desiredforsome
ID: 40525947
here is my code so far but not accurate

becasue the date stirng that is in there is supposed to start on the 35th character with a max of 11 characters in that string.

So code below
string eh = string.Format("{0}", "EH");
            string instid = string.Format("{0,6}", "");
            string instname = string.Format("{0,25}", "");

            string date = string.Format("{0,11}", "20141231");
            string envelope = string.Format("{0,9}", "ENV16");
            string name=textBox1.Text + " " + textBox2.Text + " " + textBox3.Text;
            using (StreamWriter writer = new StreamWriter(@"C:\Users\cgashlin.MORGANFINANCIA0\Desktop\test1003.txt"))
            {

               
                StringBuilder sb = new StringBuilder();
                sb.Append(eh) ;
                sb.Append(instid);
                sb.Append(instname);
                sb.Append(date);
                sb.Append(envelope);
                sb.Append(Environment.NewLine);
                sb.Append("02C"+  string.Format("{0,4}", name));
                writer.Write(sb.ToString());

Open in new window


Produces
EH                                  20141231    ENV16

Open in new window


However the spacing is not correct and I need it to be.

Here is my goal in spacing
EH                                20141231   ENV14  

Open in new window

string eh = string.Format("{0}", "EH");
            string instid = string.Format("{0,6}", "");
            string instname = string.Format("{0,25}", "");

            string date = string.Format("{0,11}", "20141231");
            string envelope = string.Format("{0,9}", "ENV16");
            string name=textBox1.Text + " " + textBox2.Text + " " + textBox3.Text;
            using (StreamWriter writer = new StreamWriter(@"C:\Users\cgashlin.MORGANFINANCIA0\Desktop\test1003.txt"))
            {

               
                StringBuilder sb = new StringBuilder();
                sb.Append(eh) ;
                sb.Append(instid);
                sb.Append(instname);
                sb.Append(date);
                sb.Append(envelope);
                sb.Append(Environment.NewLine);
                sb.Append("02C"+  string.Format("{0,4}", name));
                writer.Write(sb.ToString());

Open in new window

string eh = string.Format("{0}", "EH");
            string instid = string.Format("{0,6}", "");
            string instname = string.Format("{0,25}", "");

            string date = string.Format("{0,11}", "20141231");
            string envelope = string.Format("{0,9}", "ENV16");
            string name=textBox1.Text + " " + textBox2.Text + " " + textBox3.Text;
            using (StreamWriter writer = new StreamWriter(@"C:\Users\cgashlin.MORGANFINANCIA0\Desktop\test1003.txt"))
            {

               
                StringBuilder sb = new StringBuilder();
                sb.Append(eh) ;
                sb.Append(instid);
                sb.Append(instname);
                sb.Append(date);
                sb.Append(envelope);
                sb.Append(Environment.NewLine);
                sb.Append("02C"+  string.Format("{0,4}", name));
                writer.Write(sb.ToString());

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40525953
This specifier , "{1,-17}", states that the second field can have any number of characters from 0 - 17 left justified where the third field will always start at position 20. I think that is dynamic.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40525964
It would be easier if you specify what fields should be printed giving starting column and its width. Then we can come up with the correct formatting of the data.
0
 

Author Comment

by:desiredforsome
ID: 40529242
There are tons of these that will be happening in the end as I am working on fannie mae file format stuff.

What string.format would I use in order for a string to start on the 9th place that is 20 characters in total even though it will only have 5 characters.

"         tests               "
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40529259
Given that you want to construct a string having the first field being empty and a width of 9 and the second field having a width of 20 and being left justified the following string format specifier, "{0,9}{1,-20}", will produce that result as shown in the code snippet below.

string field1 = "";
string field2 = "tests";

string result = String.Format("{0,9}{1,-20}", field1, field2);

// result will contain the string "         tests               "

Open in new window

0
 

Author Comment

by:desiredforsome
ID: 40530170
Any good information on using that context with string.format? I Would liek to understand it as it just is not producing the results I would like to see.

The one previous wasw an example.

However

feild 1 could be 3 or 5 characters.
feild2 would always start on the 9th place of the line from 0 and the feild2 would be no longer than 20 characters thus having he next field (feild3) start on the 20th character
0
 

Author Comment

by:desiredforsome
ID: 40530185
So What it is looking like is that I need the starting charcter to start on the specified place and not ending on the specific place holder. so if i use a example like "tests" and have it do {0,9} then it movies it over until it ends on the 9th. I woudl like it to start on the 9th. NO matter what the string actually is.
0
 

Author Comment

by:desiredforsome
ID: 40530187
Also with a maxium width.
0
 

Author Comment

by:desiredforsome
ID: 40530192
Maybe it would be easier if I wrote it like this.


String feild1 = "feield1";
String field2 = "feidl2";
String feidl3 = "feidl3";

I want field1 to start at the 0 position on a line for the text file with a maximum number of characters of 10. However feild1 can be up to 10 characters it can say "Field1" or "FIelds01" it does not matter.

Then on the 11th position in the same line field2 must start with a maximum characters of 14. Again same thing it does not matter what the string actually is.

Field 3 will start on the 25th position on the line.
and so on.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40530278
To your statement, "I want field1 to start at the 0 position on a line for the text file with a maximum number of characters of 10. However feild1 can be up to 10 characters it can say "Field1" or "FIelds01" it does not matter."

To do this you would need this specifier.
"{0,-10}"

To your statement, "Then on the 11th position in the same line field2 must start with a maximum characters of 14. Again same thing it does not matter what the string actually is.",

To do this you would need this specifier.
"{1,-14}"

To your statement, "Field 3 will start on the 25th position on the line. and so on."

To do this you would need this specifier. Where XX needs to be replaced with the max field width.
"{2,-XX}"

So your sample code would be

String feild1 = "feield1";
String field2 = "feidl2";
String feidl3 = "feidl3";

string output = String.Format("{0,-10}{1,-14}{2,-XX}...",  field1, field2, field3, ...);

Please read through the Microsoft documentation on String.Format Method.
0
 

Author Closing Comment

by:desiredforsome
ID: 40530671
This makes sense now. I thank you very much this should solve all my issues :-) A+++++
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40530692
Not a problem desiredforsome, glad to help..
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

624 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