Solved

Reading in data from a file C#

Posted on 2006-11-28
2
359 Views
Last Modified: 2010-04-16
if i have a file such as:
in.txt
6 1 2 3 4 5 6
9 1 2 3 4 5 6 7 8 9
5 1 2 3 4 5
7 1 2 3 4 5 6 7

where the first number listed tells how many numbers are after it.  How do i write code so that i can read that in.  Normally i would do:

public static void ReadData()
            {
                  string fileName = "in.txt";

                  StreamReader fileReader = new StreamReader(fileName);

                  string sLine;

                  sLine = fileReader.ReadLine();

                  string[] split;
                  char delm = ' ';

                  while(sLine != null)
                  {
                        split = sLine.Split(delm);

      
                        
                        sLine = fileReader.ReadLine();
                  }
0
Comment
Question by:jmkotman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
nayernaguib earned 500 total points
ID: 18029565
This will store and print the contents of each line in a string array. If you don't need the first element, you can simply skip it while processing the array.

public static void ReadData()
{
      string fileName = "in.txt";
      StreamReader fileReader = new StreamReader(fileName);
      string sLine;
      string[] values;

      sLine = fileReader.ReadLine();
      char delm = ' ';

      while(sLine != null)
      {
            values = sLine.Split(delm);
            for(int i=0; i<values.Length; i++)
                  Console.Write(values[i]+" ");
            Console.WriteLine();
                   
            sLine = fileReader.ReadLine();
      }
}

_______________

  Nayer Naguib
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18031915
I like solution with split but if you need to use an other one, then scan the sting yourself.
Hint -  sLine[i] but don't forget to check if the string lenght is enough for number count
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
Make the most of your online learning experience.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question