C and C++Post your C and C++ Related Tutorial Here
Notices
Welcome to the AioŽ forum.
You are currently viewing AIOFORUM as a guest which gives you VERY limited access to view most discussions, Applications, Latest Movies & Tutorials and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload and Download content and access many other special features. Registration is fast, simple and absolutely free so please, Join our community today!
If you have any problems with the registration process or your account login, please use Contact us.
Function atof is used to convert a string into a float. It stands for ASCII to float, and is pronounced A to F. The string must contain a floating-point number in character format (i.e. char string[] = "12.43"). This is what a call to atof should look like.
float_var = atof( string );
The string passed in is converted to a float, which is then returned by the function. A float variable is needed to store the return value of atof. If the string does not contain a float, or it has other garbage atof will return zero. To use atof you must include the stdlib.h header file.
Function atoi is used to convert a string that has numerical characters into an integer. In fact atoi stands for ASCII to Integer. It is pronounced A to I. The format is simple.
int_var = atoi( string );
As you can see, atoi takes a string as a parameter. It examines the string then returns an integer value. That value is the integer version of the string. You need an integer variable to catch the return value. If the string you pass does not contain an integer or has other weird stuff in it, atoi will return zero. To use atoi you must include the stdlib.h header file.
Function exit is used to bail out of a program at any time. This function can be put anywhere. If the program flow gets to this command, the program will halt then and there. Even if it is nested down a dozen layers the program will quit when it gets there. The format looks like this:
exit( n );
The single parameter is usually zero. It can be any integer though. This parameter is passed to the operating system when the program quits. Some operating systems may want integers in a set range. For example DOS likes to see numbers between 0 and 255. On a Sun, the values can be negative, but I'm not sure of the range. The operating system may or may not care about the value though. This function is usually used to screen user input. If they give you input that will mess up your program, you can use an exit function to bail out on them. Depending on your compiler you will either need to include stdio.h, or stdlib.h.
The fclose function is used to close files. Simply pass the file handle of the file you want to close, and it will be done. A call to fclose should look something like this:
fclose( FileHandle );
Of course, Filehandle is a file handle to a file opened by fopen. The operating system will close this file, and write any remaining bits, as well as update things like the time it was last accessed. Although it is rare, if you do not close a file with fclose problems may result. The stdio.h header file is required to use fclose.
The fopen function is used to open or create files on disk. If successful, it will return a file handle (also called a file pointer) to the program so the file can be accessed by other functions. Here is the format:
handle = fopen( FileName, mode );
The handle must be declared in advanced, just like any other variable. File handles (or pointers) are declared like this:
FILE *FileHandle;
The first parameter to fopen is the file name. This can either be in the form of a string constant (text enclosed in quotes), or a string variable. The second parameter is the access mode for the file. Usually one or two characters enclosed in double quotes represent the mode. Below is a table summarizing the file modes. MODE USED FOR FILE CREATED? EXISTING FILE? "a" Appending Yes Appended to "a+" Reading and appending Yes Appended to "r" Read only No If not found fopen returns NULL "r+" Reading and writing No If not found fopen returns NULL "w" Write only Yes Destroyed "w+" Reading and writing Yes Destroyed fopen file modes
The fprintf function is used to write to files. It works just like printf except it writes to files not the monitor, and it needs an extra parameter. Here is the format:
fprintf( FileHandle, "format string", variables );
The first parameter must be a file pointer returned form a call to fopen. Other than that it is just like printf. The return value is still the number of characters it wrote. For more information on the formatting stuff see the printf page. The stdio.h header file is required to use fprintf.