Вызвать функцию fc (сравнение двух файлов) из C++

Discussion in 'С/С++, C#, Rust, Swift, Go, Java, Perl, Ruby' started by ~EviL~, 30 Nov 2010.

  1. ~EviL~

    ~EviL~ Elder - Старейшина

    Joined:
    14 Aug 2007
    Messages:
    169
    Likes Received:
    77
    Reputations:
    4
    Задача: вызвать функцию fc (сравнение двух файлов) из C++. В идеале: system("fc file1 file2"). Имеется:
    Code:
    #include <string>
    #include <atlstr.h>
    ...
    CString srcFile = "infile.txt";
    CString decFile = "decrypted.txt";
    ...
    if (system("fc " + srcFile + " " + decFile) == 0) {
        cout << "Files " << srcFile << " and " << decFile << " are identical." << endl;
    }
    
    Ошибка:
    Code:
    error C2664: 'system' : cannot convert parameter 1 from 'ATL::CStringT<BaseType,StringTraits>' to 'const char *'
    Я уверен, что решение очень простое, я просто очень давно не использовал C++, так что сильно не ругайте :)
     
  2. cheater_man

    cheater_man Member

    Joined:
    13 Nov 2009
    Messages:
    651
    Likes Received:
    44
    Reputations:
    7
    Code:
    #include "iostream"
    
    int main(int argc, char* argv[])
    {
    	int i = system("fc 1.txt 3.txt");
    	if(i==0)
    		printf("The different");
    	else if(i==1)
    		printf("The different");
    	else
    		printf("Error");
    	return 0;
    }
    :D
    ну а если ты имена файлов передавать как параметры, то memset, strcpy тебе в помощь, так как в ф-ции system char* =)
     
    #2 cheater_man, 30 Nov 2010
    Last edited: 30 Nov 2010
    1 person likes this.
  3. ~EviL~

    ~EviL~ Elder - Старейшина

    Joined:
    14 Aug 2007
    Messages:
    169
    Likes Received:
    77
    Reputations:
    4
    Спасибо за помощь :) Я вот так вот решил:

    Code:
    #include <string>
    ...
    string srcFile = "infile.txt";
    string decFile = "decrypted.txt";
    string cmd;
    ...
    cmd = "fc " + srcFile + " " + decFile;
    if (system(cmd.c_str()) == 0) {
    	cout << "-> Files " << srcFile << " and " << decFile << " are identical." << endl;
    }
    ...