Solved

Reading in data from a file C#

Posted on 2006-11-28
2
328 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

896 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now