You are not logged in.

#1 2023-02-15 06:35:08

duyinthee
Member
Registered: 2015-06-14
Posts: 222
Website

printf two contents from two files in one line in C Programming

I have just started learning C.
I am trying to read the laptop's battery battery state by the following code;

#include <stdio.h>

int main() {
    FILE *fptr;
    char batstatus[20], batcap[5];

    // open a file in read mode and read contents
    if ((fptr = fopen("/sys/class/power_supply/BAT0/status", "r")) != NULL) {
        fgets(batstatus, sizeof(batstatus), fptr);
        fclose(fptr);
    }
    if ((fptr = fopen("/sys/class/power_supply/BAT0/capacity", "r")) != NULL) {
        fgets(batcap, sizeof(batcap), fptr);
        fclose(fptr);
    }

    char output[50];
    // print formatted output string
    sprintf(output, "Bat: %s %s%%", batstatus, batcap);
    printf("%s", output);

    return 0;
}

The code print like

Bat: Discharging
 88
%[usr@hostname ]$

while I want to print like this

Bat: Discharging 88%

What I am missing?
Thanks in advance.

Offline

#2 2023-02-15 07:11:09

Head_on_a_Stick
Member
From: London
Registered: 2014-02-20
Posts: 7,680
Website

Re: printf two contents from two files in one line in C Programming

This works for me:

--- orig.c
+++ new.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <string.h>
 
 int main() {
     FILE *fptr;
@@ -14,6 +15,11 @@
         fclose(fptr);
     }
 
+
+	// remove trailing newlines
+	batstatus[strcspn(batstatus, "\n")] = 0;
+	batcap[strcspn(batcap, "\n")] = 0;
+
     char output[50];
     // print formatted output string
     sprintf(output, "Bat: %s %s%%", batstatus, batcap);

Disclaimer: this is my first time with C so I don't really know what I'm doing :-)

Offline

#3 2023-02-15 07:22:41

duyinthee
Member
Registered: 2015-06-14
Posts: 222
Website

Re: printf two contents from two files in one line in C Programming

Wow! it works. Thanks.

...
#include <string.h>
...
        // remove trailing newlines
	batstatus[strcspn(batstatus, "\n")] = 0;
	batcap[strcspn(batcap, "\n")] = 0;
...

is needed. I have learnt things from you, thanks.

Last edited by duyinthee (2023-02-15 07:23:29)

Offline

#4 2023-02-15 07:55:40

mpan
Member
Registered: 2012-08-01
Posts: 1,188
Website

Re: printf two contents from two files in one line in C Programming

On a more practical note, do not use `sprintf`, because this function is error-prone.⁽¹⁾⁽²⁾⁽³⁾⁽⁴⁾ In your particular use case it is also not needed in the first place: you may format directly to the output using `printf`.⁽⁵⁾ If a situation comes that you need to format to a string, see `snprintf`, which checks for buffer size.

`badstatus` and `batcap` remain uninitialized, if the corresponding files are not successfully read. Either handle the error or at least set their first bytes to 0, so in the case of a failure printing doesn’t read garbage.
____
⁽¹⁾ CWE-676
⁽²⁾ OWASP: Buffer overflow attack
⁽³⁾ Warning in libc documentation
⁽⁴⁾ CWE-242
⁽⁵⁾ I know, that in this particular case the value is very unlikely to ever cause an overflow. But there is also no reason to introduce problems only because we can suspect they will not manifest themselves this time.

Last edited by mpan (2023-02-15 07:56:13)


Sometimes I seem a bit harsh — don’t get offended too easily!

Offline

Board footer

Powered by FluxBB