Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a data file with information like this:
3 10.9
1 2.1
10 100.5
//This is a blank line
10 200
The first is an integer and the second is a float data. It also needs to check whether a blank line exist. So I use a float x[20] array to contains it and use fgets() to get the value of each line. But how can I get back these values as printf("%d%f",x[0],x[1]); can't get back the value I wanted, it gives some strange values.
Use
fgets(buffer, sizeof buffer, filehandle);
Then use
if (sscanf(buffer, "%d %f", &Intvar, &floatvar) == 2)
// Data ready
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have an array like this
int numbers[]={5,6,5,8,9,1,-6516,8,811,981,981};
and I need to print them to screen with spaces in front of them so the total number of characters printed will be 4. si the number 5 will be printed as 3 spaces and 5 5 the number 811 will be 811 and so on.
As was previously mentioned in comments, this is something you can do with printf. I'm reluctant to write the code because a) it looks like homework and b) you'll have this nailed once you've read up on printf. (You'll need to put that printf in a for-loop to go thru the array, for sure, so read up on for as well if you need to.)
Some good resources for printf are whatever textbook you're using, plus
https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm
https://www.dummies.com/programming/c/how-to-format-with-printf-in-c-programming/
http://www.cplusplus.com/reference/cstdio/printf/ (a reference, not a tutorial)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I know it is not recommended to post code as an image, since I am getting a formatting error when trying to post code on here. Anyway, I am trying to write a simple C program to count number of words, characters, newlines using do while loop. However, the output is not as expected. Please help!
Instead of a do-while, you should try using while. Since your code starts off by checking whether a is any of your if cases, it goes to the else case, and increments the New line variable. If possible, could you share the output screen.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
So, I have some code like:
char* t2 = strtok(NULL, " \t\n,");
char regN[3];
strcpy(regN, t2);
Register* rt = getRegister(regN);
I checked several times the value of regN by printing is $t0 but the function returns NULL.
However when I tried getRegister("$t0") it returns the correct value.
The only reason I can think for this is if strtok() returns something other than the predicted value. Yes, the next token is $t0. I checked this as well.
Any suggestions or ideas what is going wrong here?
Three char long string must be stored in the 4 char array as it also needs the space for terminating zero. Your array is 3 chars long, so you have the Undefined Behaviour here
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
When I am using fread() to read the integer array one by one, it stops at 26. I understand 26 is eof, but how can I avoid this?
int data[] = { 3,8,10,62,11,40,20,6,26,7,26,20,29,31,33,10,34,37,37,33,50,55,52,59,53,49,53,8,57,16,58,20 };
int count = fwrite(data, sizeof(int), 32, fp);
Edit:
plateform is windows.
fopen with mode "rb" will sovle this issue, thanks for the comments.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I would like to store 2 variables into char array, and print our the first one as shown below.
const char *a[2];
a[0] = getCapital(bufferStore); //"Australia"
a[1] = getCurrencyCode(bufferStore); "9876.00"
printf("%s", a[0]);
However, I did not get any output. The code of getCapital and getCurrencyCode should be redundant here. The main thing I want to find out is how I can print out "Australia". I'm new to C language and pointers are really hard to understand, and my assignment is due in 2 hours. Any help will be greatly appreciated!
The file stdout, which is what printf writes to, is by default line buffered. That means everything you write to it is buffered, i.e. stored in memory, and is flushed (and actually printed) when you print a newline.