使用Extern Struct跨多个.cpp文件

时间:2018-01-26 18:37:41

标签: c++ struct extern

我有一个问题让外界工作正常或我认为。

我想做什么: 我在.h文件中有一个结构,我想在两个单独的.cpp文件中使用它。我不希望每个.cpp文件都有自己的结构,而是一个结构,其值在两个文件中共享。

这导致我从我的理解中找到“extern”我可以在我的.h文件中定义一个结构,然后通过在声明前添加extern关键字在两个.cpp文件中声明相同的结构。根据我的理解,这显示编译器在这种情况下该变量或结构在其他地方定义...它看到包含的.h文件并且看到它在我的.h中声明

简单来说,这就是我所拥有的......

我的.h文件名为project1.h

#ifndef PROJECT1_H
#define PROJECT1_H

struct proj{
char text[300];
char key[300];
char padded_key[300];
char vigenere_encrypted_text[300];
char vigenere_decrypted_text[300];
char affine_encrypted_text[300];
char affine_decrypted_text[300];
char two_block_ciphertext[300];
int mod;
char answer;
int multiplier;
int offset;
};

**proj lab1;**

#endif`#ifndef PROJECT1_H
#define PROJECT1_H

第一个名为v_encrypt.cpp的.cpp文件

#include <iostream>
 #include <fstream>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include "project1.h"

 using namespace std;


 void ReadPlainText();
 void ReadVKey();
 void PadKey();
 void VigenereEncrypt();

 **extern proj lab1;**

 int main(){
   cout << "Are we using an all lowercase alphabet(modulo = 26, enter 'l' or both lowercase and uppercase(modulo = 52, 'u'))" << endl;
   cin >> lab1.answer;
   if(lab1.answer == 'l')
      lab1.mod = 26;
   else if(lab1.answer == 'u')
      lab1.mod = 52;
   else{
      cout << "Did not pick a correct choice for alphabet... exiting now..." << endl;
      exit;
   }

   cout << "Reading plain text from plaintext.txt" << endl;
   ReadPlainText();
   cout << "Plain Text: " << lab1.text << endl;

   cout << "Reading key for vigenere from vcipherkey.txt" << endl;
   ReadVKey();
   cout << "Key: " << lab1.key << endl;

   cout << "Padding key to match length of plain text so encryption can be performed properly" << endl;
   PadKey();
   cout << "Padded Key: " << lab1.padded_key << endl;

   cout << "Performing Vigenere Encryption on plain text now..." << endl;
   VigenereEncrypt();
   cout << "Vigenere Encrypted Text: " << lab1.vigenere_encrypted_text << endl;


   return 0;
 }

void ReadPlainText(){

  ifstream plain_text("plaintext.txt");
  int index = 0;
  if(!plain_text){
    cout << "Error could not open plain text file...perhaps it does not exist" << endl;
    exit;
  }
  while(!plain_text.eof()){
    plain_text >> lab1.text[index];
    index++;
  }
  plain_text.close();
}

void ReadVKey(){

  ifstream vkey("vcipherkey.txt");
  int index = 0;
  if(!vkey){
    cout << "Error could not open the file vcipherkey.txt perhaps it does not exist" << endl;
    exit;
  }
  while(!vkey.eof()){
    vkey >> lab1.key[index];
    index++;
  }

  vkey.close();
}

void PadKey(){
    int j = 0;
    for(int i = 0; i < strlen(lab1.text); i++){
        if(j == strlen(lab1.key)){
            //cout <<" HERE " << endl;
            j = 0;
        }

            //cout <<"key_array: " << key_array[j] << endl;
            lab1.padded_key[i] = lab1.key[j];
            //cout <<"padded_key_array["<<i<<"]: " << padded_key_array[i] << endl;
            j++;
    }

}

void VigenereEncrypt(){

    for(int i = 0; i < strlen(lab1.text); i++){
        lab1.vigenere_encrypted_text[i] = ((lab1.text[i] + lab1.padded_key[i]) % lab1.mod + 'A');
    }
    ofstream vigenere_cipher_text("vigenerecipheroutput.txt");
  cout << "Writing Vigenere Encrypted text to vigenerecipheroutput.txt" << endl;
    vigenere_cipher_text.write(lab1.vigenere_encrypted_text,strlen(lab1.vigenere_encrypted_text));
    vigenere_cipher_text.close();

}

第二个.cpp文件名为affine_encrypt.cpp

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "project1.h"

using namespace std;

**extern proj lab1;**
void AffineEncrypt();

int main(){
  cout << "still have text ?: " << lab1.vigenere_encrypted_text << endl;
  cout << "Performing Affine Encryption now... " << endl;
  AffineEncrypt();
  cout << "Block Affine Encrypted text: " << lab1.affine_encrypted_text << endl;
  return 0;
}
void AffineEncrypt(){

  int j = 0;
  int k = 0;
  cout << "Dividing up the vigenere cipher text into blocks of two" << endl;
  for(int i = 0; i < strlen(lab1.vigenere_encrypted_text);i++){
      if(k==2){
          lab1.two_block_ciphertext[j] = ' ';
          k = 0;
          i--;
      }
      else{
          lab1.two_block_ciphertext[j] = lab1.vigenere_encrypted_text[i];
          k++;
      }
      j++;

  }
cout << "Padded Cipher text " << lab1.two_block_ciphertext << endl;
cout << "Enter the desired multiplier" << endl;
cin >> lab1.multiplier;
cout << "Enter the offset" << endl;
cin >> lab1.offset;
//int what=0;
for(int i = 0; i<strlen(lab1.two_block_ciphertext); i++){
  //what = (((multiplier * reading[i]) + offset) % 26);
      if((lab1.two_block_ciphertext[i]) == ' '){
          //cout << "Doing nothing on this "<< endl;
          lab1.affine_encrypted_text[i] = lab1.two_block_ciphertext[i];
      }
      else
          lab1.affine_encrypted_text[i] = (char )((((lab1.multiplier * lab1.two_block_ciphertext[i] - 'A') + lab1.offset) % 26) + 'A');
  //cout << "Value: " << what << endl;
}
ofstream affine_cipher_text("blockaffinecipheroutput.txt");
cout << "Writing Block affine encrypted text to blockaffinecipheroutput.txt" << endl;
affine_cipher_text.write(lab1.affine_encrypted_text,strlen(lab1.affine_encrypted_text));
affine_cipher_text.close();
}

本质上我在第一个.cpp文件中设置struct的值,但是我的第二个.cpp文件没有打印出我在第一个.cpp文件中设置的char数组。这不是我使用extern工作方式吗?如果这不正确使用extern原谅我。如果没有任何好方法在单独的.cpp文件中共享变量及其值?

0 个答案:

没有答案