Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7805397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:12:47+00:00 2026-06-02T02:12:47+00:00

I am trying to write to a specific line part using Stream Writer, here’s

  • 0

I am trying to write to a specific line part using Stream Writer, here’s what I have so far:

The exception message is giving me: Index was outside the bounds of the array.

private void WriteData(string playerName)
    {
        StreamWriter nameWriter = null;
        StringBuilder sb = new StringBuilder();

        try
        {
            string line = "";
            string namePartOne = line.Split(DELIMITER)[0];
            string namePartTwo = line.Split(DELIMITER)[1];
            if (namePartOne != EMPTY)
            {
                namePartTwo = playerName;
                string[] gameState = { namePartOne, namePartTwo, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY };
                sb.Append(gameState);
                nameWriter.WriteLine(String.Join(",", gameState));
            }
            else
            {
                nameWriter = new StreamWriter(filepath, true);
                string[] gameState = { playerName, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY };
               
                nameWriter.WriteLine(String.Join(",", gameState));
            }
        }
        catch (Exception ex)
        {
            txtTextBox2.Text = "The following problem ocurred when writing to the file:\n"
               + ex.Message;
        }
        finally
        {
            // Closing the StreamWriter should close it 
            // AND its underlying FileStream
            if (nameWriter != null)
                nameWriter.Close();
        }
    }

I am trying to out put this to my text file:

playerOneName, playerTwoName, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY 

Basically this writer is used to write player one’s from his browser, and player two’s name from another browser/tab. I first want to check if player one’s name is written in the first field, if it is, I will write then second player in the second field. The other field are left empty to describe the game state when the game is run.

I might have logic problems, please point out if you see any. Can I use regular expression to compare and validate my players are there?

I need to Stream Read this file before I can Stream Write the file, is that correct?

I am running Visual Studio ’08 using APS.NET website and forms.

Thanks for your time

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:12:50+00:00Added an answer on June 2, 2026 at 2:12 am
    string line = "";
    string namePartOne = line.Split(DELIMITER)[0];
    string namePartTwo = line.Split(DELIMITER)[1];
    

    this will result in the index out of bounds error, as you are splitting an empty string (which will return one item) and then trying to access the second item of that one-item list.

    Also, might I suggest rather than using a StringBuilder, just do

    String.Join(",", gameState);
    

    to create your output string.

    Here’s a possible overall solution (apologies for any errors, as I don’t have a compiler to hand):

    List<String> gameState;
    using (var reader = new StreamReader(filename))
        gameState = reader.ReadLine().Split(new[] {','});
    
    if (gameState[0] == "")
        gameState[0] = playerName
    else
        gameState[1] = playerName;
    
    using (var writer = new StreamWriter(filename))
        writer.WriteLine(String.Join(",", gameState));
    

    Note: ‘using’ is a handy way to get that try/finally which closes the stream for free (you’ll still have to catch exceptions though).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to check if I have write access to a specific key
I am trying to go line by line through text file using stream reader,
I'm trying to write out the specific line of code that contains the IF
I'm trying to write a macro to make a specific usage of callbacks in
I'm trying to write an inline editor for a specific type I'm writing. My
I'm trying to read binary data from a specific offset. I write the data
Trying to write a code that searches hash values for specific string's (input by
Alright, so here's my problem. I'm trying to write a script in PHP that
Here is what im trying to achieve. I have a perl script that looks
I'm trying to write line into a file at line-position n. Even if line

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.