in Education by
So I tried reading the file and I confirmed it worked and then tried adding the contents to a listbox and it did not work. The way I do it is first read the file, split the file line by line and put the contents into a string array, then use a for loop to add the contents to a listbox. At first my problem was the listbox not having the contents added, then something changed and now it looks like the file isn't being read properly. I don't know if it affects anything but I did edit the file in Visual Studio so I wouldn't think that affect anything, but I'm mentioning it just in case. I am also using holdLine more than once since I am reading more than one file. Here is where I am reading the file using (StreamReader inOFile = new StreamReader("..\\..\\options.txt")) { while (!inOFile.EndOfStream) { holdLine = inOFile.ReadLine(); OptionsArr = holdLine.Split('\n'); } The file in question has these lines Employee ID Name Department Employment Status Salary The code where I try to add the contents of the string array to the listbox OptionsListBox.Items.Clear(); OptionsListBox.BeginUpdate(); //one way I tried string listOptions = string.Join(" ", Program.OptionsArr); OptionsListBox.Items.Add(listOptions.ToString()); //different way i tried for (int i = 0; i < Program.OptionsArr.Length; i++) { OptionsListBox.Items.Add(Program.OptionsArr[i]); } //another failed attempt OptionsListBox.Items.AddRange(Program.OptionsArr); OptionsListBox.EndUpdate(); JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Here is some code that demonstrates the problem. I added some Console.WriteLine() to print out what is in the variables during and after the loop. (here is a fiddle) using System; using System.IO; public class Program { const string _textFile = "This is the first line in the file\n" + "This is the second line in the file\n" + "This is the third line in the file\n"; public static void Main() { var utf8 = System.Text.Encoding.UTF8.GetBytes(_textFile); var memoryStream = new MemoryStream(utf8); memoryStream.Position = 0; string wholeLine; string[] optionsArray; using (var inOFile = new StreamReader(memoryStream)) { var count = 0; while (!inOFile.EndOfStream) { wholeLine = inOFile.ReadLine(); optionsArray = wholeLine.Split('\n'); Console.WriteLine("Reading Line {0}", ++count); Console.WriteLine("\tWhole: '{0}'", wholeLine); Console.WriteLine("\tSplit: '{0}'", string.Join("', '", optionsArray)); Console.WriteLine() } Console.WriteLine("Final Options Array: {0}", string.Join(" | ", optionsArray)); } } } The output of this program is: Reading Line 1 Whole: 'This is the first line in the file' Split: 'This is the first line in the file' Reading Line 2 Whole: 'This is the second line in the file' Split: 'This is the second line in the file' Reading Line 3 Whole: 'This is the third line in the file' Split: 'This is the third line in the file' Final Options Array: This is the third line in the file Notice how optionsArray only contains one item in it? And it's exact copy of wholeLine? That's because the ReadLine() function removes all the line breaks in the data. .Split('\n') won't be able to split anything. If I change the split character to a space, then I get this: Reading Line 1 Whole: 'This is the first line in the file' Split: 'This', 'is', 'the', 'first', 'line', 'in', 'the', 'file' Reading Line 2 Whole: 'This is the second line in the file' Split: 'This', 'is', 'the', 'second', 'line', 'in', 'the', 'file' Reading Line 3 Whole: 'This is the third line in the file' Split: 'This', 'is', 'the', 'third', 'line', 'in', 'the', 'file' Final Options Array: This | is | the | third | line | in | the | file In this case, each line is split into separate words because they are separated by a space ' '. But even if I change the splitting, optionsArray only contains the last line in the file. Your while loop is written to read the entire file, but since you never do anything to collect the results of the split operating, the rest of your code won't do anything.

Related questions

0 votes
    read the data from employee. text file and sort on age using counting sort,merge sort,quick sort and write ... sortedemponage.txt'. Select the correct answer from above options...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    I make a website on php and want to read some information from text file. the reason is if, i ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I make a website on php and want to read some information from text file. the reason is if, i ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    Is it possible to retrieve items from a Python dictionary in the order that they were inserted? JavaScript ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    Hi Folks i was googling one more hours.i did not find anything relevant, I have text File like ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I need to find a way to clear the Listbox for the refresh function which I am making. The function should ... clearing the Listbox) Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    This is a sample (edited slightly, but you get the idea) of my XML file: Test 192.168.1.1 Test ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I am doing some code golf challenges and the input is in stdin, Can I get the input in Python, If yes then how? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I basically have 2 python scripts one is for frontend other is for backend. On Frontend I have this: ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 11, 2022 in Education by JackTerrance
0 votes
    Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    how can you insert some other html text file into your one html file? write the tag used for this purpose? Select the correct answer from above options...
asked Nov 28, 2021 in Education by JackTerrance
0 votes
    Which component do you need to use in order to read data line by line from an input flow ... entries into iterative global variables? tIterateToFlow tFileList tFlowToIterate tLoop...
asked Mar 24, 2021 in Technology by JackTerrance
0 votes
    I am trying to simulate the look and feel of a Firefox disabled text input: textarea and input text. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    I am trying to simulate the look and feel of a Firefox disabled text input: textarea and input text. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    How to read an excel sheet and put the cell value within different text fields through UiPath? I have a excel ... help me out please? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
...