I have a text file created to a certain path.
The contents of the text file are as follow:
%
G70
G91
X.5Y-.5
G97
G41
M04
I want to edit this text file and remove certain characters for eg. a "%" sign from the file. Following is the code I implemented but it has no effect. I think it is unable to read individual line or may be it just closes the file after reading from top to bottom. Please advice:
try
{
using (StreamReader Gcode = new StreamReader(path))
{
string line;
while ((line = Gcode.ReadLine()) != null)
{
foreach (string line2 in File.ReadAllLines(path))
{
if (line2.Contains("%"))
{
line2.Replace("%", "");
}
}
}
}
}
Loading

VulpesRank #55 · 29.2K reputationPosted Mar 24, 2011, 2:27 PM EDT
try
{
lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
if (line.Contains("%"))
{
line = line.Replace("%", "");
}
}
should be :
try
{
lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Contains("%"))
{
lines[i] = lines[i].Replace("%", "");
}
}
VulpesRank #55 · 29.2K reputationPosted Mar 24, 2011, 1:14 PM EDT
So this line:
line2.Replace("%", "");
should be:
line2 = line2.Replace("%", "");
Abhinav Mehta36 reputationPosted Mar 24, 2011, 1:17 PM EDT
I have also tried with the following code:
try
{
using (StreamReader Gcode = new StreamReader(path))
{
string line;
while ((line = Gcode.ReadLine()) != null)
{
if (line2.Contains("%"))
{
line2=line2.Replace("%", "");
}
}
}
And it has not worked.
VulpesRank #55 · 29.2K reputationPosted Mar 24, 2011, 1:26 PM EDT
Try it like this instead:
string[] lines = null;
try
{
lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Contains("%"))
{
lines[i] = lines[i].Replace("%", "");
}
}
}
catch //etc
Suthish NairRank #92 · 22.8K reputationPosted Mar 24, 2011, 1:34 PM EDT
Abhinav Mehta36 reputationPosted Mar 24, 2011, 1:40 PM EDT
Unfortunately that method is not working yet. It seems it reads all the lines and closes it rite away before the replace method could come into effect.
VulpesRank #55 · 29.2K reputationPosted Mar 24, 2011, 1:48 PM EDT
What you need to do is to make your changes and then write the array back to the file with:
File.WriteAllLines(path, lines);
which overwrites the existing file.
Abhinav Mehta36 reputationPosted Mar 24, 2011, 1:54 PM EDT
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
if (line.Contains("%"))
{
line = line.Replace("%", "");
File.WriteAllLines(path, lines);
}
}
Does not work!!!!!
VulpesRank #55 · 29.2K reputationPosted Mar 24, 2011, 1:57 PM EDT
It wants to go completely after the code we're you're changing the lines:
string[] lines = null;
try
{
lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Contains("%"))
{
lines[i] = lines[i].Replace("%", "");
}
}
}
catch //etc
try
{
File.WriteAllLines(path, lines);
}
catch // etc
Abhinav Mehta36 reputationPosted Mar 24, 2011, 2:12 PM EDT
string[] lines = null;
try
{
lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
if (line.Contains("%"))
{
line = line.Replace("%", "");
File.WriteAllLines(path, lines);
}
}
}
catch (FileNotFoundException)
{
MessageBox.Show("File not found");
}
try
{
File.WriteAllLines(path, lines);
}
catch (Exception)
{
MessageBox.Show("Edit unsuccessful");
}
Sorry, Not working out!!!
VulpesRank #55 · 29.2K reputationPosted Mar 24, 2011, 2:16 PM EDT
string path = "C:\\PROG.pmc";
string[] lines = null;
try
{
lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
if (line.Contains("%"))
{
line = line.Replace("%", "");
File.WriteAllLines(path, lines); // remove this line !!
}
}
//using (StreamReader Gcode = new StreamReader(path))
//{
// string line;
// while ((line = Gcode.ReadLine()) != null)
// {
// if (line.Contains("%"))
// {
// line = line.Replace("%", "");
// }
// }
//}
}
catch (FileNotFoundException)
{
MessageBox.Show("File not found");
}
try
{
File.WriteAllLines(path, lines);
}
catch (Exception)
{
MessageBox.Show("Edit unsuccessful");
}
Abhinav Mehta36 reputationPosted Mar 24, 2011, 2:18 PM EDT
Abhinav Mehta36 reputationPosted Mar 24, 2011, 4:09 PM EDT
What is the reason it was not working before? Substituting line=lines[i] should have worked.
VulpesRank #55 · 29.2K reputationPosted Mar 24, 2011, 4:21 PM EDT
Abhinav Mehta36 reputationPosted Mar 25, 2011, 5:43 AM EDT
It displayed System.String[] on that line.
Tried lines[i]=lines[i].Trim(' ', '\r', '\n'); Did not work.
VulpesRank #55 · 29.2K reputationPosted Mar 25, 2011, 5:50 AM EDT
Abhinav Mehta36 reputationPosted Mar 25, 2011, 5:53 AM EDT
Abhinav Mehta36 reputationPosted Mar 25, 2011, 5:57 AM EDT
Thank you!!
Abhinav Mehta36 reputationPosted Mar 25, 2011, 7:44 AM EDT
In the case if we want to remove the space before the underlined line from the following:
%
G70
G91
X.5Y-.5
G97
G41
M04
X.3535Y-.3536
X3.5
Y-3.5
X-3.5
Y3.5
M14
X0Y.5
M03
G95
X3.6465Y-.1464
G99
M30
We still have to use the LINQ query. Like for example there is G00X3.6465Y-.1464 and we want to remove G00 and also the space after G00 is deleted.
VulpesRank #55 · 29.2K reputationPosted Mar 25, 2011, 7:57 AM EDT
lines = (from line in lines where line.Trim() != "" select line).ToArray();
What you did before was to copy all the blank lines to 'lines' which is why the contents of the file disappeared when you wrote the array to the file! You should, of course, be copying the non-blank lines.
You can get rid of any leading space at the same time as you do your replacements:
try
{
lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; i++)
{
lines[i] = lines[i].TrimStart(); // remove any leading space from each line (does nothing if there isn't any space)
if (lines[i].Contains("%"))
{
lines[i] = lines[i].Replace("%", "");
}
}
}
Any other manipulations you want to carry out on the lines need to be within this block of code.
Abhinav Mehta36 reputationPosted Mar 25, 2011, 8:00 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Mar 25, 2011, 8:22 AM EDT
Abhinav Mehta36 reputationPosted Mar 25, 2011, 8:28 AM EDT
I also want to parse the entire file to look for string starting with "G" and check if it's G1 and then X then it should put a "0". It should appear as "G01". However in the file I will have codes that start with "G41" where I do not want to put a "0".
Should I use regular expressions to accomplish this?
VulpesRank #55 · 29.2K reputationPosted Mar 25, 2011, 8:33 AM EDT
You'll notice that we haven't actually declared the 'line' variable to be a string because the compiler does this for us by inferring the type from the type of 'lines'.
VulpesRank #55 · 29.2K reputationPosted Mar 25, 2011, 8:38 AM EDT
try
{
lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; i++)
{
lines[i] = lines[i].TrimStart(); // remove leading space from all lines
if (lines[i].Contains("%"))
{
lines[i] = lines[i].Replace("%", "");
}
if (lines[i].StartsWith("G1X"))
{
lines[i] = lines[i].Insert(1, "0");
}
}
}
Abhinav Mehta36 reputationPosted Mar 25, 2011, 8:43 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Mar 25, 2011, 9:02 AM EDT
Anyway this is a good thread, every time new logics trying to apply.
If am not wrong you having text like below..
and you want output like below or something else?
I used this code...
Suthish NairRank #92 · 22.8K reputationPosted Mar 25, 2011, 9:03 AM EDT
Abhinav Mehta36 reputationPosted Mar 25, 2011, 9:04 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 25, 2011, 9:07 AM EDT
It'll replace the "G1X" wherever it is in the file - not just at the beginning of a line - which is no doubt why he said he wanted to keep it in lines. You could still do it your way by replacing "\r\nG1X" (unless it's on the first line) but it starts to get complicated if you're retrieving or replacing stuff at fixed positions within lines.
Suthish NairRank #92 · 22.8K reputationPosted Mar 25, 2011, 9:30 AM EDT
Abhinav Mehta36 reputationPosted Mar 25, 2011, 12:20 PM EDT
What difference will it be if I substitute foreach statement in place of the following statement that you suggested.
for (int i = 0; i < lines.Length; i++)
Does the foreach loop not iterate to individual lines?
VulpesRank #55 · 29.2K reputationPosted Mar 25, 2011, 1:10 PM EDT
foreach(string line in lines)
{
if(line.Contains("%")) line = line.Replace("%", "");
}
the compiler will inform you that you can't assign to 'line' because it's a foreach variable. So, if it's working OK, I wouldn't mess with it if I were you :)