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
if(RS_ile==1)
{
if(RS_buf == 1) break;
if(RS_buf==49)
printf("1\n");
else
{
printf("%d\n", RS_buf);
fprintf (fp, "%d\n", RS_buf);
fclose (fp);
}
}
Everything work fine but all data was saved after I close my program. All I need is to put date to file while program is running not after closed.
Operating system Windows 8.1
Put a fflush(fp) ; after your fprintf.
Related
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
I'm witnessing some trouble compiling this code wherein you may see a nice if-else statement in practice but as soon as I compile the code, I get a "statement missing" error in the compiler to basically terminate the "if" statement before the highlighted parenthesis, which is weird but when I perform it, the compiler starts to show another error stating a "misplaced else" statement now.
What is the actual error in here and how should I proceed?
///////////////////// Input Code ////////////////////
int getcount()
{int count=0;
FILE*fp;
fp=fopen("counter.DAT","rb");
If(fp==NULL)
*{
fp=fopen("counter.DAT","wb");
count=2;
If(fp==NULL)
{
printf("\nErorr");
getch();
exit(0);
}
fwrite(&count,sizeof(int),1,fp);
count=1;
}
else
{
fread(&count,sizeof(int),1,fp);
fclose(fp);
fp=fopen("counter.DAT","wb");
If(fp==NULL)
{
printf("\nErorr");
getch();
exit(0);
}
count++;
fwrite(&count,sizeof(int),1,fp);
count--;
}
while(count>2)
{count=count-2;}
fclose(fp);
return count;}
Try a lower-case, "if"... I haven't tested it, but at first glance, that seems to be what's going on here.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Hello and thank you for attention. I am writing my own shell in c and i have problem with redirect the standard output. For example, I get command: ls -l >> file and I need to display output to file. Can you give me some ideas to resolve that problem? Thanks.
You may want to use dup() & dup2(), here are two functions I have ready:
void stdout_redirect(int fd, int *stdout_bak)
{
if (fd <= 0)
return;
*stdout_bak = dup(fileno(stdout));
dup2(fd, fileno(stdout));
close(fd);
}
void stdout_restore(int stdout_bak)
{
if (stdout_bak <= 0)
return;
dup2(stdout_bak, fileno(stdout));
close(stdout_bak);
}
Here is how to use it:
int main(void)
{
int stdout_bak;
FILE *fd;
fd = fopen("tmp", "w");
stdout_redirect(fileno(fd), &stdout_bak);
/* Your Stuff Here */
stdout_restore(&stdout_bak);
return 0;
}
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
For example, I run my program like:
program.exe < text.txt
I want the program to read from file text.txt. How do I begin doing this?
Ok, as this is a textfile, you probably want to read it line by line, so
char buf[1024];
while (fgets(buf, 1024, stdin))
{
/* do whatever you need with the line that's in buf here */
}
Note your code doesn't know about the file, it just reads from standard input. With <, you tell your environment (CMD on windows, a shell like e.g. bash on *nix) to open that file for you and provide it to your program as the standard input instead of the default, the controlling terminal, which would normally just read from the keyboard.
Hint: 1024 is kind of a random pick, most text files don't have lines exceeding 1kb. You might want to modify it to better suit your expected input.
another way to do what you are looking for is
#include <stdio.h>
int main (void) {
int c;
while ((c = fgetc(stdin)) != EOF) fputc(c, stdout);
return 0;
}
some more help here
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
Could not find proper documentation on this but I am trying to read a file using *nix system call read(). And I want read the file in 1024 byte chunks. Not sure what I have below is correct or not:
while (read(fd, buffer+i, 1024) == 1){
i++;
}
Can someone please verify?
Well if you can't use man, why not just search for it?
Anyway you are using it wrong. If you want to read it by chunks you should do it like this
// consider that we allocated enough memory for buffer
// and buffer is byte array
ssize_t r = 0, i = 0;
do {
r = read( fd, buffer + i, 1024 ); // try to read 1024 bytes
i += r;
} while( r > 0 );
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
which of these codes is better?
do {
fread( ... );
if(!feof(fp)) { ... }
}
while(!feof(fp));
or
while(1){
fread( ... );
if(!feof(fp)) { ... }
else break;
}
Thanks.
Neither. You are better off making the eof test part of the loop condition (at the top).
You can do this:
while (!feof(fp)) {
fread(...);
}
Since fread returns the number of objects read, you could should also do it this way:
while (fread(...) != 0) {
}
The while loop is better since the do while do the same operations but it's calling the feof() function twice.
which is better?
No one is better than another. The only difference in between these two that first one iterate at least once.