When I read in this file it reads in the entire line.

string filename ="f:\\date.txt";
            
            FileStream f = new FileStream(filename, FileMode.Open);
            StreamReader stream = new StreamReader(f);
            
            int line;
          

            int[] array = new int[15]; 
            for (int i = 0; i < 15; i++)
            {
               
                line = stream.Read();    // header line
                Console.WriteLine(line);
              
            }

The line it reads in from the file is 001 10:00 ON
How would I read the line if so 001 is as int, 10:00 is a string, and ON is a string?

Recommended Answers

All 3 Replies

Is the string seperated by something? By whitespaces maybe?

Maybe this will do? :

string filename = "f:\\date.txt";
            using (StreamReader stream = new StreamReader(filename))
            { 
                string line;
                while ((line = stream.ReadLine()) != null)
                {
                    string[] array = line.Split(' ');
                    int a = Convert.ToInt32(array[0]); //1.
                    string b = array[1];               //2.
                    string c = array[2];               //3.
                }
            }

Maybe this will do? :

string filename = "f:\\date.txt";
            using (StreamReader stream = new StreamReader(filename))
            { 
                string line;
                while ((line = stream.ReadLine()) != null)
                {
                    string[] array = line.Split(' ');
                    int a = Convert.ToInt32(array[0]); //1.
                    string b = array[1];               //2.
                    string c = array[2];               //3.
                }
            }

Thanks I never thought about splitting the string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.