Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need some help with using memory! (C)
#21
Quote:1. printf writes on screen, you save a letter and a parameter Tongue

but now you didn´t anser my question why do you use fprintf instad of printf?

Quote:2. void does not mean "empty" that straight-forward. It is more like an absence of type. That's why I cast the returned value.

errrrr okey

*edited fixing the quotes*
Reply
#22
Shogun do me a favour, in this thread before my post you have over 50% of the posts, dont double post please. Use the edit button, you know how to use it so USE IT.

Quote:but now you didn´t anser my question why do you use fprintf instad of printf?
All printf does is point to the screen buffer instead of a file, with fprintf by pointing it to the screen (or input location) you can use it the same way. fscanf can also be used this way.

Quote:oh and why do you use void malloc doens´t that mean that malloc is always empty
Nathan already sumerized it.
You know how casts work right? if i have a float and i want to hold an inti do float = (float)int
(void) pointers can hold any struct or character format, and while its not the best programming practice to use extensively, it very valuable for tons of things, like making a very generic linked list routine.

Also in case your wondering why you can dont have to cast in c, c already dos this for you, in c++ you have to call the casts or it wont work.
b]Hard Rock[/b]
[The Stars Dev Company] [Metal Qb flopped] [The Terror]
Stop Double Posts!
Whats better? HTML or Variables?
Reply
#23
I see
Reply
#24
fprintf doesnt print to a file as such, but rather to a file descriptor. To print to a file you first need a descriptor that points to it.
Code:
#include <stdio.h>

int main() {
  FILE *fd; /* File descriptor */

  fd = fopen("myfile.txt", "r"); /* Point the file descriptor at a file */
  
  fprintf(fd, "HelloWorld\n"); /* Print to the file descriptor */
}

stdio.h handily defines three file descriptors, stdin (standard input), stdout(standard output) and stderr(standard error). Its a bit confusing but printing to a file and printing to the screen are essentially the same thing. printf("HelloWorld\n"); is the same as writting printf(stdout, "HelloWorld\n");

By ensuring that your program prints errors to stderr you allow for greater flexibility when using your program. e.g.
Code:
$ ./myprog
myprog starting...
Error: Not enough memory
myprog ending...

$ ./myprog > stdout.txt 2> stderr.txt  

$ cat stdout.txt
myprog starting...
myprog ending....

$ cat stderr.txt
Error: Not enough memory

The redirection operators in the shell can be used to redirect stdin, stdout and stderr (and other descriptors).

Na_th_an, yeah the two OSes do use a few gotos here and there. I removed the commands directory from MINIX because it contains user mode programs:
Code:
$ cd minix/src
$ grep -r goto * | grep -v commands | wc -l
96
$ cd /usr/src/linux
$ grep -r goto * | wc -l
14837
MINIX doesnt use them nearly as much as Linux does. Linux uses them primarly for speed because unconditional jumps are much quicker than both function calls and conditional jumps.
esus saves.... Passes to Moses, shoots, he scores!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)