Convert a Text File into a CSV File with C# [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a txt file that is Tab Delimited, like this:
Well Name Detector Task Quantity
1 try1 Cam1 UNKN 0
2 try2 Cam1 UNKN 0
3 try3 Cam3 STND 1000
4 try4 Cam5 UNKN 0
5 try5 Cam6 UNKN 0
6 try6 Cam6 UNKN 0
....
92 try92 Cam4 STND 100
I need to output a CSV file from this TXT file.
In the outputted CSV file I need to change the delimiter from Tab to semi-colon and I only need the first two columns.
I put the text file in an array with:
string [] source....
also to skip some header lines I don't need,
But now I'm stuck at how to go on.
I know about StreamReader and StreamWriter but I don't know how to use them to accomplish my task.
Any help would be kindly appreciated.
Thanks!

Hope this gives you the idea, so try to understand each part.
string[] lines = System.IO.File.ReadAllLines(YOUR INPUT FILE);
StringBuilder builder = new StringBuilder();
foreach (string line in lines)
{
var temp = line.Split('\t');
builder.AppendLine(string.Join(";", temp[0], temp[1]));
//builder.AppendLine(string.Format("{0}; {1}", temp[0], temp[1]));
}
System.IO.File.WriteAllText(YOUR OUTPUT FILE, builder.ToString());
File.ReadAllLines: Read all lines of a file into a string[]
File.WriteAllText: Write a string into a file.
StringBuilder: This class represents a string-like object.
AppendLine: Adds a string line to the StringBuilder instance.
string.Format: Replaces each format item in a specified string with the text equivalent of a corresponding object's value. More

Related

C# How read a string to listView? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
How to load string from a txt file to the listView?
I have a txt file with three lines of characters.
I read the first line of a txt file into the first row in listView and etc.?
Well if your line contains everything you need and doesn't need to be split after you can use ListBox instead of ListView
foreach(string line in File.ReadAllLines(pathToYourFile))
ListBox.Items.Add(line);
Or if you really need ListView you can use
foreach(string line in File.ReadAllLines(pathToYourFile))
listView.Items.Add(new ListViewItem(line));
Use iostreamreader .. then use readline function .. then fill the listview
Try something like this:
string[] lines = System.IO.File.ReadAllLines(#"yourtextfile");
foreach (string line in lines)
{
listView1.Items.Add(line);
}
Here is a Linq example for you.
using System.Linq;
...
System.IO.File.ReadAllLines(pathToFile)
.ToList()
.ForEach(line => listView.Items.Add(new ListViewItem(line)));
First,
using System.IO; <-- to read the file
Then, if you can use a listbox, addrange works well without the loop:
listBox1.Items.Clear();
string[] s_array = File.ReadAllLines( -- your file path -- );
listBox1.Items.AddRange(s_array);
If you're using a list view, then the loop suggested by Blablablaster above works well

How to get No extension file type [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want get file type but my file noting extension in C#,Asp.net.
I want get file type from downloaded file with webClient.DownloadFile.
Can trace file?
Can understand downloaded file is pdf or no?
There is no absolute way to get the file type of a file with no extension.
Some file types, such as EXE, have a header that you could check. Other files, such as text files, could probably be detecting using some sort of heuristic that looks for only text characters. But still other files would not be identifiable.
As long as you know the extension beforehand it's possible, otherwise you'd have to be familiar with the file's structure. If all you're doing is downloading a pdf without the .pdf extension, rename your file [something].pdf when you download it and you'll be able to access it as a regular pdf.
There are some links where same question has been asked . I hope it helps
https://superuser.com/questions/435224/how-do-i-find-out-the-file-type-without-an-extension
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/16c34850-8799-4a8b-8702-2f59d96e79c1
http://www.troublefixers.com/open-files-without-any-valid-file-extension/
http://bytes.com/topic/c-sharp/answers/812692-open-file-without-knowing-extension
FileStream fs = new FileStream(#"c:\a.pdf", FileMode.Open, FileAccess.Read);
StreamReader r = new StreamReader(fs);
string pdfText = r.ReadToEnd();
Regex rx1 = new Regex(#"/Type\s*/Page[^s]");
MatchCollection matches = rx1.Matches(pdfText);
MessageBox.Show("The PDF file has " + matches.Count.ToString() + " page(s).";

Comparing specific columns of two csv files [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have two csv files with like 18 columns each...I would like with c# to compare the first column of the first file with the first column of the second file and the third column of the first with the third column of the second and when a difference is found,I want to be saved to another file so the third file should have as an output two columns.So somehow I need to tell which is the first column and which is the third column and then compare.
Any suggestions how can i achieve this?
The basics are reading each file line by line and then splitting each string. With a CSV file you typically have a comma as seperator, but this could be a tab or similar char as well. So use the one you have. You then get something similar to
string line;
System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.csv");
while ((line = file.ReadLine()) != null)
{
var arr = line.Split(new char[] { ',' });
// do your comparison
}
though you nee to open 2 files. You can then compare the array from file 1 with the array from file 2 (index into array == column, starting at 0). Smilarly, you can use String.Join to create your output again.
browse 2 excel sheets to a 2 Datatable. Comapre 2 tables and if there is difference put it in the new table.
Finally export the table to Excel.

Search in a text file from one symbol to another [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am coding program, and stacked. Please can give me a code which search text in file from one specific symbol to another using C# visual Windows Forms , not console application. Like this text in textfile c:\id.txt
The entry was successfully copied to {ea4c4653-cc65-11e1-a2fc-001e101f4e71}.
search string from { to } , and result with { and }, without . at the end. And send found text in a message box.Code to search text in a file an send whole line in message box. But i need part of line.
Regex can be useful:
MessageBox.Show(
Regex.Match(inputString, "\{(?<path>[^}]*)\}").Groups["path"].Value);
explain:
{ '{'
[^}]* any character except: '}'
(0 or more times, matching the most amount possible)
} '}'
Try by using regular expressions:
var line = " The entry was successfully copied to {ea4c4653-cc65-11e1-a2fc-001e101f4e71}.";
var foo = Regex.Match(line, #"to\s*\{([^}]+)\}");
if(foo.Success) {
MessageBox.Show(foo.Groups[1].Value); //ea4c4653-cc65-11e1-a2fc-001e101f4e71
} else {
//not found value
}

file is not read by completely [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have an file text with approximate 113687 lines, but my application reads only 314 lines, can anyone say why?
My code:
string file = #"z:\foo.txt";
StreamReader reader = File.OpenText(file);
string line;
int rows = 0;
while ((line = reader.ReadLine()) != null) {
++rows;
doSomethingWith(line);
// ...
}
The DoSomethingWith function is similar to:
protected static bool DoSomethingWith(string line)
{
return Regex.Match(line, #"\d+\-\d+\.\d+\.\d+\.\d+").Success;
}
Updated:
In answer to Gregs question:
Does your foo.txt contain a Ctrl+Z character on line 314?
Yes, my file contains a Control-Z character on line 314.
Text files on Windows can be terminated with a Ctrl+Z character. This means that when the file is read, the StreamReader returns end-of-file when the Ctrl+Z is encountered. Any data following the Ctrl+Z is not read.
If you wish to read the entire file without this text-mode behaviour, use File.OpenRead instead of File.OpenText.

Categories