C# 比较c中的两个文本文件内容#
我有两个C语言的文本文件,比如:C# 比较c中的两个文本文件内容#,c#,C#,我有两个C语言的文本文件,比如:file-A&file-B。我想比较两个文件的内容,如果在文件-A中找到的任何内容在文件-B中不存在,那么我想将该内容放在文件-B中文件-A中的相同位置 File - A File - B This is the example text. This is text. 现在,如果我们比较以上2个文件内容,则输出应为:
file-A
&file-B
。我想比较两个文件的内容,如果在文件-A
中找到的任何内容在文件-B
中不存在,那么我想将该内容放在文件-B中文件-A
中的相同位置
File - A File - B
This is the example text. This is text.
现在,如果我们比较以上2个文件内容,则输出应为:
File - B
This is the example text.
因此,如果c语言中有什么方法可以帮我做到这一点,请告诉我。我并没有完整的要求,但这里有
string[] fileAWords = File.ReadAllText("C:\\File - A.txt").Split(' ');
string[] fileBWords = File.ReadAllText("C:\\File - B.txt").Split(' ');
// The comparer makes it so the union is case insensitive
// For example: Welcome in File - A and welcome (lower-case) in File - B in a Union would both be in the result
// With the comparer, it will only appear once.
IEnumerable<string> allWords = fileAWords.Union(fileBWords, new StringEqualityComparer());
// We did the split on a space, so we want to put the space back in when we join.
File.WriteAllText("C:\\File - C.txt", string.Join(" ", allWords));
string[]fileAWords=File.ReadAllText(“C:\\File-A.txt”).Split(“”);
字符串[]fileBWords=File.ReadAllText(“C:\\File-B.txt”).Split(“”);
//比较器使其不区分大小写
//例如:联合中文件-A中的Welcome和文件-B中的Welcome(小写)都将出现在结果中
//使用比较器,它将只显示一次。
IEnumerable allWords=fileAWords.Union(fileBWords,new StringEqualityComparer());
//我们在一个空间上进行了拆分,所以我们想在加入时将该空间放回。
File.writealText(“C:\\File-C.txt”,string.Join(“,allWords));
StringEqualityComparer类代码为:
class StringEqualityComparer : IEqualityComparer<string>
{
// Lower-case your strings for a case insensitive compare.
public bool Equals(string s1, string s2)
{
if (s1.ToLower().Equals(s2.ToLower()))
{
return true;
}
else
{
return false;
}
}
#region IEqualityComparer<string> Members
public int GetHashCode(string s)
{
return s.GetHashCode();
}
#endregion
}
类StringEqualityComparer:IEqualityComparer
{
//小写字符串,用于不区分大小写的比较。
公共布尔等于(字符串s1、字符串s2)
{
如果(s1.ToLower().)等于(s2.ToLower())
{
返回true;
}
其他的
{
返回false;
}
}
#地区资格比较成员
public int GetHashCode(字符串s)
{
返回s.GetHashCode();
}
#端区
}
我并没有一套完整的需求,但这里有
string[] fileAWords = File.ReadAllText("C:\\File - A.txt").Split(' ');
string[] fileBWords = File.ReadAllText("C:\\File - B.txt").Split(' ');
// The comparer makes it so the union is case insensitive
// For example: Welcome in File - A and welcome (lower-case) in File - B in a Union would both be in the result
// With the comparer, it will only appear once.
IEnumerable<string> allWords = fileAWords.Union(fileBWords, new StringEqualityComparer());
// We did the split on a space, so we want to put the space back in when we join.
File.WriteAllText("C:\\File - C.txt", string.Join(" ", allWords));
string[]fileAWords=File.ReadAllText(“C:\\File-A.txt”).Split(“”);
字符串[]fileBWords=File.ReadAllText(“C:\\File-B.txt”).Split(“”);
//比较器使其不区分大小写
//例如:联合中文件-A中的Welcome和文件-B中的Welcome(小写)都将出现在结果中
//使用比较器,它将只显示一次。
IEnumerable allWords=fileAWords.Union(fileBWords,new StringEqualityComparer());
//我们在一个空间上进行了拆分,所以我们想在加入时将该空间放回。
File.writealText(“C:\\File-C.txt”,string.Join(“,allWords));
StringEqualityComparer类代码为:
class StringEqualityComparer : IEqualityComparer<string>
{
// Lower-case your strings for a case insensitive compare.
public bool Equals(string s1, string s2)
{
if (s1.ToLower().Equals(s2.ToLower()))
{
return true;
}
else
{
return false;
}
}
#region IEqualityComparer<string> Members
public int GetHashCode(string s)
{
return s.GetHashCode();
}
#endregion
}
类StringEqualityComparer:IEqualityComparer
{
//小写字符串,用于不区分大小写的比较。
公共布尔等于(字符串s1、字符串s2)
{
如果(s1.ToLower().)等于(s2.ToLower())
{
返回true;
}
其他的
{
返回false;
}
}
#地区资格比较成员
public int GetHashCode(字符串s)
{
返回s.GetHashCode();
}
#端区
}
试试这个:
string fileAContent = File.ReadAllText(fileAPath);
string fileBContent = File.ReadAllText(fileBPath);
string[] fileAWords = filesAContent.split(_your delimiters_);
string[] fileBWords = filesBContent.split(_your delimiters_);
if (fileAWords.Except(fileBWords).Length > 0)
{
// there are words in file B that are not in file A
}
如果要优化性能,可以在哈希集中添加fileAWords中的所有单词,然后迭代所有fileBWords并检查哈希集中是否存在不存在的工作,请尝试以下操作:
string fileAContent = File.ReadAllText(fileAPath);
string fileBContent = File.ReadAllText(fileBPath);
string[] fileAWords = filesAContent.split(_your delimiters_);
string[] fileBWords = filesBContent.split(_your delimiters_);
if (fileAWords.Except(fileBWords).Length > 0)
{
// there are words in file B that are not in file A
}
var f1 = File.ReadAllLines(@"c:\temp\l1.txt");
var f2 = File.ReadAllLines(@"c:\temp\l3.txt");
var result = f1.Select((l, index) => new {Number= index, Text = l})
.Join(f2.Select((l, index) => new {Number= index, Text = l}),
inner => inner.Number,
outer => outer.Number,
(inner, outer) => {
if(inner.Text == "")
return outer.Text;
return inner.Text;
})
.Concat(f1.Where((l, index) => index >= f2.Count()))
.Concat(f2.Where((l, index) => index >= f1.Count()));
//.Dump();
File.WriteAllLines(@"c:\temp\l3.txt", result);
如果要优化性能,可以在哈希集中添加fileAWords中的所有单词,然后迭代所有fileBWords并检查哈希集中是否存在不存在的工作
var f1 = File.ReadAllLines(@"c:\temp\l1.txt");
var f2 = File.ReadAllLines(@"c:\temp\l3.txt");
var result = f1.Select((l, index) => new {Number= index, Text = l})
.Join(f2.Select((l, index) => new {Number= index, Text = l}),
inner => inner.Number,
outer => outer.Number,
(inner, outer) => {
if(inner.Text == "")
return outer.Text;
return inner.Text;
})
.Concat(f1.Where((l, index) => index >= f2.Count()))
.Concat(f2.Where((l, index) => index >= f1.Count()));
//.Dump();
File.WriteAllLines(@"c:\temp\l3.txt", result);
这将逐行比较,如果第一个文件的行为空,将保留第二个文件的行,否则将始终打印第一个文件行
然后我们用两个文件左边的行来表示结果
这将逐行比较,如果第一个文件的行为空,将保留第二个文件的行,否则将始终打印第一个文件行
然后我们用两个文件左边的行来表示结果。一种简单的LINQ方法:
var file1 = File.ReadLines(path1);
var file2 = File.ReadAllLines(path2);
var onlyInFileA = file1.Except(file2);
File.WriteAllLines(path2, file2.Concat(onlyInFileA));
一种简单的LINQ方法:
var file1 = File.ReadLines(path1);
var file2 = File.ReadAllLines(path2);
var onlyInFileA = file1.Except(file2);
File.WriteAllLines(path2, file2.Concat(onlyInFileA));
你需要在代码中这样做吗?或者你可以使用命令行调用BeyondCompare或WinMerge或类似的方法吗?那么为什么不用文件A的内容替换整个文件B的内容呢?@TimSchmelter这是个好主意,但只有在不会删除文件-B中不存在的其他内容的情况下-A@KingKong:准确地说,您希望用文件A的内容替换文件B的内容,而不删除文件A中不存在的文件B的内容,对吗?如何处理第10行A中有内容,而在文件B中有第10行上的内容不在文件A中的情况?什么会赢?任何sameline或differentitline都不必担心,但两者都必须在输出文件(file-B)中@SeekerofKnowledge您需要在代码中执行此操作吗?或者您可以通过命令行调用BeyondCompare或WinMerge或类似命令吗?那么为什么不将整个文件B的内容替换为文件A的内容呢?@TimSchmelter这是一个好主意,但只有在不会删除文件-B中不存在的其他内容的情况下-A@KingKong:准确地说,您希望用文件A的内容替换文件B的内容,而不删除文件A中不存在的文件B的内容,对吗?如何处理第10行A中有内容,而在文件B中有第10行上的内容不在文件A中的情况?什么会赢?任何sameline或differentline都不用担心,但两者都必须在输出文件(file-B)@seekerofKnowledge中如果第一个文件中的行为空,保留第二行。如果第二个文件中的行与第一个文件中的行不同怎么办?我想他想把这一行保留在第二个文件中。可能他想在第二个文件中的行之前或之后添加第一个文件中的行。这些可能需要答案。救命啊!这个问题有点模糊,我的回答和我理解的一样:)…如果他想在之前的o之后添加一行,那就是合并像windiff、kdiff之类的文件,但这并不能解决问题,可能需要选择多个,然后使用一个带索引的匿名类…好吧,让我们来看看