-6

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!

user1776401
  • 39
  • 1
  • 2
  • 9
  • 2
    Why don't you show us what you've done so far so we can help you figure out what you're getting wrong? – Chris Pfohl Apr 23 '13 at 14:29
  • As I said, I'm stuck after the creation of the array... – user1776401 Apr 23 '13 at 14:31
  • 1
    Step 1: parse CSV: http://stackoverflow.com/questions/2081418/parsing-csv-files-in-c-sharp. Step 2: create text file using StreamWriter or something else. – L-Four Apr 23 '13 at 14:31
  • Code is for .NET 2.0 and I'm using 4.0. Could you post some sample code please? – user1776401 Apr 23 '13 at 14:33
  • 2
    ah, that is a huge difference. what about trying to convert it up? or you want us to do that for you? – nothrow Apr 23 '13 at 14:34
  • 3
    @user1776401: Really, don't be lazy. Research, try something, think, *develop*! – Daniel Hilgarth Apr 23 '13 at 14:35
  • I don't know how to change the delimiter and how to retrieve only the first two columns. The best would be some sample code I could also use to learn. – user1776401 Apr 23 '13 at 14:35
  • 1
    I'm not lazy at all. I've been trying all day to do it. And i don't want anyone to do something in my place...but...if this is the best advice i can get here...I could get it alone without posting anything. Post would be helpful to others, no some useless comment, as I've been searching the web for some hint but didn't find any. – user1776401 Apr 23 '13 at 14:37
  • 1
    @user1776401 We just need more information than 'how do I do this?' You didn't even give us a full line of code. You told us what classes you have used. Is `source` just an initialized array? Is it full of the lines of text? Are you having trouble writing the file? Are you having trouble skipping lines? Splitting lines? Do you see what the problem is? – Chris Pfohl Apr 23 '13 at 15:24

1 Answers1

-1

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
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116