c++ - fstream point to a specific path (to desktop ex.) -
i'm doing little cpp console application comparing 2 files , see if different.
i want know, how can alter path c:\users\%user%\desktop\tekst1.txt
do that? because have tried google it, , cant find it.
it application book "engineering problem solving 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 linie: " << linje << endl; } if (a == newline) { linje++; } afil.get(a); bfil.get(b); } if ((afil.eof()) != (bfil.eof())) { cout << "filerne er forskellige sidste karakter: " << linje << endl; forskellige++; } if (forskellige == 0) cout << "filerne er ens\n"; afil.close(); bfil.close(); system("pause"); homecoming 0; }
a quick n' dirty solution change:
const string afil = "tekst1.txt"; const string bfil = "tekst2.txt";
to:
const string afil = "c:\\users\\%user%\\desktop\\tekst1.txt"; const string bfil = "c:\\users\\%user%\\desktop\\tekst2.txt";
you can seek reading on changing current working directory process (os-specific, see this corresponding, simple win32 api). then, such paths passed fstream
constructors relative whatever choose.
c++ fstream
No comments:
Post a Comment