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
VulpesPosted Mar 24, 2011, 6:27 PM
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("%", "");
}
}
VulpesPosted Mar 25, 2011, 5:10 PM
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 :)
Abhinav MehtaPosted Mar 25, 2011, 4:20 PM
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?
Suthish NairPosted Mar 25, 2011, 1:30 PM
VulpesPosted Mar 25, 2011, 1:07 PM
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.
Abhinav MehtaPosted Mar 25, 2011, 1:04 PM
Suthish NairPosted Mar 25, 2011, 1:03 PM
Suthish NairPosted Mar 25, 2011, 1:02 PM
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...
Abhinav MehtaPosted Mar 25, 2011, 12:43 PM
VulpesPosted Mar 25, 2011, 12:38 PM
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");
}
}
}
VulpesPosted Mar 25, 2011, 12:33 PM
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'.
Abhinav MehtaPosted Mar 25, 2011, 12:28 PM
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?
Suthish NairPosted Mar 25, 2011, 12:22 PM
Abhinav MehtaPosted Mar 25, 2011, 12:00 PM
VulpesPosted Mar 25, 2011, 11:57 AM
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 MehtaPosted Mar 25, 2011, 11:44 AM
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.
Abhinav MehtaPosted Mar 25, 2011, 9:57 AM
Thank you!!
Abhinav MehtaPosted Mar 25, 2011, 9:53 AM
VulpesPosted Mar 25, 2011, 9:50 AM
Abhinav MehtaPosted Mar 25, 2011, 9:43 AM
It displayed System.String[] on that line.
Tried lines[i]=lines[i].Trim(' ', '\r', '\n'); Did not work.
VulpesPosted Mar 24, 2011, 8:21 PM
Abhinav MehtaPosted Mar 24, 2011, 8:09 PM
What is the reason it was not working before? Substituting line=lines[i] should have worked.
Abhinav MehtaPosted Mar 24, 2011, 6:18 PM
VulpesPosted Mar 24, 2011, 6:16 PM
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 MehtaPosted Mar 24, 2011, 6:12 PM
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!!!
VulpesPosted Mar 24, 2011, 5:57 PM
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 MehtaPosted Mar 24, 2011, 5:54 PM
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!!!!!
VulpesPosted Mar 24, 2011, 5:48 PM
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 MehtaPosted Mar 24, 2011, 5:40 PM
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.
Suthish NairPosted Mar 24, 2011, 5:34 PM
VulpesPosted Mar 24, 2011, 5:26 PM
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
Abhinav MehtaPosted Mar 24, 2011, 5:17 PM
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.
VulpesPosted Mar 24, 2011, 5:14 PM
So this line:
line2.Replace("%", "");
should be:
line2 = line2.Replace("%", "");