Answer the question
In order to leave comments, you need to log in
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();
#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();
}
#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 *) ×tamp, 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
Create global variables in the function.h file and use them to exchange data in *.cpp files.
(so-so advice)
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);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question