Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I want to concatenate lots of strings together to produce a unique string which will be long enough. Here is an example code:
char *s;
s= strconcat("A big-length string",NULL);
fprintf(stdout,"%s",s);`
When I try to print it, the string is less that is printed is smaller than the actual string.
Assume that the strconcat() function works well. Do I have to malloc() some space for s??
Also when I try strlen(s) it shows me the right number like 1078. just the printing is the matter.
EDIT
The problem solved when after all strings I try to inject \n like this
s= strconcat("A big-length string","\n",NULL);
Output using printf/fprintf is usually buffered.
If the output file is connected to a terminal, output is line buffered, so the buffer will be flushed wfter each \n.
If the output file is not connected to a terminal, output is fully buffered, so the buffer will be flushed when it is full. Typical buffer sizes is 1kb-8kb.
to ensure that the buffer is flushed call:
fflush(stdout);
You can flush all file buffers with:
fflush(NULL);
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
I'm starting to write a C code after a gap of almost 10 year. I was teaching my sister and unfortunately got stuck at a very basic scanf() function for a simple interest program on Ubuntu.
My program is not giving any compilation error but it's not showing anything on screen
Here is the code:
#include <stdio.h>
int main()
{
int p,n;
float r,si;
printf("enter values of p,n,r");
scanf("%d %d %f",&p,&n,&r);
si = p*n*r/100;
printf("%f" , si);
return 0;
}
This is a common "issue" when trying to printf() and scanf() after one another. The operating system buffers stdin and stdout in order to increase performance and only flushes them when necessary. You can explicitly flush stdout by calling fflush(stdout); right after printf().
See C/C++ printf() before scanf() issue.
Your code would end up looking like
#include <stdio.h>
int main()
{
int p,n;
float r,si;
printf("enter values of p,n,r\n");
fflush(stdout); // Force stdout to be flushed
scanf("%d %d %f",&p,&n,&r);
si = p*n*r/100;
printf("%f" , si);
return 0;
}
I think you are just not looking at your console properly. The output "si" is getting printed on the same line as your first printf since you do not have a newline inserted at the end of it.
When you say
not showing anything on screen
(emphasis added), I take you to mean that when you run it, it's not displaying the data-entry prompt you're printing. This would be because the standard output is line-buffered by default when connected to a terminal -- it will buffer the output in memory before printing it, until either the buffer is filled, or (because line buffered) a newline is output, or the stream is closed (including when the program terminates normally).
One way to cause the prompt to be displayed, therefore, is to append a newline to it:
printf("enter values of p,n,r\n");
Another is to use an output function that automatically appends a newline:
puts("enter values of p,n,r");
If you want to ensure that the output appears even though no newline has been sent then you can flush the stream (standard output in this case) instead:
printf("enter values of p,n,r: ");
fflush(stdout);
Your code worked perfectly for me in my Ubuntu system.This may be because of the mistake of the IDE you are using.
Try running code in Ubuntu this way:-
save file as program.c on desktop.
open terminal and type cd Desktop (navigate to desktop).
compile code by typing gcc -o program program.c .
run program by typing ./program.
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 4 years ago.
Improve this question
I was wondering if there was any benefit/detriment to using output redirection on unix, compared to calling fprintf(file, ...);
edit: to further clarify. I'm writing a program that will need to do a data dump to disk: 50000+ lines by 40 characters of data.
The program itself takes up a significant amount of memory as it is, and I need to know whether or not allocating a buffer inside the program will incur a bigger memory penalty than using a unix output redirect.
I have noticed that the difference in output time is within margin of error. Both are very efficient and the only difference is that I need to write extra code for writing to a file without unix redirects.
However, most of my attempts at benchmarking have run up against the same issue: they don't show how much memory is allocated for the buffered output if I use output redirection.
If there is any difference, it would stem from whether a chosen file descriptor is buffered or not. stderr is often unbuffered, stdout is buffered, so if you use output redirection for the latter, you might be able to notice a tiny, possibly but unlikely statistically significant speedup compared to writing to stderr.
The underlying mechanism for writes is the same in both cases, it is the initialization phase (opening and assigning file descriptors) that will be different, but it is only performed once.
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 4 years ago.
Improve this question
I'm trying to write a very simple win32 program. I open a file for reading using CreateFile(), and then read it's content using ReadFile()
HANDLE hfile=CreatFileW(L"Capturejpg.jpg", GENERIC_READ, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
bReadResult=ReadFile(hFile, ReadBuff, BUFFERSIZE-1, &dwBytes, &OL);
when I'm reading a .txt file for example, this works just fine, the problem is when the file I am reading contains NULL (which is the case with most files) then ReadFile function stops reading(or maybe writing bytes to ReadBuff) when reaching the first NULL.
before asking I searched and found two answers.
use something other that char array and char *: for this, I don't know what else I can use, cause my goal is to read the file and search for the files extension(for example if it's .gif then the first 3 characters read "Gif")
change DCB: the problem with this one is that I have no idea what DCB is actually, how to change it, and change exactly what in it.
EDIT: other posts with the same problem: this one and this one
ReadFile does not care one bit about the content that it reads. It will quite happily read zero bytes and continue reading beyond that point in the file. It wouldn't be much use if it could not do that.
You have just misdiagnosed the problem. You have read into a character array ReadBuffer and then printed like this:
printf("%s", ReadBuffer);
Now, printf will indeed stop when it reaches a zero byte. You will need to find some other way to output the content of this file.
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a text file where every row has the format
A B:C
I want to read it using C language, but all i want to get in every line is what comes after the ':'. In other words I want to get only the C value in every line of the file, but all I want to do is read the file line-by-line and get every row's values.
Any idea to do that?
open the file for reading
fp = fopen("file.txt" , "r");
Use fgets() to read the line
fgets (str, MAX_LENGTH, fp);
Use Strtok to break according to ":"
ptr = strtok(str,":");
Read file line by line by using fgets()
fgets(line,MAX_SIZE,file_stream);
And use strchr(), to get the position from where C starts
char *ptr=strchr(line,':'); //ptr points to the : location
//now if you Move ptr location next to ':' ptr points to C
printf("C=%s",ptr+1);
And with out using additional pointer, You can do like this
printf("C=%s",strchr(line,':')+1);
Repeat these steps until reaching end of file. By checking the return value of fgets() against NULL.
Note: In this A B:C , if A or B consists : then You need to Apply logic accordingly.
You can also use strrchr()