LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2023, 10:46 PM   #1
lqmsh
Member
 
Registered: Oct 2021
Posts: 32

Rep: Reputation: Disabled
printf multiple variables in one line in C


I am trying to print two variables in one line from the following code
Code:
#include <stdio.h>

int main() {
    FILE *fptr;
    FILE *fptrr;

    // open a file in read mode
    fptr = fopen("/sys/class/power_supply/BAT0/status", "r");
    fptrr = fopen("/sys/class/power_supply/BAT0/capacity", "r");

    char batstatus[15];
    char batcap[5];
    fgets(batstatus, 15, fptr);
    fgets(batcap, 5, fptrr);

    // format the output string using sprintf
    char output[50];
    sprintf(output, "Bat: %s %s%%", batstatus, batcap);

    printf("%s", output);
    fclose(fptr);
    fclose(fptrr);
    return 0;
}
But it only return in 3 lines. What I am missing?
Thanks in advance.
 
Old 02-14-2023, 11:30 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,882
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
New-line characters should be removed from `batstatus` and `batcap`.
 
Old 02-16-2023, 04:43 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
Remove a newline at the end.
Code:
batstatus[strcspn(batstatus, "\n")] = 0;
batcap[strcspn(batcap, "\n")] = 0;
printf("Bat: %s %s%%\n", batstatus, batcap);

Last edited by MadeInGermany; 02-16-2023 at 04:45 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Printf: giving "001 002 003 004 006.... 150" in a single line dash command? Xeratul Programming 6 01-16-2011 07:00 AM
[SOLVED] printf $"Hello $var\n" vs. printf "Hello $var\n" -- not a typo. What is it? GrapefruiTgirl Programming 2 10-21-2010 08:21 AM
BASH: Each line of multiple text files gets added to one line Gavin Harper Programming 3 09-12-2010 07:31 PM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM
How is 'man 3 printf' different from 'man printf' ?? purpleburple Linux - General 3 09-23-2002 12:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration