Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Simple (to you, not me) String Issue In C#

Name: Anonymous 2011-03-09 20:52

I'm trying to read financial asset prices into an array, and I have a file that's set up like so:

"Date","Open","High","Low","Close","Volume","OpenInt"
03/19/1999,97.97,97.97,97.97,97.97,0,0
03/22/1999,97.83,97.83,97.83,97.83,0,0
03/23/1999,97.79,97.79,97.79,97.79,0,0

I'm just trying to learn how to load this data into an array right now, but I am having trouble with getting the program to separate the last entry on a line from the first entry on the next line.

In the code below, I am trying to get the program to skip the headers (i.e., "Date", "Open", etc.). But, no matter what I do (i.e., require i>7 or i>6), I either lose the date on the second line along with the "Open Int" heading, or I lose both of them. I need to just get rid of the "Open Int".

Here's the code. I didn't get to using the for loop yet. The carriage return replace bit was in an effort to change the string so that it could separate the two... Replacing \r and \n produced no results...

I'm using Visual C# Express as I have not yet made my fortune in the financial markets.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            // create a reader and open the file
            StreamReader tr = new StreamReader("c:\\users\\b\\downloads\\dx\\DX00H.txt");
            StreamWriter wr = new StreamWriter("c:\\users\\b\\downloads\\dx\\output.txt");

            // read the entire file
            string myString=(tr.ReadToEnd());
       
           
            myString = myString.Replace("\"", " ");
            myString = myString.Replace("\\r", ",\\r ");
            string[] sArray =(myString.Split(','));

            string[][] aLines;


            int i = 0;
            foreach (string s in sArray)
            {
                i++;
                Console.WriteLine(s);
                if (i > 6)
                    wr.Write(s + " ");
           
            }



            // close the stream
            tr.Close();
            Console.ReadLine();

        }
    }
}

Name: Anonymous 2011-03-09 20:53

OP here, actually I am using the foreach loop, I deleted the unused for loop before I posted this code, so ignore what I said previously...

Name: Anonymous 2011-03-09 20:57

Name: Anonymous 2011-03-09 21:06

Use a CSV library. I'd be mildly surprised to learn C# doesn't ship with something for this.

Name: Anonymous 2011-03-09 21:08

That's the thing. The lines don't end with commas, and I can't seem to place commas at the ends of the lines once I've read the file into the string...

Name: Anonymous 2011-03-09 21:16

And I can't even get an extra newline to separate...

I.e.:

myString = myString.Replace("\n", "\n\n");


isn't putting any more newlines in there...

However,

myString = myString.Replace("\n", ",");

does work, but for some reason, I can't INSERT any more newlines in to the fucking thing, or they're not writing when I write to the file in the end...

Name: Anonymous 2011-03-09 21:22

Why is it Windows people never know what their EOLs look like?

Use a hex editor already. Or vim -b, but I know you don't have that installed. And please stop bumping this thread.

Name: Anonymous 2011-03-09 21:30

Relax man. It turns out that it was something weird. Environment.NewLine had to be used rather than '\n' for the second arg to the replace function.

And, you're right, I don't have vim installed. You know why? Because it's a crappy editor.

I was using Linux when it took me ten 3.5" floppies to install Slackware, but I'm not going to use software that doesn't get the job done simply.

Name: Anonymous 2011-03-10 0:51

faggot

Name: Anonymous 2011-03-10 0:59

lol at mailto:noko.

Here's what's bugging me: Don't these people realise that noko doesn't work after the first time they use it?

Name: Anonymous 2011-03-10 0:59

>>10
it's no less useful than sage

Name: Anonymous 2011-03-10 1:01

>>11
sage does something.

noko does absolutely nothing besides screaming "look at me I am a faggot who thinks NOKO works"

Name: >>10-kun 2011-03-10 1:02

did I mention that IHBT

Name: Anonymous 2011-03-10 1:35


fuck you faggot>>10
>>10

Name: Anonymous 2011-03-10 1:40

>>10
mailto:noko
Tsk.

Name: Anonymous 2011-03-10 1:44

sage? give me a break

its not polite

its FUCKING GAY

Name: Anonymous 2011-03-10 1:49

>>16
Saging shows that you give a fuck. So it's polite.

Name: Anonymous 2011-03-10 1:52

>>15
FUCK YOU FUCKING FAGGOT

Name: Anonymous 2011-03-10 2:08

I trap the spirits of the computer with my monads.

Don't change these.
Name: Email:
Entire Thread Thread List