Solved

Reading in data from a file C#

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This article will show, step by step, how to integrate R code into a R Sweave document
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now