slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Text files PowerPoint Presentation
Download Presentation
Text files

Loading in 2 Seconds...

play fullscreen
1 / 5

Text files - PowerPoint PPT Presentation


  • 104 Views
  • Uploaded on

Text files. Text file contains ASCII-characters New line and end-of-file are special characters in a text file Ex. 1 This is a text<newline>It has two lines<newline><end-of-file> Examples of predifined text files stdin, stdout, stderr (these are file pointers)

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Text files' - anneke


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

Text files

  • Text file contains ASCII-characters
  • New line and end-of-file are special characters in a text file

Ex. 1

  • This is a text<newline>It has two lines<newline><end-of-file>
  • Examples of predifined text files

stdin, stdout, stderr (these are file pointers)

  • stderr always associated with the screen
  • stdin, stdout might be re-directed to other text files

TDBA66, vt-04, Lecture Ch9

slide2

EOF

EOF is defined in <stdio.h> and is returned from scanf() or fscanf() if end-of-file of the read file is detected

Possible to check if EOF is read by

e.g.

for (status=scanf(”%d”, &num); status != EOF; status=scanf(”%d”, &num))

process(num);

e.g.

while (fscanf(infpt,”%d”, &num) != EOF)

process(num);

TDBA66, vt-04, Lecture Ch9

slide3

Declare file pointers

  • Open a file for reading or writing
  • -check if opening was ok
  • Use it/them
  • Close them before ending program
  • Ex. 1
  • FILE *inf, *outf;
  • if ((inf=fopen(”filname1.txt”,”r”)) == NULL){
  • fprintf(stderr,”Error when opening filname1.txt”);
  • exit(-1);
  • }
  • if ((outf=fopen(”filname2.txt”,”w”))== NULL){
  • fprintf(stderr,”Error when opening filname2.txt”);
  • exit(-2);
  • }

Using text files

TDBA66, vt-04, Lecture Ch9

slide4

/* Makes a backup file. Repeatedly prompts for the name of a file to

* back up until a name is provided that corresponds to an available

* file. Then it prompts for the name of the backup file and creates

* the file copy.

*/

#include <stdio.h>

#define STRSIZ 80

intmain(void){

char in_name[STRSIZ], /* strings giving names */

out_name[STRSIZ]; /* of input and backup files */

FILE *inp, /* file pointers for input and */

*outp; /* backup files */

char ch; /* one character of input file */

int status; /* status of input operation */

/* Get the name of the file to back up and open the file for input*/

printf("Enter name of file you want to back up> ");

for (scanf("%s", in_name);

(inp = fopen(in_name, "r")) == NULL;

scanf("%s", in_name)) {

printf("Cannot open %s for input\n", in_name);

printf("Re-enter file name> ");

}

TDBA66, vt-04, Lecture Ch9

slide5

/*Get name to use for backup file and open file for output */

printf("Enter name for backup copy> ");

for (scanf("%s", out_name);

(outp = fopen(out_name, "w")) == NULL;

scanf("%s", out_name)) {

printf("Cannot open %s for output\n", out_name);

printf("Re-enter file name> ");

}

/* Make backup copy one character at a time */

for (status = fscanf(inp, "%c", &ch);

status != EOF;

status = fscanf(inp, "%c", &ch))

fprintf(outp, "%c", ch);

/* Close files and notify user of backup completion */

fclose(inp);

fclose(outp);

printf("Copied %s to %s.\n", in_name, out_name);

return(0);

}

TDBA66, vt-04, Lecture Ch9