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");
}
Related
I am trying to read characters from a file and then append them in another file after removing the comments (which are followed by semicolon).
sample data from parent file:
Name- Harly Brown ;Name is Harley Brown
Age- 20 ;Age is 20 years
Desired result:
Name- Harley Brown
Age- 20
I am trying the following code-
StreamReader infile = new StreamReader(floc + "G" + line + ".NC0");
while (infile.Peek() != -1)
{
letter = Convert.ToChar(infile.Read());
if (letter == ';')
{
infile.ReadLine();
}
else
{
System.IO.File.AppendAllText(path, Convert.ToString(letter));
}
}
But the output i am getting is-
Name- Harley Brown Age-20
Its because AppendAllText is not working for the newline. Is there any alternative?
Sure, why not use File.AppendAllLines. See documentation here.
Appends lines to a file, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file.
It takes in any IEnumerable<string> and adds every line to the specified file. So it always adds the line on a new line.
Small example:
const string originalFile = #"D:\Temp\file.txt";
const string newFile = #"D:\Temp\newFile.txt";
// Retrieve all lines from the file.
string[] linesFromFile = File.ReadAllLines(originalFile);
List<string> linesToAppend = new List<string>();
foreach (string line in linesFromFile)
{
// 1. Split the line at the semicolon.
// 2. Take the first index, because the first part is your required result.
// 3. Trim the trailing and leading spaces.
string appendAbleLine = line.Split(';').FirstOrDefault().Trim();
// Add the line to the list of lines to append.
linesToAppend.Add(appendAbleLine);
}
// Append all lines to the file.
File.AppendAllLines(newFile, linesToAppend);
Output:
Name- Harley Brown
Age- 20
You could even change the foreach-loop into a LINQ-expression, if you prefer LINQ:
List<string> linesToAppend = linesFromFile.Select(line => line.Split(';').FirstOrDefault().Trim()).ToList();
Why use char by char comparison when .NET Framework is full of useful string manipulation functions?
Also, don't use a file write function multiple times when you can use it only one time, it's time and resources consuming!
StreamReader stream = new StreamReader("file1.txt");
string str = "";
while ((string line = infile.ReadLine()) != null) { // Get every line of the file.
line = line.Split(';')[0].Trim(); // Remove comment (right part of ;) and useless white characters.
str += line + "\n"; // Add it to our final file contents.
}
File.WriteAllText("file2.txt", str); // Write it to the new file.
You could do this with LINQ, System.File.ReadLines(string), and System.File.WriteAllLines(string, IEnumerable<string>). You could also use System.File.AppendAllLines(string, IEnumerable<string>) in a find-and-replace fashion if that was, in fact, the functionality you were going for. The difference, as the names suggest, is whether it writes everything out as a new file or if it just appends to an existing one.
System.IO.File.WriteAllLines(newPath, System.IO.File.ReadLines(oldPath).Select(c =>
{
int semicolon = c.IndexOf(';');
if (semicolon > -1)
return c.Remove(semicolon);
else
return c;
}));
In case you aren't super familiar with LINQ syntax, the idea here is to loop through each line in the file, and if it contains a semicolon (that is, IndexOf returns something that is over -1) we cut that off, and otherwise, we just return the string. Then we write all of those to the file. The StreamReader equivalent to this would be:
using (StreamReader reader = new StreamReader(oldPath))
using (StreamWriter writer = new StreamWriter(newPath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
int semicolon = line.IndexOf(';');
if (semicolon > -1)
line = c.Remove(semicolon);
writer.WriteLine(line);
}
}
Although, of course, this would feed an extra empty line at the end and the LINQ version wouldn't (as far as I know, it occurs to me that I'm not one hundred percent sure on that, but if someone reading this does know I would appreciate a comment).
Another important thing to note, just looking at your original file, you might want to add in some Trim calls, since it looks like you can have spaces before your semicolons, and I don't imagine you want those copied through.
I'm writing a small program that reads some people's firstname, surname, ID and email from an Excel sheet into the console, which isn't the problem, but instead of getting this output:
Poul EjnarRovsingpersomething#mail.com
ReneBach2014914something#mail.com
JohnJohnsson3950185something#mail.com
I want the output to be similar to this:
Poul Ejnar Rovsing per something#mail.com
Rene Bach 2014914 something#mail.com
John Johnsson 3950185 something#mail.com
The code I'm using is giving me this output, which is certainly a step in the right direction, but not quite what I'm looking for:
Poul Ejnar Rovsing per something#mail.com
Rene Bach 2014914 something#mail.com
John Johnsson 3950185 something#mail.com
And for some reason it's only outputting every other row instead of all of them, which is also puzzling me quite a bit. What am I missing here?
This is my code:
static void Main(string[] args)
{
string[] tokens;
char[] separators = {';'};
string str = "";
string newSeparator = " ";
FileStream fs = new FileStream(#"D:\Dokumenter\Skole\6. semester\GUI\Exercises\Exercise2\02 deltagerliste.csv", FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
while ((str = sr.ReadLine()) != null)
{
str = sr.ReadLine();
tokens = str.Split(separators, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(tokens[0] + newSeparator + tokens[1] + newSeparator + tokens[2] + newSeparator + tokens[3]);
}
Console.ReadLine();
}
Fixed Width Outputs
For fixed width formatting, you can take advantage of composite formatting and alignments using String.Format. For example:
String.Format("{0,10}", "name"); // output blocks of 10 characters, right aligned
String.Format("{0,-10}", "name"); // output blocks of 10 characters, left aligned
Format strings are of the form: {index[,alignment][:formatString]}. To left align an item, use a negative value for alignment.
To use this in a composite format string, you just add more format placeholders in curly brackets, the index corresponds to the index of the argument in String.Format:
var sString = "name";
var anInt = 1;
var aDecimal = 1.23M;
var s = String.Format("|{0,10}|{1,10:0}|{2,10:0.00}|", sString, anInt, aDecimal);
Output:
| name| 1| 1.23|
Line skipping
And, it is skipping every other line as every time you iterate in the while loop, you read one line, then read again:
while ((str = sr.ReadLine()) != null) // <--- first read
{
str = sr.ReadLine(); // <--- second read replaces the first one
try a do loop with the while and the read at the end
str = sr.ReadLine();
do {
... do stuff here ...
} while ((str = sr.ReadLine()) != null);
You invoke the ReadLine twice, and you are skipping a row also use \t, this indent your output in tabs.
You are reading every other line because there are two calls to the StreamReader
while ((str = sr.ReadLine()) != null)
{
str = sr.ReadLine(); // Don't need this one!
...
The first call in the While statement will advance the reader one line. The second call will advance it again, overwriting what you previously just read.
To get the spacing correct you could use \t to insert tabs, but you would still need to do some math on the size of each token so you can use the correct number of tabs. Alternatively you could use String.PadRight to make each column a specific length.
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.
The following is a line from a UTF-8 file from which I am trying to remove the special char (0X0A), which shows up as a black diamond with a question mark below:
2464577 外國法譯評 True s6620178 Unspecified <1>�1009-672
This is generated when SSIS reads a SQL table then writes out, using a flat file mgr set to code page 65001.
When I open the file up in Notepad++, displays as 0X0A.
I'm looking for some C# code to definitely strip that char out and replace it with either nothing or a blank space.
Here's what I have tried:
string fileLocation = "c:\\MyFile.txt";
var content = string.Empty;
using (StreamReader reader = new System.IO.StreamReader(fileLocation))
{
content = reader.ReadToEnd();
reader.Close();
}
content = content.Replace('\u00A0', ' ');
//also tried: content.Replace((char)0X0A, ' ');
//also tried: content.Replace((char)0X0A, '');
//also tried: content.Replace((char)0X0A, (char)'\0');
Encoding encoding = Encoding.UTF8;
using (FileStream stream = new FileStream(fileLocation, FileMode.Create))
{
using (BinaryWriter writer = new BinaryWriter(stream, encoding))
{
writer.Write(encoding.GetPreamble()); //This is for writing the BOM
writer.Write(content);
}
}
I also tried this code to get the actual string value:
byte[] bytes = { 0x0A };
string text = Encoding.UTF8.GetString(bytes);
And it comes back as "\n". So in the code above I also tried replacing "\n" with " ", both in double quotes and single quotes, but still no change.
At this point I'm out of ideas. Anyone got any advice?
Thanks.
may wanna have a look at regex replacement, for a good example of this, take a look at the post towards the bottom of this page...
http://social.msdn.microsoft.com/Forums/en-US/1b523d24-dab6-4870-a9ca-5d313d1ee602/invalid-character-returned-from-webservice
You can convert the string to a char array and loop through the array.
Then check what char the black diamond is and just remove it.
string content = "blahblah" + (char)10 + "blahblah";
char find = (char)10;
content = content.Replace(find.ToString(), "");
How can I make indent only in the second row, to be space under the first word of paragraph.
as this:
If you can read each line into an array, loop through that array, and if index = 1 then add "\t" to the beginning of the line then adding it to the output String. Let's see if I can write up an example.
Let's say you already read each line into String[] lines.
String[] lines = readFromTextFileLineByLine();
String output = "";
int index = 0;
foreach(String line in lines)
{
if(index==1)
line = "\t" + line;
output += line;
index++;
}
I hope this helps.
Now for each paragraph in a richtextbox would be a bit different. Maybe you can split the text up by "\n", instead of reading from a file line by line, then running this to change your output. You will, however, run into the issue of where a new line occurs and how large the text box is width wise.