how to write formatted text into a file - winforms

I was trying to write to a file from textbox input with date:
Here is a part of the code:
DateTime dt=System::DateTime::Now;
System::IO::StreamWriter^ history = gcnew StreamWriter("history.txt");
history->WriteLine(textBox1->Text);
history->WriteLine(dt);
history->Close();
But the output is like this: text
09/02/2015 23:26:07
But I want it to be like:
text 09/02/2015 23:26:07
And also has to append next input to next line of the file.
It's something like a log file.

you use two writeLine and so that write it in 2 Line concat them to write it in one line
history->WriteLine(textBox1->Text);
history->WriteLine(dt);
change this part to
history->write(textBox1->Text);
history->writeLine(dt);

history->WriteLine(textBox1->Text);
Using WriteLine() causes the line break of course. You'd have to use Write() instead. And fret a bit about how you get the extra space between the text and the date, never hesitate to use composite formatting in .NET:
history->WriteLine("{0} {1}", textBox1->Text, System::DateTime::Now);

Related

Tab separated text to CSV in C

I need to read a tab-separated text from a file and then write it to the CSV file.
My problem is when I get to a part where there is more than one tab in a row.
Given txt file is an address book, and has 37 values. When I encounter a tab, a should jump over it and read the next string. So when there is only one tab it works fine. But when there is more than one tab, my regex eats all the tabs until the next string and then reads it.
I need to read that "nothing" in-between the two tabs.
This problem results in storing a value to a field that is not meant for it. For example, I have fields firstName, lastName, fullName, and email. if there is no fullName value, there would be two tabs and then my code should store email value to email. Instead, it stores that value to the fullName field. Here is my code:
char* format = "%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t"
"%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t"
"%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\n";
fscanf(file, format, cont.firstName, cont.lastName, cont.displayName, cont.nickname, cont.primaryEmail, cont.secondaryEmail,
cont.screenName, cont.workPhone, cont.homePhone, cont.faxNumber, cont.pagerNumber, cont.mobileNumber,`enter code here`
cont.homeAddress, cont.homeAddress2, cont.homeCity, cont.homeState, cont.homeZipCode, cont.homeCountry,
cont.workAddress, cont.workAddress2, cont.workCity, cont.workState, cont.workZipCode, cont.workCountry,
cont.jobTitle, cont.department, cont.organization, cont.webPage1, cont.webPage2, cont.birthYear, cont.birthMonth, cont.birthDay,
cont.custom1, cont.custom2, cont.custom3, cont.custom4, cont.notes);

VBSCRIPT REPLACE not removing spaces from Decrypted fields

Got quite a head-scratcher....
I'm using the VBScript function REPLACE to replace spaces in a decrypted field from a MSSQL DB with "/".
But the REPLACE function isn't "seeing" the spaces.
For example, if I run any one of the following, where the decrypted value of the field "ITF_U_ClientName_Denc" is "Johnny Carson":
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/")
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/")
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"),"Chr(160)","/")
REPLACE(CSTR(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"))," ","/")
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/",1,-1,1)
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/",1,-1,0)
The returned value is "Johnny Carson" (space not replaced with /)
The issue seems to be exclusively with spaces, because when I run this:
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"),"a","/")
I get "Johnny C/rson".
Also, the issue seems to be exclusively with spaces in the decrypted value, because when I run this:
REPLACE("Johnny Carson"," ","/")
Of course, the returned value is "Johnny/Carson".
I have checked what is being written to the source of the page and it is simply "Johnny Carson" with no encoding or special characters.
I have also tried the SPLIT function to see if it would "see" the space, but it doesn't.
Finally, thanks to a helpful comment, I tried VBS REGEX searching for \s.
Set regExp = New RegExp
regExp.IgnoreCase = True
regExp.Global = True
regExp.Pattern = "\s" 'Add here every character you don't consider as special character
strProcessed = regExp.Replace(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"), "?")
Unfortunately, strProcessed retruns "Johnny Carson" (ie. spaces not detected/removed).
If I replace regExp.Pattern = "a", strProcessed returns "Johnny C?rson".
Many thanks for your help!!
As we found, the right character code is 160, and that did the trick:
replace(..., ChrW(160), "...")
This seems to be data specific and, additionally, as an alternative you can try to get same encoding of the source script (i.e. save with Save As with Encoding), or convert received database value into a different target encoding.

I accidentally split my imported txt file so every string was on a line by itself

enter image description here
I was trying to put a list of strings ("eggs, alfalfa sprouts, bean sprouts, cabbage, tomatoes, etc) into an array. I did this however, it put each string onto a line by itself. I essentially need to correct this so it outputs "bean sprouts" and "alfalfa sprouts" on the same line not "alfalfa" then "sprouts" on the line below it. Attached is my code as well as the output that it is giving me.

Writing a new column in a csv file through Command line

I want to create a python script that will write a new column in a csv file along with the data through command line, using optparse.
For e.g. if following is the input file(script_name.py) :-
User_ID,Date,Num_1,Num_2,Com_ID
101,2015-04-13,12,21,1011
102,2014-4-3,1,7,1002
then python script_name.py Num_3 1101 1102. will create a new column with data.
Can anybody please direct me somewhere where I can learn how to achieve this ??I already read about OptParse but got nothing.
use
import optparse
parser = optparse.OptionParser()
(options, args) = parser.parse_args()
print args
the args variable will contain an array of all arguments
than using simple loops you can run over the file and add the contents of the args variable

How to read a file in Groovy into a string?

I need to read a file from the file system and load the entire contents into a string in a groovy controller, what's the easiest way to do that?
String fileContents = new File('/path/to/file').text
If you need to specify the character encoding, use the following instead:
String fileContents = new File('/path/to/file').getText('UTF-8')
The shortest way is indeed just
String fileContents = new File('/path/to/file').text
but in this case you have no control on how the bytes in the file are interpreted as characters. AFAIK groovy tries to guess the encoding here by looking at the file content.
If you want a specific character encoding you can specify a charset name with
String fileContents = new File('/path/to/file').getText('UTF-8')
See API docs on File.getText(String) for further reference.
A slight variation...
new File('/path/to/file').eachLine { line ->
println line
}
In my case new File() doesn't work, it causes a FileNotFoundException when run in a Jenkins pipeline job. The following code solved this, and is even easier in my opinion:
def fileContents = readFile "path/to/file"
I still don't understand this difference completely, but maybe it'll help anyone else with the same trouble. Possibly the exception was caused because new File() creates a file on the system which executes the groovy code, which was a different system than the one that contains the file I wanted to read.
the easiest way would be
new File(filename).getText()
which means you could just do:
new File(filename).text
Here you can Find some other way to do the same.
Read file.
File file1 = new File("C:\Build\myfolder\myTestfile.txt");
def String yourData = file1.readLines();
Read Full file.
File file1 = new File("C:\Build\myfolder\myfile.txt");
def String yourData= file1.getText();
Read file Line Bye Line.
File file1 = new File("C:\Build\myfolder\myTestfile.txt");
for (def i=0;i<=30;i++) // specify how many line need to read eg.. 30
{
log.info file1.readLines().get(i)
}
Create a new file.
new File("C:\Temp\FileName.txt").createNewFile();

Resources