fflush(FILE *stream) not working? [duplicate] - c

This question already has an answer here:
need of fseek() in c
(1 answer)
Closed 8 years ago.
while(1)
{
ch=fgetc(ft);
if(ch==EOF)
{
break;
}
if(ch=='u')
{
fputc('b',ft);
fflush(ft);
}
}
I tried to replace character after u with b in a file pointed by *ft.
This code runs fine but when I open the file it seemed to be unedited.
The above code works fine with fseeks(ft,0,SEEK_CUR).
Why it is not working with fflush(ft).

fflush only flushes output stream. Hence you need to put fseek(ft,0,SEEK_CUR) above your fputs(ft)

Related

Statements before non stopping while loop is not executing [duplicate]

This question already has answers here:
printf just before a delay doesn't work in C
(5 answers)
Closed 5 years ago.
Why below function is not printing "Just kidding!".
void justCheck() {
printf("Just kidding!");
while (1) {
}
}
while this is printing "Justing Kidding!" followed by non stopping "Just Kidding inside loop!".
void justCheck() {
printf("Just kidding!\n");
while (1) {
printf("Justing Kidding inside loop!\n");
}
}
can anyone please explain the logic?
Your first example
printf("Just kidding!");
The output is buffered and therefore not displayed
In the second example
printf("Just kidding!\n");
The \n at the end will flush the buffer and therefore the string will be displayed.
In the first example before the while loop insert fflush(stdout);

'\n' trouble: print standard output to the text files using FILE DESCRIPTORS in C [duplicate]

This question already has an answer here:
printf() without '\n' doesn't work in libev [duplicate]
(1 answer)
Closed 6 years ago.
I have a doubt regarding file descriptors used in C language.
I am trying to redirect standard output to a text file so that printf("") output goes in the file.
Using dup2,I have written following code snippet. The code works fine and I get 2 strings "sugar" and "spice" on screen output and "salt" is stored in the file.
But when I remove "\n" from statements: printf("sugar\n"), printf("salt\n"), printf("spice\n"),then I only get screen output as: "sugarsaltspice" and nothing is stored in the file.
Please help me regarding this as I am not able to figure out why "\n" makes a difference here.
Thanks a tonne.
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>
int main()
{
int fd;
int o = dup(1); //backup of STD_OUT
fd = open("/home/Desktop/Text2.txt",O_CREAT|O_TRUNC|O_WRONLY,0);
printf("sugar\n");
dup2(fd,1); //CHANGE STD_OUT to FILE
printf("salt\n");
dup2(o,1); //Restore STD_OUT
printf("spice\n");
return 0;
}

Is it possible to set the standard input/output to a file in C? [duplicate]

This question already has an answer here:
Redirect stdout to file without ANSI warnings
(1 answer)
Closed 6 years ago.
Can the standard input/output be set to a file for a C programme. I know that we can use fscanf and fprintf to read from and write to a file but by setting the std i/o to a file, can we use just printf/scanf for i/o operations in a C programme?
Is it possible to set the standard input/output to a file in C?
Yes, you can do that using freopen.
Example code from the above site:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
puts("stdout is printed to console");
if (freopen("redir.txt", "w", stdout) == NULL)
{
perror("freopen() failed");
return EXIT_FAILURE;
}
puts("stdout is redirected to a file"); // this is written to redir.txt
fclose(stdout);
}

printf in Linux does not work right [duplicate]

This question already has answers here:
Why does printf not flush after the call unless a newline is in the format string?
(10 answers)
Closed 7 years ago.
pid_t pid=fork();
printf("%d\n",pid);
if(pid==0){
sleep(3);
printf("!");
}
else
{
printf("#");
read_routine(clnt_sock,buf);
}
On my console, I can see two pid and !, but there is no #.
And when I delete the statement read_routine(clnt_sock,buf);, then I can see # on console.
In read_routine function, there is just some input statement using fgets().
Are there some secrets of printf?
Your output is probably buffered. After the printf, you will likely want to fflush(stdout);.
Add fflush(stdout) after print and try.

About reading file in c [duplicate]

This question already has answers here:
Why is “while( !feof(file) )” always wrong?
(5 answers)
Closed 7 years ago.
So i just got an assigment from my prof to make a program that would read or write a file . And i got some problem when reading an empty file , whenever i read an empty file it would input a space character and 0.0 . So is there any way to
Handle this ?
Here is my read code
void read()
{
n = 0 ;
FILE *f ;
f = fopen("namafile.txt","r");
if (f)
{
while (!feof(f))
{
fscanf(f,"%[^|]|%[^#]#%f\n", mhs[n].nim, mhs[n].nama, &mhs[n].x) ;
n++ ;
}
}
else
{
printf("file not found\n");
}
fclose(f) ;
}
The EOF marker will be set after fscanf() fails, so you need a failing fscanf() in order for the loop to end. Also, you have to check if fscanf() succeeded for which you check it's return value, that said all you need is to change this
while (!feof(f))
to
while (fscanf(f,"%[^|]|%[^#]#%f\n", mhs[n].nim, mhs[n].nama, &mhs[n].x) == 3)
NOTE: Don't use identifiers like f, that's a bad habit that you can porbably justify if your IDE has no autocomplete feature, but it's hardly the case, you can name your identifier file instead to make it clear what it is.

Resources