![]() |
|
#11
|
||||
|
||||
|
fprintf(FILE* fptr, const char* format, arg1, . . .
fprintf() is identical to printf() except that it writes to the stream instead of the screen. It returns the number of bytes output. In the event of an error, it returns EOF. This function can be used to create text reports. It will be shown later how to easily create very sophisticated reports. Example: CPP / C++ / C Code:
|
||||
|
#12
|
||||
|
||||
|
size_t fread(void* ptr, size_t size. size_t n, FILE* fptr);
fread() is designed to read binary data into a variable or structure. This function reads n items of data, each of length size bytes, from the given input stream into a block pointed to by ptr. The total number of bytes read is n multiplied by size. On success it returns the number of itesm (not bytes) actually read. It returns a short count on end-of-file or error. Example: CPP / C++ / C Code:
Last edited by dsmith : 03-Mar-2004 at 08:13.
|
|
#13
|
||||
|
||||
|
size_t fwrite(void* ptr, size_t size, size_t n, FILE* fptr);
fwrite() was designed for writing binary data into a variable or a structure. This function writes n items of data, each of length size bytes, to the given output stream from a block pointed to by ptr. The total number of bytes written is n multiplied by size. On success, it returns the number of items (not bytes) actually written. It returns a short count on end-of-file or error. Example: CPP / C++ / C Code:
Last edited by dsmith : 03-Mar-2004 at 08:13.
|
|
#14
|
||||
|
||||
|
long ftell(FILE* fptr);
This function returns the current file pointer for a stream given by fptr. The offset is measured in bytes from the beginning of the file. It returns the current file pointer position on success. It returns -1L on error. Example: CPP / C++ / C Code:
|
|
#15
|
||||
|
||||
|
int fseek(FILE* fptr, long offset, int whence);
This function sets the file pointer associated with fptr to a new position that is offset bytes from the current file position given by whence. The values of can be:
On success, fseek() returns 0. On failure, it returns a nonzero value. Example: CPP / C++ / C Code:
Last edited by dsmith : 03-Mar-2004 at 08:14.
|
|
#16
|
||||
|
||||
|
void rewind(FILE* fptr);
rewind() repositions a file pointer to the beginning of a stream. rewind() is equivalent to fseek(fptr, 0L, SEEK_SET), except that rewind() clears the end-of-file and error indicators, while fseek() clears the end-of-file indicator only. After rewind(), the next operation on an update file can be either input or output. Example: CPP / C++ / C Code:
|
|
#17
|
||||
|
||||
|
void remove(const char* filename);
remove() deletes the file specified by filename. If your file is open, be sure to close it before removing it. The filename string can include a full path. On success, remove() returns 0. On error, it returns -1. Example: CPP / C++ / C Code:
|
|
#18
|
||||
|
||||
|
Standard File Pointers
There are several predefined pointers in MS-DOS:
Edit - Added by dsmith For a further discussion of I/O pipes and redirection please refer to this thread. End Edit Now let's look at examples using standard I/O. Last edited by dsmith : 03-Mar-2004 at 08:45.
Reason: Added link to Pipe & Redirection thread
|
|
#19
|
||||
|
||||
|
Standard I/O - Example #1
This example reads in lines of text from a file and produces a formatted report. CPP / C++ / C Code:
Input File Format Code:
This example demonstrates the power of fscanf() and fprintf(). The input file is opened in read only mode in text format and verified that it opened correctly. The report file is opened in write only mode in text format and verified that it opened correctly. Notice that if the report file did not open correctly, the input file was closed before exiting the program. The input file was scanned for the first line of input. If there were five input values, the while loop was entered and the first formatted line of output was sent to the report file. The next input line was read. If there were five input values, the while loop continued with the next line of output being sent to the report file. This continued until all lines of text are read from the input file and sent to the report file. When the while loop terminates, both files are closed. The user can then print the report file. The file can be printed in a variety of ways. The user can use any print utility available that has the ability to print text files. For example: PRINT in DOS, or Explorer in Windows, or LPR in UNIX. IMPORTANT: This example followed the following algorithm Code:
This is a very important algorithm. Many beginning programmers do not follow this algorithm and find that they fail to process the last line of data or process the last line of data twice. If you ever have this type of problem, check to see if your code follows this algorithm. |
|
#20
|
||||
|
||||
|
Standard I/O - Example #2
This example reads in structures from a binary file using the structure defined below. It produces a formatted report. The binary file of structures was created using example #3. CPP / C++ / C Code:
|
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Re: Formatting C / C++ code | WaltP | C Programming Language | 1 | 07-Jan-2008 00:59 |
| Re: Naming Conventions | WaltP | C Programming Language | 8 | 06-Jun-2004 23:22 |
| New wi-fi standard approved - here comes 24-54Mbps | jrobbio | Open Discussion Forum | 1 | 17-Jul-2003 01:29 |
| [Tutorial] XSL Basics Pt. I | pcxgamer | Web Design Forum | 15 | 22-Apr-2003 07:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The