Here's something that is probably simple but so far I haven't figured out how to do it: I have a console application that I want to pass three arguments to when I call it. The three arguments are the input file name, the field delimiter, and the output file name. The input file will be a delimited file and I need to put each field on a line into a list, so I'm using the split() function to separate the fields. The problem I'm having is the split() function seems to want a char value for the field separator but the delimiter I've passed in from the commandline is in the form of a string (i.e. "\t" for a tab separator). So far I cannot get the thing to convert the string to a char and I don't understand why not. Am I going about this the wrong way and there's an easier way to do what I want to do?

Coold you show your code?

Well, for instance I might call the program from a command prompt like this: program.exe s:\nt\dd.tsv \t s:\nt\dd.out

Here's the code in the main program:

static void Main(string[] args)

other stuff like setting console colors, checking for zero-length args, etc...

string delimIn = args[0];
char delimiter = Convert.ToChar(args[1]);
string ffOut = args[1];

CreateFixedWidthFile createFile = new CreateFixedWidthFile(delimIn, delimiter, ffOut);

Then here's the code within the class that should be doing the work:

class CreateFixedWidthFile
{
    private string inputFile;
    private char fieldDelimiter;
    private string outputFile;

    public CreateFixedWidthFile(string inputFile, char fieldDelimiter, string  outputFile) 
    {
        this.inputFile = inputFile;
        this.fieldDelimiter = fieldDelimiter;
        this.outputFile = outputFile;

        // read input file one line at a time & put fields into a list
        StreamReader sr = new StreamReader(new FileStream(inputFile, FileMode.Open, FileAccess.Read));
        List<string> empData = new List<string>();
        while (sr.Peek() != -1)
        {
            string row = sr.ReadLine();
            string[] fields = row.Split(fieldDelimiter);
            for (int i = 0; i < 19; i++)
            {
                empData.Add(fields[i]);
            }
            sr.Close();
          do other stuff
        }
}

The class will do more than that of course, but this is as far as I've gotten. The ultimate goal here is to convert a delimited file to a fixed-width file.

Based on the example string you provided I'm not sure why you couldn't just use args.Split(' ') to parse s:\nt\dd.tsv \t s:\nt\dd.out since there is a clear space between the arguments.

However... if you're intent on splitting based on string values you might want to look into setting the following at the top of your app:

using System.Text.RegularExpressions;

and utilizing

Regex.Split(string, string)

to perform your parse.

Hope this helps :) If it solves your issue please mark as solved.

Forgive my ignorance but I don't seem to be able to use args.split(), intellisense does not give me that choice. Using your regex example basically got me to the same point I already was. Let me also clarify a bit more: the arguments are coming in properly separated as far as I can tell, meaning args[0] returns "s:\\nt\\dd.tsv", args[1] returns "\\t", and args[2] returns "s:\\nt\\dd.out". Where it's generating an exception is at the point where I'm trying to convert args[1], which is "\\t", from string to char:

char delimiter = Convert.ToChar(args[1]);

The reason I'm trying to do that is to define the char for the field separator of the input file fields. The exception tells me that the string must be exactly one character long so I figure the problem is because the args[1] string is "\\t" rather than just "\t" but maybe I'm wrong.
What I want to do is pass both the input file & output file, and also the field-delimiting character, to the program from the commandline and then use that passed-in delimiting character to do a split on the input file fields; I figured by doing things that way, if the delimiter changes it wouldn't matter so much. So if there's a better way to do what I want to do, I'm open to suggestions.

Ahh, my bad.

Easy way out of that one...

int startInd = (args[1].LastIndexOf('\\') + 1);
    int indLength = ((args[1].Length - 1) - startInd);
    string testString = args[1].Substring(startInd, indLength);

testString should give you all the characters following the last instance of the \ character (the first \ is the escape sequence).

From there, if you're retrieving a single char reference after the \ (ie: '\t' becomes 't') you can convert testString to char.

Thanks, I won't be in front of my computer that has the program on it until next Tuesday but I'll give your example a shot & let you know how it goes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.