Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am extracting data from file with:
sscanf (match, "cpu %d %d %d...", &user1, &nice1, &kernel1, &idl...);
My problem starts when I try to read it for N cores in a loop:
sscanf (match, "cpu0 %d %d..", &user1, &nic...);
sscanf (match, "cpu1 %d %d..", &user1, &nic...);
sscanf (match, "cpu2 %d %d...", &user1, &nic...);
Is there any way to insert decimal variable to this sscanf in C?
Edit:
I want to increment number next to "cpu" string to get "cpu0 %d %d", "cpu1 %d %d", "cpu2 %d %d"
Edit2 + solution:
/* Locate the line that starts with "cpuX " */
match = strstr (buffer, cpux);
/* Parse the line and extract data */
sscanf (match, "%s %d %d %d %d %d %d %d %d %d %d", *&cpuXgarbage,&...)
There is, but it is probably the wrong way to do it.
Instead of putting cpu0 in your sscanf string, why not a %s? Read the cpu0 into a string. Then it does not matter if it matches or not. You could have a read loop that looks for "cpu" at the start and stops when it doesn't match.
for (int i=0; i < 10; i++) {
sprintf(format, "cpu%d blah blah", i);
sscanf(match, format, ...);
}
int user[20];
int nic[20];
int i = 0;
sscanf (match, "cpu0 %d %d..", &(user[i++]), &(nic[i++])...);
sscanf (match, "cpu0 %d %d..", &(user[i++]), &(nic[i++])...);
.....
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
This is my code
#include<stdio.h>
int main(){
int num[5];
printf("Enter the numbers: ");
scanf("%d %d %d %d %d", &num[0] &num[1] &num[2] &num[3] &num[4]);
printf("Your numbers are : %d %d %d %d %d \n",&num[0] &num[1]&num[2]&num[3]&num[4]);
}
This is the error I get:
cpractice3.c:7:33: error: invalid operands to binary expression ('int
*' and 'int') scanf("%d %d %d %d %d", &num[0] &num[1] &num[2] &num[3] &num[4]);
~~~~~~~ ^~~~~~~ cpractice3.c:8:55: error: invalid operands to binary expression ('int *' and 'int') printf("Your
numbers are : %d %d %d %d %d \n",&num[0] &num[1] &num[2] &num[3]
&num[4]);
I have googled and figured out how to print the contents of an array using for loop.
I have seen some similar questions here where the answers pointed to int being a pointer and not the actual value.
I am trying to write a simple code which will read the user input of multiple numbers at once and store them at specific index in the array and I don't want to use for loop.
Can anyone tell me what dumb mistake I am making.
Thanks for the help.
You're missing commas between parameters.
You're passing the addresses of variables to printf instead of their values.
pass the variables and add the commas
int main(void) {
int num[5];
printf("Enter the numbers: ");
scanf("%d %d %d %d %d", &num[0],&num[1],&num[2],&num[3],&num[4]);
printf("Your numbers are : %d %d %d %d %d \n",num[0] ,num[1],num[2],num[3],num[4]);
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
void patient_data(){
char name[10];
int ID[4];
int age;
char history[100];
float temp;
int breath;
printf("enter patient data:\n");
scanf("name : %s\n", name);
scanf("ID: %d\n",ID);
scanf("age %d\n",&age);
scanf("history: %s\n", history);
scanf("temp: %f\n",&temp);
scanf("breath: %s\n", breath);
FILE *info;
info = fopen("info.txt","a");
fscanf(info,"%s %d %d %s %f %d",name, ID, &age, history, &temp,&breath);
}
this code is supposed to take user input for patient data and save it in a file that should be accessed later, but the scanf functions are not working.
any Idea on what might be the issue here?
Your scanf's are ill-formed, they should simply be:
scanf(" %9s", name); //<-- added buffer limit
scanf("%d",ID);
scanf("%d",&age);
scanf(" %99s", history); //<-- added buffer limit
scanf("%f",&temp);
scanf("%d", &breath);` // <-- corrected the specifier
If you want to print tags for the user to know what to input use printf or puts before every scanf.
Note that the last scanf has a specifier missmatch, it takes an integer but uses a string specifier.
Also note that the "%s" specifier is unsafe, you should use "%99s" for a 100 char container to avoid buffer overflow. The way you have it, it's no better than gets().
Finally the correct function to write to a file is fprintf.
fprintf(info,"%s %d %d %s %f %d", name, ID, age, history, temp, breath);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
In the following code a,b,c,d,e are "subjects" (as in class) and the variable perc is the average of the marks for the "subjects".
I can't figure out what is wrong in the code, I can't write anything after pressing enter while writing mark of first subject.
//Calculating the grade of students
#include<stdio.h>
main()
{
int a,b,c,d,e ;
printf("Enter the marks of the following subjects \n") ;
scanf("%d ,%d ,%d ,%d ,%d"), &a,&b,&c,&d,&e ;
float perc ;
perc= (a+b+c+d+e)/5 ;
printf("Your percentage is %f \n", perc);
if (perc>90)
printf("You have A grade") ;
else if (perc>70)
printf("You have B grade") ;
else if (perc>50)
printf("You have C grade") ;
else if (perc>35)
printf("You have D grade") ;
else if (perc<35)
printf("Failed") ;
}
Would appreciate some help
The problem is the usage of scanf() in reading multiple int's
in order to do so you need to get read of the "," in the scanf()
Thus replacing:
scanf("%d ,%d ,%d ,%d ,%d"), &a,&b,&c,&d,&e;
In:
scanf("%d %d %d %d %d", &a,&b,&c,&d,&e);
and also divide by 5.0 instead of 5:
perc= (a+b+c+d+e)/5.0 ;
Now it will work as expected.
P.S. you also had problem with the placing of ")"
This is a syntax problem. Replace your scanf line with the following:
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
Your code has few errors,
scanf("%d ,%d ,%d ,%d ,%d"), &a,&b,&c,&d,&e;
It should be scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
And perc = (a+b+c+d+e)/5; should be perc = (a+b+c+d+e)/5.0;
You use non-whitespace characters , in the format string. According to the function description, each such character in the format string consumes exactly one identical character from the input stream, or causes the function to fail if the next character on the stream does not compare equal. Thus, your input format shall strictly follow the following pattern any-number, any-number, any-number, any-number, any-number and only after it printed you shall press the enter key. In other cases your program fails
This question already has answers here:
Why does my output only print if there is another print after it?
(3 answers)
Closed 6 years ago.
I’m trying to convert arguments passed on the command line to int using atoi, but it is taking forever no matter whether the string is small or big. Any ideas?
int main(int argc, char *argv[]) {
int id;
int v[5];
id=atoi(argv[2]);
v[0]=atoi(argv[3]);
v[1]=atoi(argv[4]);
v[2]=atoi(argv[5]);
v[3]=atoi(argv[6]);
v[4]=atoi(argv[7]);
//conversion must be taking forever; this is never printed
printf("%d %d %d %d %d %d", id,v[0],v[1],v[2],v[3],v[4]);
return 0;
}
I suspect you are Suffering from Buffering. Change your printf line to
printf("%d %d %d %d %d %d\n", id,v[0],v[1],v[2],v[3],v[4]);
Note the added newline, \n.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to get the integers values to be passed to a linked list from a text file.
The problem is that the text file is structured as columns and rows.
This is an example below regarding the text file:
5:0:3
4:1:2
3:1:1
4:2:2
3:3:1
How can I get these values? Noting that my program should notice the : and not only the EOF. I wrote a similar program but it can't take care of the colon. it only scans the integer values until the EOF.
You can use fgets() to read lines and then sscanf() to parse each line. If the lines, contain only 3 integers then it can be done as:
int i, j, k;
char str[256];
FILE *fp = fopen("filename", "r");
if(!fp) {
/* File opening error */
}
while(fgets(str, sizeof str, fp)) {
char *p = strchr(str, '\n');
if (p) *p = 0; // remove the trailing newline, if
if( sscanf(str, "%d:%d:%d", &i, &j, &k) == 3) {
printf("%d %d %d\n", i, j, k);
/* do processing with i, j & k */
}
else {
/* failure */
}
}
You can use fscanf as
fscanf(fp, "%d:%d:%d", &var1, &var2, &var3);
You can read in the entire line as a string using getline.
Then you can use the string function find() to find the :
After that you'll have saved in what position in the string the : was found, and you can convert the character that is one position behind the : into a integer using atoi(). That works for the first 2 numbers.
For the last number, you do the same thing but instead of : you look for a space.