저는 초보자 인 C# 프로그래머이며 건물에있는 응용 프로그램에 대한 빠른 질문이 있습니다. 내 프로세스는 텍스트 파일의 1 또는 0 파이프 구분 필드를 기반으로 특정 레코드를 제거하는 목적으로 여러 파일을 읽습니다. 파일에서 실제로 마지막으로 구분 된 필드입니다. 0 인 경우 임시 파일에 쓰고 (나중에 읽은 원본을 바꿉니다), 그렇지 않은 경우 임시 파일에 저장합니다. 너무 혼란스럽지 않게하려고하지만 파일에는 헤더 행과 두 가지 유형의 레코드가 있으며 그 다음에는 몇 가지 supp 행이옵니다. 머리글 행은 플래그가있는 유일한 레코드이므로 아래에서 확인할 수 있듯이 bool이 0으로 설정되면 좋은 레코드로 설정되면 머리글 레코드가 그 아래에있는 모든 supp 레코드와 함께 나쁜 레코드가 될 때까지 씁니다 하나의 경우에는 다음 좋은 것을 얻을 때까지 쓰지 않게됩니다.C# 문자열 구분에서 마지막 구분 된 필드 제거
그러나 지금하고 싶은 것은 (가장 쉬운 방법을 알고 싶습니다.) 마지막 파이프 구분 필드 (IE 플래그)없이 헤더 레코드를 작성하는 방법입니다. 그것은 항상 행의 마지막 2 문자 (예 : "0 |"또는 "1 |"로 시작하는 파이프가 필요합니다)이므로, inputrecord 문자열에서 문자열을 다듬어야합니까? 더 쉬운 방법이 있습니까? 레코드에 분할을 수행하는 방법이 있지만 마지막 필드를 포함하지는 않습니다 (이 경우, 필드 36)? 어떤 조언을 주시면 감사하겠습니다. 당신이 코드에서 그 시점에서 원하는 것을 항상 string[]
의 마지막 요소를 제외하고 모두 작성하는 경우 - -
static void Main(string[] args)
{
try
{
string executionDirectory = RemoveFlaggedRecords.Properties.Settings.Default.executionDirectory;
string workDirectory = RemoveFlaggedRecords.Properties.Settings.Default.workingDirectory;
string[] files = Directory.GetFiles(executionDirectory, "FilePrefix*");
foreach (string file in files)
{
string tempFile = Path.Combine(workDirectory,Path.GetFileName(file));
using (StreamReader sr = new StreamReader(file,Encoding.Default))
{
StreamWriter sw = new StreamWriter(tempFile);
string inputRecord = sr.ReadLine();
bool goodRecord = false;
bool isheaderRecord = false;
while (inputRecord != null)
{
string[] fields = inputRecord.Split('|');
if (fields[0].ToString().ToUpper() == "HEADER")
{
goodRecord = Convert.ToInt32(fields[36]) == 0;
isheaderRecord = true;
}
if (goodRecord == true && isheaderRecord == true)
{
// I'm not sure what to do here to write the string without the 36th field***
}
else if (goodRecord == true)
{
sw.WriteLine(inputRecord);
}
inputRecord = sr.ReadLine();
}
sr.Close();
sw.Close();
sw = null;
}
}
string[] newFiles = Directory.GetFiles(workDirectory, "fileprefix*");
foreach (string file in newFiles)
{
string tempFile = Path.Combine(workDirectory, Path.GetFileName(file));
string destFile = Path.Combine(executionDirectory, Path.GetFileName(file));
File.Copy(tempFile, destFile, true);
if (File.Exists(destFile))
{
File.Delete(tempFile);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
// not done
}
}
당신은 분할 수, 사용 링크의'가라 (계산)'촬영합니다 마지막 필드를 제외하고 모두 다시 입력하십시오. –