?
Solved

Reading in data from a file C#

Posted on 2006-11-28
2
Medium Priority
?
361 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 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

752 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