我正在做一个小的 Cpp 控制台应用程序,我正在比较两个文件并查看它们是否不同。
我想知道如何将路径更改为 C:\Users\%user%\Desktop\tekst1.txt
我该在哪里做?因为我试图谷歌它,但我找不到它。
《用C++解决工程问题》一书的应用
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const string AFIL = "tekst1.txt";
const string BFIL = "tekst2.txt";
const char NEWLINE = '\n';
int main()
{
char a, b;
int linje = 1, forskellige = 0, linje_flag = 0;
ifstream afil, bfil;
afil.open(AFIL.c_str());
if (afil.fail()){
cerr << AFIL << " kan ikke åbnes\n";
exit(1);
}
bfil.open(BFIL.c_str());
if (bfil.fail()){
cerr << BFIL << " kan ikke åbnes\n";
exit(1);
}
afil.get(a);
bfil.get(b);
while ((!afil.eof()) && (!bfil.eof()))
{
if (a != b)
{
forskellige++;
linje_flag = 1;
while (a != NEWLINE && !afil.eof())
afil.get(a);
while (b != NEWLINE && !bfil.eof())
bfil.get(b);
cout << "Filerne er forskellige i linie: " << linje << endl;
}
if (a == NEWLINE)
{
linje++;
}
afil.get(a);
bfil.get(b);
}
if ((afil.eof()) != (bfil.eof()))
{
cout << "Filerne er forskellige i sidste karakter: " << linje << endl;
forskellige++;
}
if (forskellige == 0)
cout << "Filerne er ens\n";
afil.close();
bfil.close();
system("pause");
return 0;
}
最佳答案
一个快速而肮脏的解决方案是改变:
const string AFIL = "tekst1.txt";
const string BFIL = "tekst2.txt";
收件人:
const string AFIL = "C:\\Users\\%user%\\Desktop\\tekst1.txt";
const string BFIL = "C:\\Users\\%user%\\Desktop\\tekst2.txt";
您还可以尝试阅读有关为您的进程更改当前工作目录的内容(特定于操作系统,请参阅 this 以获取相应的简单 Win32 API)。然后,传递给 fstream
构造函数的所有此类路径都将与您选择的任何内容相关。
关于c++ - fstream 指向特定路径(到桌面 ex.),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26698881/