CT GunZ Server
Would you like to react to this message? Create an account in a few clicks or log in to continue.
CT GunZ Server

CT GunZ EN-Forums
 
HomeHome  PortalPortal  Latest imagesLatest images  SearchSearch  RegisterRegister  Ger Forums  GunZ Register  Server  VOTE please  Be Donator  Download CTGunZ  Log inLog in  

 

 Coding TuT 7

Go down 
3 posters
AuthorMessage
bossxd
BANNED
BANNED
bossxd


Male

Number of posts : 21
Location : usa
Points : 64
Character : Noname
GunZ Exp (nub/pro) :
Coding TuT 7 Left_bar_bleue0 / 1000 / 100Coding TuT 7 Right_bar_bleue

Registration date : 2009-04-12
Reputation : 0


Coding TuT 7 Empty
PostSubject: Coding TuT 7   Coding TuT 7 EmptyTue Apr 14, 2009 10:42 am

I/O

Files and more using printf, fscanf and fprintf

printf("%[flags][width].[precision][size][type]", variable);
printf format specifiers
Flags Width Precision Size Type example
-+0# Leading zeroes and justification
- Left align the result within the max width.
+ Sign the output, add + for positive, - for negative.
0 Pad floating point numbers with zeroes to max width.
# Type o, x or X: prefix with '0','0x' or 'X'.
Type e,E or F - force output to contain the decimal point.
Type g or G - force decimal point and do not remove trailing zeroes.
2 minimum characters to fill
.3 max characters to fill
lh modify size of the type
l Type d,i,o,x or X - long int
h Type d,i,o,x or X - short int
l Type u - long unsigned int
h Type u - short unsigned int
cCdiouxXeEfgGpsS data type
c single character
C single multi-byte character
d signed decimal integer
i equivalent to d
o unsigned octal integer
u unsigned decimal integer
x lower case unsigned hexadecimal integer
X upper case unsigned hexadecimal integer
e signed floating point exponential
E as e with upper case E in exponent
f signed floating point double
g e or f, whichever is most compact
G E of F, whichever is most compact
p hexadecimal segmented memory address
s null terminated string
S as S with multi-byte characters.
- 2 .3 l d 12.532
Simple file I/O using the standard library
Formatting ASCII output to the filesystem is simply an extension of output to the screen. Binary file interaction is more complex, but reading and writing simple text files is a matter of opening a file and printing the formatted output.

#include

int main() {
char quote[] = "GNU";
FILE* textfile = NULL;
// use fopen() to create an empty file
textfile = fopen("linux.txt","w");
if(!textfile) { return -1; }
fprintf(textfile, "%s is not Unix",quote);
fclose(textfile);
return 0;
}
char quote[]; creates an array of characters that can be used as a string. quote[0] = "G"; - each character in the array/string can be accessed individually in the normal manner of an array.
textfile is the first encounter with pointers. Be very careful with pointers - pointers are one of the easiest ways to create security vulnerabilities in your code! This line defines textfile as a pointer of type FILE - textfile points to a space in memory that is large enough to accomodate this special data type. textfile is initially defined as NULL (a special version of zero), simply to allow the compiler to allocate enough memory for the FILE type that holds details of how to access the file.
Now that textfile is pointing to sufficient memory, the linux.txt file can be opened. If it does not already exist, the "w" switch will create it. If it does exist, "w" will erase all content upon opening.
Another first - very basic error checking. If the file could not be opened for any reason, the FILE data type will be empty. This will mean that textfile points to the same blank memory that it was assigned in the second line. The ! means if_not_exist, so the if conditional is true only when textfile points to an empty FILE structure in memory. If true, execution moves to the right side of the statement - return -1; which will cause the program to exit with an error code of -1.
Always close your files - it may be true that compilers will attempt to close the files on your behalf but you cannot rely on the data being intact next time you want the file. Close the file as soon as you have finished with file operations on that specific file.
The final act is to return a successful result. Note that main() was defined to return an int value. This allows the program to return a failure result of -1 but it means that you must return a value on success as well.
Reading a file
Using fread() in place of fwrite() and a change from "w" mode to "r" for read, the standard library provides access to existing files. It is inefficient to read the file only one byte at a time, so this example uses a block (blocked I/O) and stops when the file no longer contains sufficient data to fill the block. It is vital that the buffer is created large enough for this block.

#include
#define BUFFER_LENGTH 1024

int main() {
char contents[BUFFERLENGTH + 1];
int readcount;
FILE* inputfile = NULL;
inputfile = fopen("linux.txt","r");
if(!inputfile) return -1;
do {
readcount = fread(contents, 1, BUFFERLENGTH, inputfile);
contents[readcount]='\0';
printf("%s", contents);
} while (readcount == BUFFERLENGTH);
fclose(inputfile);
return 0;
}
As well as including headers, the # syntax (preprocessor commands) also allows the definition of static values. This is very common in header files when used to hold critical details like the ID numbers of error messages, hardware identifiers and other fixed values. The text string (or macro), by convention, is always upper case and the text will be replaced by the value in the first stage of compiling the program. Notice also that C is capable of arithmetic within assignments - contents if defined as of size 1024 + 1, once the macro is expanded.
Note: readcount is not assigned a value when created. By using do{}, readcount is always assigned a value before reaching the conditional while - otherwise the result would be unpredictable.
fread() starts at byte 1 and puts the data into the contents[] buffer returning the number of bytes read successfully into readcount. When reading blocked I/O, the data in the buffer is split by our program, independently of the data being read. When you copy a set number of letters from a book, you cannot expect to read the complete sentence, or even complete words. As a result, the contents[] buffer must be terminated properly by adding the special null value '\0' to the end. Strings and arrays start at zero, so if a string contains 6 letters, the last letter is in the fifth position string[5]: 0,1,2,3,4,5 is 6 digits long. The end of the data from the file is in the last position, if the buffer was full this would be: contents[1023]. With readcount starting at 1, readcount therefore contains 1024. contents[], defined as length 1025, has one remaining position: contents[1024], ready for the terminating null. By using the value from readcount instead of a fixed value, we can be sure to only read valid data from the file, not reading duff memory space from beyond the real data in contents[] when the buffer is not full.
Back to top Go down
Snake
Retired Staff
Snake


Male

Number of posts : 3572
Age : 31
Location : UK
Points : 3473
Character : Coding TuT 7 Snake11copy-1
GunZ Exp (nub/pro) :
Coding TuT 7 Left_bar_bleue99 / 10099 / 100Coding TuT 7 Right_bar_bleue

Registration date : 2009-02-02
Reputation : 27


Coding TuT 7 Empty
PostSubject: Re: Coding TuT 7   Coding TuT 7 EmptyTue Apr 14, 2009 2:49 pm

You really made all these TUT's or all copyed?
Back to top Go down
Tittenfee
Retired Staff
Tittenfee


Warnings : 2.99
Male

Number of posts : 2522
Age : 30
Location : Austria, Vienna
Points : 2583
Character : Tittenfee, _NPC_
GunZ Exp (nub/pro) :
Coding TuT 7 Left_bar_bleue69 / 10069 / 100Coding TuT 7 Right_bar_bleue

Registration date : 2009-01-22
Reputation : 22


Coding TuT 7 Empty
PostSubject: Re: Coding TuT 7   Coding TuT 7 EmptyWed Apr 15, 2009 9:46 am

i think its copied. why shud he post his own tuts on a pserver lmao. i think thats a waste of time.
Back to top Go down
Snake
Retired Staff
Snake


Male

Number of posts : 3572
Age : 31
Location : UK
Points : 3473
Character : Coding TuT 7 Snake11copy-1
GunZ Exp (nub/pro) :
Coding TuT 7 Left_bar_bleue99 / 10099 / 100Coding TuT 7 Right_bar_bleue

Registration date : 2009-02-02
Reputation : 27


Coding TuT 7 Empty
PostSubject: Re: Coding TuT 7   Coding TuT 7 EmptyWed Apr 15, 2009 10:19 am

Yeah i now know he copied them all xD
Back to top Go down
Sponsored content





Coding TuT 7 Empty
PostSubject: Re: Coding TuT 7   Coding TuT 7 Empty

Back to top Go down
 
Coding TuT 7
Back to top 
Page 1 of 1
 Similar topics
-
» Coding TuT 11
» Coding TuT 12
» Coding tut
» Coding TuT 2
» Coding TuT 3

Permissions in this forum:You cannot reply to topics in this forum
CT GunZ Server :: Basket-
Jump to: