StreamReader does not read in carriage returns - c#

I have to write a console application for a computer course that I'm taking. The program reads text in from a file using StreamReader, splits the string into single words and saves them in a String array and then prints the words out backwards.
Whenever there is a carriage return in the file, the file stops reading in the text. Could anyone help me with this?
Here is the main program:
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace Assignment2
{
class Program
{
public String[] chop(String input)
{
input = Regex.Replace(input, #"\s+", " ");
input = input.Trim();
char[] stringSeparators = {' ', '\n', '\r'};
String[] words = input.Split(stringSeparators);
return words;
}
static void Main(string[] args)
{
Program p = new Program();
StreamReader sr = new StreamReader("input.txt");
String line = sr.ReadLine();
String[] splitWords = p.chop(line);
for (int i = 1; i <= splitWords.Length; i++)
{
Console.WriteLine(splitWords[splitWords.Length - i]);
}
Console.ReadLine();
}
}
}
And here is the file "input.txt":
This is the file you can use to
provide input to your program and later on open it inside your program to process the input.

You can use StreamReader.ReadToEnd instead of StreamReader.ReadLine.
// Cange this:
// StreamReader sr = new StreamReader("input.txt");
// String line = sr.ReadLine();
string line;
using (StreamReader sr = new StreamReader("input.txt"))
{
line = sr.ReadToEnd();
}
The addition of the using block will make sure the input file is closed properly, as well.
Another alterantive would just be to use:
string line = File.ReadAllText("input.txt"); // Read the text in one line
ReadLine reads a single line from the file, and strips off the trailing carraige return and line feed characters.
ReadToEnd will read the entire file as a single string, and preserve those characters, allowing your chop method to work as written.

You are just reading in one line. You need to read all lines till end of file.
The following should work:
String line = String.Empty;
using (StreamReader sr = new StreamReader("input.txt"))
{
while (!sr.EndOfStream)
{
line += sr.ReadLine();
}
}

The problem is that you're calling ReadLine() which does exactly that, it reads til it encounters a carriage return (you have to call it in a loop).
Typically if you want to read a file line by line with StreamReader the implementation looks more like this (from msdn);
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
The condition in the while loop ensures that you will read til the end of the file because ReadLine will return null if there is nothing to read.
Another option is just to use File.ReadAllLines(MyPath) which will return an array of strings, each element being one line in the file. To give a more complete example of that;
string[] lines = File.ReadAllLines(MyFilePath);
foreach(string line in lines)
{
string[] words = line.Split(' ').Reverse();
Console.WriteLine(String.Join(" ", words));
}
Those three lines of code do the following; Reads the entire file into a string array where each element is a line. Loops over that array, on each line we split it into the words and reverse their order. Then I join all the words back together with spaces between them and print it to the console. If you want the whole file in reverse order then you need to start at the last line instead of the first, I'll leave that detail to you.

Related

Trying to get certain line endings when using streamreader

I'm trying to get certain line endings when using streamreader in a C# app.
Code:
public static IEnumerable<string> ReadAllLines(string path)
{
if (!File.Exists(path)) return null;
List<string> lines = new List<string>();
using (var reader = new StreamReader(path))
{
while (!reader.EndOfStream)
{
lines.Add(reader.ReadLine(#"(\r\n|\n)"));
}
}
return lines.ToArray();
}
you can see where I have reader.ReadLine(#"(\r\n|\n)"); If I write reader.ReadLine(); i have no issues but when I try to add line endings to it like I found online it tells me there is no overload to ReadLine.
Question: Can someone assist me with figuring out how to add certain line endings so I can successfully scan my CSV files?
Update:
So I found a way to add the line endings i was looking for and attempted it three different ways. But I'm still getting \r only one some lines. It doesn't make a lot of sense. Can anyone see any issues with the below lines of code?
var reader = new StreamReader(path, Encoding.Default);
//string text = reader.ReadToEnd();
////// attampt 1 - this gives the best result but is still splitting an a \r in one of the fields
//// List<string> lines = new List<string>(text.Split(new[] {"\r","\n"}, StringSplitOptions.None));
////// attempt 2 This worked almost identical to the option above but seemed faster.
//var lines = Regex.Split(text, "\r\n");
//// attempt 3 - this split both \r and \n separately
// List<string> lines = new List<string>(text.Split("\r\n".ToCharArray()));
any other suggestions on how to do this would be great!
Based on your comment to your question:
so just to explain what is going on i have a CSV file. when you put it in excel i have some lines that go to ZZ and other lines that go to AZ (not as long). the white space at the end of AZ all the way to ZZ gets added to the next line and screws everything. i assumed it was because the line endings were not correct but they are as you state above
Try a String.TrimEnd() method call before adding the string to your list.
public static IEnumerable<string> ReadAllLines(string path)
{
if (!File.Exists(path)) return null;
var lines = new List<string>();
using (var reader = new StreamReader(path))
{
while (!reader.EndOfStream)
{
// add the TrimEnd call here
lines.Add(reader.ReadLine().TrimEnd());
}
}
return lines.ToArray();
}

Remove lines from Text file c#

I have a text file from which I would like to remove an entire line of text. I want to delete the entire line of text if the first amount of characters in that line exceed a certain amount before the white space starts in that line, I hope that makes sense.
Example:
1234567890************** (* = white space such as SPACE or TAB)
1234567890123********** (* = white space such as SPACE or TAB)
The amount of characters before white space in Example line 2 exceed 10 characters so the entire line of text should be deleted.
Thanks in advance.
first read the text in, line by line.
for each line check the first 10th chars. If they are not an "invalid char" (tab or space) include the line in the final string. Then store the final string. (you can overwrite the original file if needed)
List<char> invalidChars = new List<char> {" ", ";"};
string finalString = "";
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
//grab line
String line = sr.ReadToEnd();
//grab first 10 chars of the line
string firstChars = line.substring(0,10);
//check if contains
bool hasInvalidChars = false;
foreach(char c in invalidChars)
{
if(firstChars.toLowerInvariant().IndexOf(c) == 1)
hasInvalidChars = true;
}
if(!hasInvalidChars)
finalString += line + Environment.NewLine;
}
//now store results
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(#"results.txt"))
{
file.write(finalString);
}
then break the line into pieces:
include reference =>using System.IO;
give you file Path where you have place your file and then call stream writer to write that file as i have write "i am good"
using (StreamWriter sw = new StreamWriter(FilePath))
{
sw.Write("i am good");
}

Read and extract from file

I have a huge file with ~3 mill rows. Every line contains record like this:
1|2|3|4|5|6|7|8|9
Exactly 8 separators like '|' on every line. I am looking for a way to read this file then extract last '9' number only from every line and store it into another file.
edit:
Ok here is what i done already.
using (StreamReader sr = new StreamReader(filepath))
using (StreamWriter sw = new StreamWriter(filepath1))
{
string line = null;
while ((line = sr.ReadLine()) != null)
sw.WriteLine(line.Split('|')[8]);
}
File.WriteAllLines("filepath", File.ReadAllLines(filepath).Where(l => !string.IsNullOrWhiteSpace(l)));
Read file, extract last digits then write in new file and clear blank lines. Last digit is 10-15 symbols and I want to extract first 6. I continue to read and try some and when I'm done or have some question I'll edit again.
Thanks
Edit 2:
Ok, here I take first 8 digits from the number:
sw.WriteLine(line.Substring(0, Math.Min(line.Length, 8)));
Edit 3:
I have no idea how can I match now every numbers that left in file. I want to match them and to see witch number how many times is in the file.
Any help?
I am looking for a way to read this file then extract last [..] number only from every line and store it into another file.
What part exactly are you having trouble with? In psuedo code, this is what you want:
fileReader = OpenFile("input")
fileWriter = OpenFile("output")
while !fileReader.EndOfFile
line = fileReader.ReadLine
records[] = line.Split('|')
value = records[8]
fileWriter.WriteLine(value)
do
So start implementing it and feel free to ask a question on any specific line you're having trouble with. Each line of code I posted contains enough pointers to figure out the C# code or the terms to do a web search for it.
You don't say where you are stuck. Break the problem down:
Write and run minimal C# program
Read lines from file
Break up one line
write result line to a file
Are you stuck on any one of those? Then ask a specific question about that. This decomposition technique is key to many programming tasks, and indeed complex tasks in general.
You might find the string split capability useful.
Because it's a huge file you must read it line by line!
public IEnumerable ReadFileIterator(String filePath)
{
using (StreamReader streamReader = new StreamReader(filePath, Encoding.Default))
{
String line;
while ((line = streamReader.ReadLine()) != null)
{
yield return line;
}
yield break;
}
}
public void WriteToFile(String inputFilePath, String outputFilePath)
{
using (StreamWriter streamWriter = new StreamWriter(outputFilePath, true, Encoding.Default))
{
foreach (String line in ReadFileIterator(inputFilePath))
{
String[] subStrings = line.Split('|');
streamWriter.WriteLine(subStrings[8]);
}
streamWriter.Flush();
streamWriter.Close();
}
}
using (StreamReader sr = new StreamReader("input"))
using (StreamWriter sw = new StreamWriter("output"))
{
string line = null;
while ((line=sr.ReadLine())!=null)
sw.WriteLine(line.Split('|')[8]);
}
Some pointer to start from: StreamReader.Readline() and String.Split(). There are examples on both pages.
With LINQ you could do a thing like the following to filter the numbers:
var numbers = from l in File.ReadLines(fileName)
let p = l.Split('|')
select p[8];
and then write them into a new file like that:
File.WriteAllText(newFileName, String.Join("\r\n", numbers));
Use String.Split() to get the line inside an array and get the last element and store it into another file. Repeat the process for each line.
Try this...
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
string[] words = s.Split('|');
string value = words [8]
Console.WriteLine (value);
}
file.Close();

What's a way to read a specific line from a text file?

I want to read a line from a text file, except that I want to specify the line to read.
I've tried:
using (StreamReader reader = new StreamReader(#"C:\Program Files\TTVB\Users.txt"))
{
text = reader.ReadLine(acctMade);
}
acctMade is an int.
This returns:
No overload for method 'ReadLine' takes 1 arguments
If the file is not that big, you can use File.ReadAllLines to put the file into a array of strings:
string[] lines = File.ReadAllLines(#"C:\Program Files\TTVB\Users.txt");
Console.WriteLine(lines[acctMade]);
You need to use using System.IO; at the top of your code or use System.IO.File.ReadAllLines in order for it to be usable.
A variety of ways:
Read certain line in a text file (CodeProject
A simple way using StreamReader:
string GetLine(string fileName, int line)
{
using (var sr = new StreamReader(fileName)) {
for (int i = 1; i < line; i++)
sr.ReadLine();
return sr.ReadLine();
}
}
Snippet from: How do I read a specified line in a text file?
For a more efficient but complex way:
Efficient way to read a specific line number of a file. (BONUS: Python Manual Misprint)

Reading text file block by block

I am using C#.net
How can read a text file block by block, the block is separated by new line character.
Block size is not fixed, so i am not able to use ReadBlock method of StreamReader.
Is there any ohter method for getting the data block by block as it is separated by new line character.
You could use a StreamReader:
using (var reader = File.OpenText("foo.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
// do something with the line
}
}
This method reads the text file line by line (where Environment.NewLine is used as line separator) and has only the current line loaded in memory at once so it could be used to read very large files.
If you just want to load all the lines of a small text file in memory you could also use the ReadAllLines method:
string[] lines = File.ReadAllLines("foo.txt");
// the lines array will contain all the lines
// don't use this method with large files
// as it loads the entire contents into memory
You can look at StreamReader.ReadToEnd() and String.Split()
use:
string content = stream.ReadToEnd();
string[] blocks = content.Split('\n');//You may want to use "\r\n"
You could use the File.ReadLines method
foreach(string line in File.ReadLines(path))
{
//do something with line
}
ReadLines returns an IEnumerable<string> so only one line is stored in memory at once.
Something like:
using (TextReader tr = new StreamReader(FullFilePath))
{
string Line;
while ((Line = tr.ReadLine()) != null)
{
Line = Line.Trim();
}
}
Funny I was doing some file i/o the other day, and found this which was very useful at processing large delimited record text files (e.g. converting a record to a pre-defined type which you define yourself)

Categories

Resources