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.
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 1 year ago.
Improve this question
Ok, so i've been having a probelm with using int and char for my function. And is getting an error message enter image description here want to know how i should fix this with the code that i have:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char name[100];
int roll_no, chars;
float marks;
fp = fopen("records.txt", "r");
if(fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
printf("Testing fscanf() function: \n\n");
printf("Name:\t\tRoll\t\tMarks\n");
while( fscanf(fp, "Name: %s\t\tRoll no: %d\t\tMarks: %f\n"
, name, &roll_no, &marks) != EOF )
{
printf("%s\t\t%d\t\t%.2f\n", name, roll_no ,marks);
}
fclose(fp);
return 0;
}
The intedend output was this... enter image description here
Advice/help on how to use the char function at line 8 would be appericated
NOTE: This answer refers to revision 1 of the question. Meanwhile, OP has clarified the question and is asking for additional help concerning a related issue. That is why this answer does not fully address the question in its current state.
That's not an error message, that is simply a warning because you declare the variable 'chars' on line 8 and never use it in your program. Your program should be able to run even if there are compiler warnings, which are different from compiler errors.
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.
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.
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.
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.