Problem with function

Dear all,

I did a small program on Linux to monitor the temperature of my processor.
The programe works well, but I would like optimize it through a specific function.

This is the main code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  
#include <string>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>

#define T_CORE1 "/sys/class/hwmon/hwmon2/temp2_input"

using namespace std;

int main()
{
	int tC1;
	string tCore1;

	while(true)
	{
		ifstream fichierTcore1(T_CORE1);

		if(fichierTcore1)
		{
			while(getline(fichierTcore1, tCore1))
			{
				tC1 = atoi(tCore1.c_str());
			}
		}

		cout << "Temp. Core 1 : " << tC1/1000 << " °c" << endl;

		usleep(100000);
		system("clear");
	}

	return 0;
}


I tried to build a function but it doesn't work.
Could you please help me to convert the program in a function ?

I just would like build a function for this part:

1
2
3
4
5
6
7
8
9
		ifstream fichierTcore1(T_CORE1);

		if(fichierTcore1)
		{
			while(getline(fichierTcore1, tCore1))
			{
				tC1 = atoi(tCore1.c_str());
			}
		}


Thank you,

Chris
What does "it doesn't work" mean?

And how can we tell what's wrong with the function that "doesn't work" if you don't show us the code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <unistd.h>
#include <string>
#include <fstream>
#include <iostream>

#define T_CORE1 "/sys/class/hwmon/hwmon2/temp2_input"

int cpu_temp()
{
    std::ifstream file(T_CORE1);
    std::string line;

    if (std::getline(file, line))
        return atoi(line.c_str());

    throw std::runtime_error("cannot read cpu temp");
}

int main()
{
    while(true)
    {
        std::cout << "Temp. Core 1 : " << cpu_temp()/1000 << " °c" << std::endl;
        usleep(100000);
    }

    return 0;
}
Last edited on
Hello Mike,

Thanks for you reply.

The function I tried to do is:

1
2
3
4
5
6
7
8
9
10
11
12
void thermal(string fichierTcore1, string T_CORE1, string tCore1, int tC1)
{
	ifstream fichierTcore1(T_CORE1);

		if(fichierTcore1)
		{
			while(getline(fichierTcore1, tCore1))
			{
				tC1 = atoi(tCore1.c_str());
			}
		}
}


The compiler has a problem with the declaration of "fichierTcore1", and I don't how how to solve it.
The main() is a function. What "optimization" do you think that you achieve by factorization?

The /sys/class/hwmon/hwmon2/temp2_input appear to contain one line. Why do you use a loop to read it?

What do you mean by "doesn't work"? What did you do and what was the exact error?


Note that you could use a shell script like:
1
2
3
4
5
6
7
#!/bin/bash
while true
do
  echo $[$(cat /sys/class/hwmon/hwmon2/temp2_input)/1000]
  usleep 100000
  clear
done


Or a shell oneliner like:
watch -n 0.1 echo $[$(cat /sys/class/hwmon/hwmon2/temp2_input)/1000]


And probably:
watch sensors
cherault wrote:
The compiler has a problem with the declaration of "fichierTcore1", and I don't how how to solve it.


Your compiler will be giving you a specific error message, describing the problem exactly. Why are you so reluctant to share useful information with us?

If you look at your function, you'll see that you define a parameter called fichierTcore1 of type string. However you immediately declare a local variable of exactly the same name of type ifstream. That's going to cause you confusion, and will probably be the cause of your error.

I'd advise you to give your variables more helpful names generally. In that function, you have T_CORE1, tCore1 and tC1. Clearly, you mean to use those for different things, but nothing about those names indicates what those differences are. That's going to confuse you, and anyone else who looks at your code.

Make your code as self-documenting as possible. It will help keep things clear for you, and for everybody else.

Also, I'll note that the value you pass in for tC1 get overwritten at line 9, before you ever use it. Or did you mean that to be a value that gets calculated by the function and gets passed back to the calling code.
Last edited on
Thank you for your kind reply and useful informations.

Best regards,
Topic archived. No new replies allowed.