有条件地在文本文件中组合2行 [英] Combine 2 lines in text file conditionally
问题描述
我有一个vbscript(下面)我想在C#中重写但我无法得到它
工作(除非有更好的方法)。
>
如果(下一行)行包含+,则脚本将合并2行,
否则只需写入行。
#### vbscript :
Do Not Not InputFile.AtEndofStream
Line = InputFile.ReadLine
如果Len(Line)= 0那么
退出Do''在最后一行退出,因为它是空的
结束如果
如果Mid(Line,26,1)=" +"然后
out = out& Mid(Line,27)
Else
如果Len(Out)那么
OutputFile.WriteLine(out)
结束如果
out = Left(Line,15)& " ;;;" &安培; RTrim(Mid(Line,32,8))&
" ;;" &安培;中(线,66)
结束如果
循环
OutputFile.WriteLine(out)
>
OutputFile.Close()
InputFile.Close()
####下面是我无法工作的C#版本。 ..
private static void CreateText()
{
string inFile = @" c:\\ \\ text.txt" ;;
string outFile = @" C:\OutText.txt" ;;
StreamReader sr = new StreamReader(inFile);
StreamWriter sw = new StreamWriter(File.Create(outFile));
while(!(sr.EndOfStream))
{
string Line = sr.ReadLine();
if(Line.Length == 0){
goto exitDoLoopStatement0;
}
if(Line.Substring(26,1)==" +")
{
string outL = outL + Line.Substring(27);
}否则
{
if(Line.Length 0){
sw.WriteLine(outL);
}
outL = Line.Substring(0,15)+&quo吨;;" + Line.Substring(31,
8).TrimEnd()+" ;;" + Line.Substring(65);
}
}
exitDoLoopStatement0:;
sw.WriteLine(outL );
sr.Close();
sw.Close();
}
###文本文件:Text.txt
06.299 22:06:34 HBR10224W DUID = SERVER29:客户中止,
文件= C:\Program Files \Websense Reporter \ LogServer \Cache \LogServer.state
06.299 22:06:36 HBR10224W DUID = SERVER29:客户中止,
FILE = D:\Program Files \ISS\RealSecure SiteProtector\Site
Database \ data\Re
06.299 22:06:36 HBR10224W + alSecureDB.mdf
06.299 22:06:36 HBR10224W DUID = SERVER29:客户端中止,
FILE = D:\Program Files \ISS\RealSecure SiteProtector \Site
数据库\ data\Re
06.299 22:06:36 HBR10224W + alSecureDBLog.ldf
06.299 22:06 :52 HBR10224W DUID = SERVER1:客户中止,
文件= C:\\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
06.299 22:06:52 HBR10224W DUID = SERVER1:客户中止,
文件= C:\ STATIC \ trace
06.299 22:09:13 HBR10224W DUID = SERVER8:客户中止,
FILE = D:\ perfdata_DB \ perfdata_Data.MDF
任何帮助或建议非常感谢。
首先,请说明是什么错了(错误?意外输出?)所以
我们没有必要立即编译和运行示例。
其次,请注意你可以使用' 'break''声明,你使用
''goto'',这对我来说真的好看(而且我也确定那里有
这里的人可以提出更好的论据:))。
问候,
Jeroen
抱歉。错误在行
" string outL = outL + Line.Substring(27);"带有局部变量outL
可能在访问之前未初始化。此外,随后的outL
得到无法解析符号'outL''"
(如果我尝试编译,我得到'名称''outL ''不存在于
当前上下文中。
d4写道:
抱歉。错误在行
" string outL = outL + Line.Substring(27);" withLocal variable outL
在访问之前可能未初始化。此外,随后的outL
得到无法解析符号''outL''"
(如果我尝试编译,我得到名称''outL''在
当前上下文中不存在。
这是你的变量outL在你的第二个
一旦你获得该变量已离开结束支架即使在相应的else中,e如果,而不是
" while"循环已经完成,你需要在声明你的sr和sw变量的地方声明变量
。
注意我有没有检查你的代码,以确保它正在做你想做的事情
它,但这应该让你超过当前的编译错误,所以你可以
开始调试。
-
Tom Porterfield
I have a vbscript (below) I want to rewrite in C# but I cannot get it
to work (unless there is a better way to do it).
The script will combine 2 lines if the (next) line contains a "+",
otherwise just write the line.
#### vbscript:
Do While Not InputFile.AtEndofStream
Line = InputFile.ReadLine
If Len(Line) = 0 Then
Exit Do '' Exit at last line because it is empty
End If
If Mid(Line, 26, 1) = "+" Then
out = out & Mid(Line, 27)
Else
If Len(Out) Then
OutputFile.WriteLine(out)
End If
out = Left(Line, 15) & ";;" & RTrim(Mid(Line, 32, 8)) &
";;" & Mid(Line, 66)
End If
Loop
OutputFile.WriteLine(out)
OutputFile.Close()
InputFile.Close()
#### Below is the C# version I cannot get to work...
private static void CreateText()
{
string inFile = @"c:\Text.txt";
string outFile = @"C:\OutText.txt";
StreamReader sr = new StreamReader(inFile);
StreamWriter sw = new StreamWriter(File.Create(outFile));
while (!(sr.EndOfStream))
{
string Line = sr.ReadLine();
if (Line.Length == 0) {
goto exitDoLoopStatement0;
}
if (Line.Substring(26, 1) == "+")
{
string outL = outL + Line.Substring(27);
} else
{
if (Line.Length 0) {
sw.WriteLine(outL);
}
outL = Line.Substring(0, 15) + ";" + Line.Substring(31,
8).TrimEnd() + ";" + Line.Substring(65);
}
}
exitDoLoopStatement0: ;
sw.WriteLine(outL);
sr.Close();
sw.Close();
}
### Text file: Text.txt
06.299 22:06:34 HBR10224W DUID=SERVER29: ABORTED BY CLIENT,
FILE=C:\Program Files\Websense Reporter\LogServer\Cache\LogServer.state
06.299 22:06:36 HBR10224W DUID=SERVER29: ABORTED BY CLIENT,
FILE=D:\Program Files\ISS\RealSecure SiteProtector\Site
Database\Data\Re
06.299 22:06:36 HBR10224W+alSecureDB.mdf
06.299 22:06:36 HBR10224W DUID=SERVER29: ABORTED BY CLIENT,
FILE=D:\Program Files\ISS\RealSecure SiteProtector\Site
Database\Data\Re
06.299 22:06:36 HBR10224W+alSecureDBLog.ldf
06.299 22:06:52 HBR10224W DUID=SERVER1 : ABORTED BY CLIENT,
FILE=C:\STATIC\straudit.log
06.299 22:06:52 HBR10224W DUID=SERVER1 : ABORTED BY CLIENT,
FILE=C:\STATIC\strerror.log
06.299 22:06:52 HBR10224W DUID=SERVER1 : ABORTED BY CLIENT,
FILE=C:\STATIC\trace
06.299 22:09:13 HBR10224W DUID=SERVER8 : ABORTED BY CLIENT,
FILE=D:\perfdata_DB\perfdata_Data.MDF
Any help or suggestions would be greatly appreciated.
Hi,
First, please indicate what goes wrong (error? unexpected output?) so
that we don''t have to compile and run the example right away.
Second, notice that you can use the ''break'' statement where you use the
''goto'', which to me really seems nicer (and I''m also sure that there''s
people around here that can put forth better arguments :)).
Regards,
Jeroen
Sorry. The error is in the line
"string outL = outL + Line.Substring(27);" with "Local variable outL
might be not initialized before accessing". Also, the subsequent outL
get "Cannot resolve symbol ''outL''"
(If I try to compile, I get "The name ''outL'' does not exist in the
current context".
d4 wrote:Sorry. The error is in the line
"string outL = outL + Line.Substring(27);" with "Local variable outL
might be not initialized before accessing". Also, the subsequent outL
get "Cannot resolve symbol ''outL''"
(If I try to compile, I get "The name ''outL'' does not exist in the
current context".That is to be expected. Your variable outL is declared inside your second
"if" statement. As such it is scoped only to that if block. You cannot
access that variable once you have left the closing brace for the "if", not
even in the corresponding "else". Since you need to write outL once your
"while" loop has finished, you''ll need to declare the variable at the same
place where you declare your sr and sw variables.
Note that I have not checked your code to insure it is doing what you want
it to, but this should get you past the current compile error so you can
start debugging.
--
Tom Porterfield
这篇关于有条件地在文本文件中组合2行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!