GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 5.00 average.
  #11  
Old 27-Feb-2004, 19:24
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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:
if (fprintf(fptr,"format . . .",arg1,. . .) == EOF)
  . . .
  #12  
Old 27-Feb-2004, 19:29
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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:
if (fread(ptr, size, n, fptr) != n)
  . . .
Last edited by dsmith : 03-Mar-2004 at 08:13.
  #13  
Old 27-Feb-2004, 19:44
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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:
if (fwrite(ptr, size, n, fptr) != n)
  . . .
Last edited by dsmith : 03-Mar-2004 at 08:13.
  #14  
Old 27-Feb-2004, 19:48
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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:
if ( (offset = ftell(fptr)) == -1L)
  . . .
  #15  
Old 27-Feb-2004, 20:00
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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:
  • SEEK_SET -- value = 0, Beginning of file
  • SEEK_CUR -- value = 1, Current file pointer position
  • SEEK_END -- value = 2, End of file

On success, fseek() returns 0. On failure, it returns a nonzero value.

Example:
CPP / C++ / C Code:
if (!fseek(fptr, 200L, SEEK_SET))
  . . .
if (!fseek(fptr, -100L, SEEK_CUR))
  . . .
Last edited by dsmith : 03-Mar-2004 at 08:14.
  #16  
Old 27-Feb-2004, 20:04
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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:
rewind(fptr);
  #17  
Old 27-Feb-2004, 20:07
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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:
remove("c:\\txtfiles\\names.txt");
  #18  
Old 27-Feb-2004, 20:12
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Standard File Pointers

There are several predefined pointers in MS-DOS:
  • stdin -- standard input (keyboard, unless redirected)
  • stdout -- standard output (screen, unless redirected)
  • stderr -- standard error (screen)
  • stdaux -- auxiliary device (often serial devices)
  • stdprn -- printer

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  
Old 27-Feb-2004, 21:15
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Standard I/O - Example #1

This example reads in lines of text from a file and produces a formatted report.

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#define NAMESIZE 10
#define SSNSIZE 11

int main()
{
  char  name[NAMESIZE+1],
        ssn[SSNSIZE+1];
  int   age,
        result,
        count = 0;
  float GPA,
        salary;
  FILE *infile,
       *report;
  if ((infile = fopen("c:\\data.txt","rt")) == NULL)
    {
      perror("Cannot open output file.");
      exit(0);
    }
  if ((report = fopen("c:\\report.txt","wt")) == NULL)
    {
      fclose(infile);
      perror("Cannot open input file.");
      exit(0);
    }
  result = fscanf(infile,"%s %s %d %f %f\n", &name, &ssn, &age, &GPA, &salary);

  while (result == 5)
    {
      fprintf(report,"%2d.  %*s %*s %3d %4.2f\n",++count,NAMESIZE,name,SSNSIZE,ssn,
              age,salary);
      result = fscanf(infile,"%s %s %d %f %f\n", &name, &ssn, &age, &GPA, &salary);
    }
  fclose(infile);
  fclose(report);
  return 0;
}

Input File Format
Code:
name SSN age gpa salary Aaron 123-45-6789 29 4.0 123456 Sandra 234-56-7890 34 3.8 78901 . . .

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:
read data from file while (read from file is successful) process the data read next data from file end while

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  
Old 27-Feb-2004, 21:47
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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:
#include <stdio.h>
#include <stdlib.h>

#define NAMESIZE 10
#define SSNSIZE 11

typedef struct
{
  char  name[NAMESIZE+1];
  char  ssn[SSNSIZE+1];
  int   age;
  float gpa;
  float salary;
}PERSON;

int main()
{
  int    result,
         count=0;
  PERSON student;
  FILE  *infile,
        *report;

  if ((infile = fopen("c:\\data.dat","rb")) == NULL)
    {
      perror("Couldn't open input file");
      exit(0);
    }
  if ((report = fopen("c:\\report.txt","wt")) == NULL)
    {
      perror("Couldn't open output file");
      fclose(infile);
      exit(0);
    }
  while (fread(&student, sizeof(PERSON),1,infile) == 1)
    {
      fprintf(report,"%2d.   %*s %*s %3d %4.2f",++count, NAMESIZE,student.name,
              SSNSIZE,student.ssn,student.salary);
    } // This set of braces is for clarity; they aren't really necessary.
  fclose(infile);
  fclose(report);
  return 0;
}
In this example we read one structure at a time into student. If the read was successful, we send a formatted line of output to the report file.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 17:12.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.