C programming, how to prevent puts from writing to console? [closed] - c

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 3 years ago.
Improve this question
So let's say you have a C program, that calls a function int foo() before returning control to main(), then main() uses puts() to write to the console.
What are some ways to prevent puts from writing to the console?
The only #includes you get are stdio and stdlib. You cannot touch any of the code in main(), only foo().
#include <stdio.h>
#include <stdlib.h>
unsigned int foo()
{
//Your code here
return 0;
}
int main()
{
foo();
puts("If you are reading this you have failed");
return 0;
}

Just redirect stdout to somewhere else (like a normal file or a character device like /dev/null) with freopen(). See the sample there
#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);
}
Adapting this to your particular case should be simple

At //Your code here, put exit(EXIT_SUCCESS);.

Related

Why withot newline charecter printf doesnot work? [closed]

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 4 years ago.
Improve this question
I was asked to write a program to blink the given word without using clrscr function in c and I am tring this following code when I add \n in the printf then only it works fine and when I remove \n it doesnt display anything why this is so?
#include <stdio.h>
#include <unistd.h>
int main()
{
int j=10;
while (j--)
{
printf("BLINK");
sleep(1);
system ("clear");
sleep(1);
}
return 0;
}
Buffering.
Output to stdout (which is where printf writes) is by default line buffered. That means the text written to stdout is put into a buffer only, and is not flushed (actually written to the device) unless either the buffer is full, or a newline is written.
Information about this should be in any good book, tutorial or class.
You can solve your problem by adding a flush of stdout.
#include <stdio.h>
#include <unistd.h>
int main()
{
int j=10;
while (j--)
{
printf("BLINK");
fflush(stdout);
sleep(1);
system ("clear");
sleep(1);
}
return 0;
}
But system("clear") will probably make code reviewers cry blood, so maybe consider something like backspace escape sequence.
#include <stdio.h>
#include <unistd.h>
int main()
{
int j=10;
while (j--)
{
printf("BLINK");
fflush(stdout);
sleep(1);
printf("\b\b\b\b\b \b\b\b\b\b");
fflush(stdout);
sleep(1);
}
return 0;
}

Unexpected value from File read [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 6 years ago.
Improve this question
I have a file y.txt that contains the text abcdefgh
this code convert file descriptor(int fd) to file pointer (FILE*), and tries to read from it.
#include <stdio.h>
#include<fcntl.h>
int main()
{
FILE *fp;
int fd = open("y.txt",O_RDONLY);
printf("%d",fd);
fp=fdopen(fd,"r");
close(fd);
char a[5];
a[4]='\0';
fread(a,2,1,fp);
printf("%s",a);
return 0;
}
The program outputs p instead of ab, as it should if it is reading from the begining of y.txt.
What am I doing wrong?
I see a problem with your code. You close(fd) before you are done with fp, which is probably causing your calls to fread to fail. This is because when you use fdopen:
The file descriptor is not dup'ed.
meaning that the underlying file that fp is trying to read from gets closed when you run close(fd). To fix this, you can remove the call to close and add a call to fclose which should close fd as well.
if you really just want a FILE * you should just be able to use fopen to open it, skipping the fd all together.
Consider your code:
char a[5];
a[4]='\0';
fread(a,2,1,fp);
This gives you a 5 character string. It is an "auto" variable and is not initialized. Then you terminate the string with a '\0'. Good. Then you fread one item of size 2. So bytes 3 and 4 are still garbage. Does this help?
Here are a few corrections to the program, as commented in the code.
#include <stdio.h>
#include <io.h> // added header
#include <fcntl.h>
int main()
{
FILE *fp;
int fd = open("y.txt",O_RDONLY);
printf("%d\n",fd); // add a newline for clarity
fp=fdopen(fd,"r");
//close(fd); // do not close before reading!
char a[5];
a[4]='\0';
fread(a,4,1,fp); // read 4 chars to fill the string space
printf("%s\n",a); // add a newline for clarity
close(fd); // close after reading
return 0;
}
File input:
abcde
Program output:
3
abcd
Although it would be better in the first place to have the simpler
FILE *fp = fopen("y.txt", "r");

Reading test cases from a file [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 7 years ago.
Improve this question
I was asked to solve a programming challenge, and there is this line which I don't understand can some one explain to me how can I pass the test cases to the program using this command, I think I have to store it in some file but I am not sure
size_t getline(char **lineptr,size_t *n, FILE *stream);
here is the full code
#include <stdio.h>
#include <stdlib.h>
size_t getline(char **lineptr,size_t *n, FILE *stream);
int main()
{
size_t maxLineLen=1024;
char *line = (char*)malloc(maxLineLen);
while(getline(&line, &maxLineLen,stdin)!= -1){
printf("Hello, World!\n");
printf("%s\n",line);
}
}
Seems you are asking how to run the given code and get input into.
getline(&line, &maxLineLen,stdin)
That reads a line from stdin. stdin is a standard file stream and is opened by the startup code for you. Without redirection, reading from stdin will get the input typed into the terminal
So to get input into the program you can do one of the following:
Run the program and then type each input line into the terminal.
Run the program and then redirect a file into the program. Example:
./my_program < my_input.txt
Are you given a file name?
Then the FILE * parameter would have to be opened via fopen.
See manual for fopen

Finding the largest of 3 numbers [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
#include <stdio.h>
int main()
{
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if (a>=b)
{
if(a>=c)
printf("Largest number = %.2f",a);
else
printf("Largest number = %.2f",c);
}
else
{
if(b>=c)
printf("Largest number = %.2f",b);
else printf("Largest number = %.2f",c);
}
return 0;
}
when I compile. the code will scan for the 3 numbers but wont do anything afterwards. even if i put {} around every if and else statements, it wont change.
As Shubham suggested, try putting something at the end of the program that prevents the windows command line interface from closing instantly.
A getchar() from stdio.h is more appropriate than getch() from conio, because it's in the standard library.
If you run the program from a command line interface and not by double clicking the icon or hitting the run-button in the IDE your program runs fine without the getchar(), as you would expect.
You can also check if your IDE supports an option to leave the command line interface open after the program has terminated.
Another option is setting a breakpoint on the last line of your main() function.
Try putting a getch() before the return 0 statement. Don't forget to #include <conio.h>.
What is happening is that the program displays the result and closes immediately.
EDIT:
Assuming you are on windows
you have include one more header file like conio.h . you may aware of this one....sorry to remember you this basic header file for output window.......then before your return 0; line just write getch();
Sample like:
#include <stdio.h>
#include <conio.h>
int main()
{
/* your code to check conditions for which number is greater among these numbers */
getch();
return 0;
}

Junk values from an input file 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 8 years ago.
Improve this question
So I have this bit of C code below. When I place printf statements to test the text from the input file, I see that I'm getting a bunch of junk values, to be more specific they are not even alphabetic or numerical, I think they are diamonds with question marks in them. I assume this means it is not processing these values the way it should be. The input file a bit of MIPS assembly code, but in this context it is only a text file. I have commented out all other parts of my program and am left with this small piece and yet I still receive the bad values. What could I possibly be doing wrong here?
The command I use to run the program on the console is:
./assembler -symbols adder.asm
Where ./assembler is the driver (argv[0])
-symbols is a tag used (argv[1])
adder.asm is the input file (argv[2])
So once opened I should be able to grab text out of this file, and it's not a problem with the file as far as I believe, it was working earlier.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
FILE *fp;
FILE *fp_out;
void main(int argc, char* argv[])
{
int mode;
if (strcmp(argv[1], "-symbols") == 0)
{
fp = fopen(argv[2], "r");
mode = 1;
}
else
{
fp = fopen(argv[1], "r");
fp_out = fopen(argv[2], "w");
mode = 2;
}
}
Try to add the following line right after the open section and add #include <errno.h> to the beginning.
printf("%p, %p, %d\n", fp, ftp_out, errno);
If the fp is null then there is some problem opening the file. If you do not check the return value, you can read from a wrong buffer. Maybe there is some permission problems (or whatever). Also if errno != 0 you have a problem. Check with perror <num> the errno value in command line (or see perror(3) function).

Resources