saiz66 Posted September 27, 2004 Share Posted September 27, 2004 How do I read the first word in every line of a file? E.g. File.txt: User name is G. And this line. Also this! So I want to read only User, And, and Also. Link to comment Share on other sites More sharing options...
0 K-LineD Posted September 27, 2004 Share Posted September 27, 2004 Just keep reading char by char comparing the char you read with a whitespace, if char readed == ' ' finish. Link to comment Share on other sites More sharing options...
0 saiz66 Posted September 27, 2004 Author Share Posted September 27, 2004 thanx a lot Link to comment Share on other sites More sharing options...
0 SoftContest Posted September 29, 2004 Share Posted September 29, 2004 That's kind of inefficient. You can use the StreamReader class to do it better: Dim sr As StreamReader = New StreamReader("TestFile.txt") Dim line As String ' Read and display the lines from the file until the end ' of the file is reached. Do ? ?line = sr.ReadLine() ? ?FirstWord = line.Split(" ", 1)(0) Loop Until line Is Nothing I'm not sure if this will work right because I wrote it off the top of my mind, but I'm sure you can fix:)t :) Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted September 29, 2004 Share Posted September 29, 2004 (edited) That's kind of inefficient. You can use the StreamReader class to do it better: Dim sr As StreamReader = New StreamReader("TestFile.txt") Dim line As String ' Read and display the lines from the file until the end ' of the file is reached. Do line = sr.ReadLine() FirstWord = line.Split(" ", 1)(0) Loop Until line Is Nothing I'm not sure if this will work right because I wrote it off the top of my mind, but I'm sure you can fix it :) This is in cshell, not vb.... And since you're using a shell, you can use cut. foreach i (`cat file.txt | cut -f 1 -d " "`) echo $i end This cuts the first stuff up to a space. At least I guess you're talking about cshell scripts (i.e. one of the *nix shells, not C the language)... Edited September 29, 2004 by kjordan2001 Link to comment Share on other sites More sharing options...
0 SoftContest Posted September 30, 2004 Share Posted September 30, 2004 Oh, sorry, I thought this was a VB.NET thread :) Nevermind me. Link to comment Share on other sites More sharing options...
Question
saiz66
How do I read the first word in every line of a file? E.g.
File.txt:
User name is G.
And this line.
Also this!
So I want to read only User, And, and Also.
Link to comment
Share on other sites
5 answers to this question
Recommended Posts