Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Streamwriter/reader questions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Streamwriter/reader questions

    Greetings,

    I have a couple of questions about the Streamwriter/reader functionality (as provided in the "reference samples" downloads).

    1. How is it possible to "write" information to a certain line and "tab" position of a text file in the Streamwriter? For example to write "Hi There" 12 lines down and 25 character positions in from the left in a text file.
    2. How is it possible to "read" information at a certain line/tab position of a text file in the Streamreader? For example to determine if a text reading "Hi There" exists at a postion 12 lines down and 25 character positions in from the left in a text file.

    Thanks for any information.

    #2
    Hi Burga,

    Text files are generally not formatted in advanced manners simply due to the complexity it adds to reading and writing to the file. To write to the file you would simply write in a bunch of blank lines and then your string would have a bunch of spaces to get it over to 25. To read from a file you would just read each line and use a loop to count which line you got to. Then you would compare the line of text in question with the text you are looking for. I suggest keeping the file simple in terms of formatting since it makes it easier to work with programmatically. If you are trying to generate a report of some sort then you will have to manually space out the lines in the fashion you desire.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply. I guess that shouldn't be a problem. How does one "compare" the read text file with what is desired? For example when I "read" the line that says "Hi There" am I simply comparing that with what is desired, for example:

      string line;
      while ((line = sr.ReadLine()) != null)
      {
      if(line == "Hi There"){do something;}
      }

      Is that the general idea? Or perhaps there is a command or other method?

      Comment


        #4
        Yup you would just get the StreamReader feed as a string and then it is just a matter of comparing strings.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Thank you, does the entire line string need to match exactly? Or will a match be registered if the desired text is simply contained within the read line? Thus in my previous example will a match be returned if the read string were "Hi There, my name is Bob"?

          Comment


            #6
            Needs to be an exact match unless you add specific code to strip whitespaces. That would be a matter of string manipulation. You can look to the MSDN docs for that if you wish to pursue it, but it is beyond what we can offer support for due to bandwidth restraints.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Thanks for your help.

              Comment


                #8
                Hi,

                I've copied the StreamWriter sample code provided into a strategy. I'm getting an error however, on this line:

                sw = File.AppendText(path);

                The error is "The name 'File' does not exist in the current context"...

                I don't understand I've simply copied from working code?

                Comment


                  #9
                  You are likely missing a "using" stateme at the top the file. Just copying a few lines does not necesarily mean it will work, please review the entire reference sample.
                  RayNinjaTrader Customer Service

                  Comment


                    #10
                    Thanks for that advice. That seems to solve that problem. How do I get around the issue of "Object reference not set to instance of an object" error?

                    I know I need lines like "if(sw != null)" and "if(sw == null)" to correct that, I simply do not know what to do with this:

                    if(sw == null){do what here then? What should "sw" be to prevent null?}

                    Thank you again.

                    Comment


                      #11
                      This is object oriented programming concepts and unfortunately we can not provide support for that, you would need to dig into some online tutorials (just google) to better understand this.

                      In a nutshell, if an object variable is null, then you can't access any of its methods/properties since there is no object there. Thus, you can't do anything. You need to make sure an object is assigned to an object variable, such as as creating a new one.
                      RayNinjaTrader Customer Service

                      Comment


                        #12
                        Thank you for the info.

                        Comment


                          #13
                          Hi again,

                          When reading an outside file, is it possible to only read a particular line of the file? If I feed the file into StreamReader in a loop it will read each line, but suppose I'm only interested in a certain line, for example "line 55" of the file? Possible? Thanks in advance.

                          Comment


                            #14
                            Check out MSDN DOC on StreamReader to see if there are any methods that would allow that.
                            RayNinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by swestendorf, Today, 11:14 AM
                            2 responses
                            5 views
                            0 likes
                            Last Post NinjaTrader_Kimberly  
                            Started by xiinteractive, 04-09-2024, 08:08 AM
                            4 responses
                            12 views
                            0 likes
                            Last Post xiinteractive  
                            Started by Mupulen, Today, 11:26 AM
                            0 responses
                            1 view
                            0 likes
                            Last Post Mupulen
                            by Mupulen
                             
                            Started by Sparkyboy, Today, 10:57 AM
                            1 response
                            5 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by TheMarlin801, 10-13-2020, 01:40 AM
                            21 responses
                            3,917 views
                            0 likes
                            Last Post Bidder
                            by Bidder
                             
                            Working...
                            X