Different while loops (do while vs infinite while), which is better? [closed] - c

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.

Related

can anyone help me? My code is not taking input [closed]

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 1 year ago.
Improve this question
I don't know why, but my code is not taking input.... where did i do mistake???
After running it just prints this:
Type your input (press enter to save and exit).🙂️
Done, your file is saved successfully🤩️
My code is:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fpp;
char Entry;
char sid;
fpp = fopen("sid","w");
if (fpp == NULL)
{
printf("Sorry🙁️ file not created\n");
exit(0);
}
printf("Type your input (press enter to save and exit).🙂️\n");
while (1)
{
putc(Entry,fpp);
if(Entry =='\n')
break;
}
printf("Done, your file is saved succesfully🤩️\n");
fclose(fpp);
return 0;
}
yes, guys I used scanf() instead of putc(). My online tutor said me to write this...
while((ch=getchar())!='\n')
{
putc(Entry,fpp);
}
and I used that.... but now I used this code and it worked.
while (1)
{
scanf("%c",&Entry);
if(Entry =='\n')
break;
}
In order to get this out of the list of unanswered questions I compile an answer from comments.
Credits to the commenters: MikeCAT, 0x5453, Vlad, Daniel Farrell.
Other commenters provided wise input, which I however do not see as immeditate parts of the solution.
Your question is not very specific about what puzzles you about the shown output so here is my guess, along with explanation:
Why does any input you provide not have any influence on program behavior?
Because you do never take any input. It seems that you intend to read input with putc(Entry,fpp);, but as 0x5453 comments
putc is used for output, not effective input.
I.e. with no input reading functions called there is no input.
Why does Done, your file is saved successfully🤩️ occur at all? It should only occur after '\n' input.
Because you have an unconditional break; in your endless loop.
That might be non-obvious, but as Daniel Farrell commetns:
... the semicolon between if(...) and break essentially makes the if a no-op... Thus break is executed the first time the loop runs.
I.e. unconditional immediate break, end of loop, output. End of program.

Is this some "Repetitive Error Loop in C?" [closed]

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.

How to get fast I/O in C language? [closed]

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 9 years ago.
Improve this question
How to get fast input/output in c? i have been using scanf and printf but in programming contest i am getting "Time limit exceeded" .please suggest which functions to use for fast I/O in c.
#include<stdio.h>
#include<math.h>
main()
{
int i,j,flag,num,t,a[500000],k;
unsigned int n;
scanf("%d",&t);
if(t<=10)
{
while(t!=0)
{
scanf("%d",&n);
if(n<=70000)
{
i=1;flag=1;num=3;a[0]=2;k=0;
while(i<n)
{
for(j=3;(j<=sqrt(num))&&(flag==1);j++)
{
if(num%j==0)
{
flag=0;
}
}
if(flag==1)
{
a[i]=num;
num=num+2;
flag=1;
printf("%d",a[k]);
k++;
i++;
}
else
{
flag=1;
num=num+2;
}
}
}
printf("%d",a[k]);
t--;
printf("\n");
}
}
}
this is the code for which i am getting "time limit exceeded"
How do you know, that your program is I/O-bound? Programming contests like http://codility.com usually specify a desired time-complexity like O(N). If you submit an algorithm with O(N^2) complexity, you just cannot win, even if you use the fastest I/O library of the world.
printf/scanf is buffered I/O. It first catch I/O in buffer. You can use fflush() function to flush buffer in memory.
fgets and puts should be substantially faster than formatted IO.

How to write from all the file from child to parent using time? [closed]

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 9 years ago.
Improve this question
I have written the following code in c, the parent read what he get from child using pipe, and write it to a new file, and the child should wait between each 50 characters he read but the problem is that parent only write one line to the file, I know there is something missing but unable to recognize it?
if(fork())
{
//Parent
read(fd[0],str,sizeof(str));
fprintf(fpnew,str,"w+");
}
else
{
//Child 1
while(fgets(str,n,fp)!=NULL)
{
write(fd[1],str,sizeof(str));
sleep(1+rand()%10);
}
}
You need to put a loop in the parent to read the child output until it is finished. fprintf is the wrong thing to use here. If you want to stick to the std C lib then you probably want fwrite here. But rather than mix and match the stdlib functions with the lower level read I tend to like to be consistent. It's just one less thing to think about. You want something more like this. (Also, read up on the parameters of the functions you are using and check return code.)
int fpnew = open("whatever", O_WRONLY);
if (pfnew < 0)
error....
int bytesread;
while ((bytesread = read(fd[0], str, sizeof(str))) != 0)
write(fpnew, str, bytesread);
close(pfnew);

Save data to file before program will close not after [closed]

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.

Resources