K
K
kytcenochka2018-04-27 14:58:06
linux
kytcenochka, 2018-04-27 14:58:06

Relationship between cpp files? How can one get and write the value of a variable from one cpp to another?

I am making two cpp files to build a library from them using makefile in LINUX
I need to write the values ​​of the variables discretX, discretY from function1.cpp to function2.cpp
How to do this?
function.h

struct InfaboutChanell
{ 
    int chanellcount;
    int analogChanell;
    int discretChanell;
};
void writeCfgFile (); 
void writeBinaryFile();

function1.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include "functions.h"
using namespace std;


 struct Station station;
   void writeCfgFile(){

        ofstream file("fileConfiguration.cfg");
       //The information about count of channels
       struct InfaboutChanell infaboutChanell;
       infaboutChanell.chanellcount=14;
       const int analogX =6;
       infaboutChanell.analogChanell=analogX;
       const int discretX=8;
       infaboutChanell.discretChanell=discretX;  
       //проверка на сумму каналов
       int sum;
       sum = infaboutChanell.analogChanell+infaboutChanell.discretChanell;
       if (sum != infaboutChanell.chanellcount)
       {
           cout << "Please enter correct count of channels: " <<'\n'; 
       }
       file<<infaboutChanell.chanellcount<<","<<infaboutChanell.analogChanell<<"A"<<","<<infaboutChanell.discretChanell<<"D"<<"\n";
      
       file.close();
   
}

function2.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include "functions.h"
using namespace std;

struct GeneralInf inf;
struct InfaboutChanell aboutchanell;


void writeBinaryFile(){
     // Create Binary file
    fstream binary_file("D:\\Struct\\Struct\\ComtradeFormat\\fileConfiguration.dat",ios::out|ios::binary|ios::app);
    if(!binary_file)
    {
        cout << "Cannot open file.\n";
        return 1;
    }

    int i=0;
    int n=0;
    int timestamp =0;

    for (i=0; i<=generalinformation.ensamp; i++){
        n = i+1;
        binary_file.write((char *) &n, sizeof n);
        timestamp = i*1000;
        binary_file.write((char *) &timestamp, sizeof timestamp);  

        // Value of analog channels
       short analogValue<b>  [analogX]</b>;
        for(int i=0; i<analogX; i++)
         
            analogValue[i] = rand() % 20000 - 10000;
        binary_file.write((char *) &analogValue, sizeof analogValue);

         unsigned __int16 m_bytesCount = 0;
         m_bytesCount = discretX / 16;
         if (discretX % 16){
             m_bytesCount++;
         }

        int ChValue<b> [discretX]</b> = {1, 1, 1, 0, 0, 0, 0, 0};
   
        for(int i = 0; i < discretX; ++i)
        {
            if(ChValue[i] == 1)
                m_bytesCount |= 1 << i;
        }

        binary_file.write((char *) &m_bytesCount, sizeof m_bytesCount);
    }

    binary_file.close();

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander Ananiev, 2018-04-27
@SaNNy32

Create global variables in the function.h file and use them to exchange data in *.cpp files.
(so-so advice)

R
res2001, 2018-04-27
@res2001

Return values ​​from one function and pass them to another as parameters.
You can return 2 values ​​in a struct or with reference input parameters. Example.

void writeCfgFile(int & directX, int & analogX){
       analogX =6;
       discretX=8;
}
void writeBinaryFile(int directX, int analogX) {
}
main() {
int analog, direct;
writeCfgFile(direct, analog);
writeBinaryFile(direct, analog);
}

When you need to return many values, they are combined into a structure and filled in the structure in the same way.
Global variables are, of course, simple and work, but this is not the right approach.

S
Sergey Sakhno, 2018-04-27
@Punk_Joker

In one module a global variable, and extern in another.

K
Kastuś, 2018-04-28
@Gytim

Well, you can transfer a pointer to an object, but this is possible if you need to transfer and change it everywhere. And so set get properties to describe.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question