C - File I/O - Text                                                                               Home : www.sharetechnote.com

 

 

 

 

File I/O - Text

 

 

 

 

Reading a whole text file - fgetc()  =============================================

 

File Name :  FileText_Ex01.txt

This

is

a

Sample

Text File

 

 

This is a Sample Text File

 

 

Example 01 >

 

#include <stdio.h>

 

main() {

 

   FILE *fp;

   char c;

 

   fp = fopen("FileText_Ex01.txt", "r");

 

   while(!feof(fp)) {

    

     c = fgetc(fp);

     printf("%c", c);

    

   }

       

 

   fclose(fp);

 

}

 

Result :

This

is

a

Sample

Text File

 

 

This is a Sample Text File

 

 

 

Reading a whole text file - fgets()  =============================================

 

File Name :  FileText_Ex01.txt

This

is

a

Sample

Text File

 

 

This is a Sample Text File

 

 

Example 01 >

 

#include <stdio.h>

 

main() {

 

   FILE *fp;

   char buff[255];

 

   fp = fopen("FileText_Ex01.txt", "r");

 

   while(!feof(fp)) {

    

     fgets(buff, 255, (FILE*)fp) ;

     printf("%s", buff );

 

   }

       

 

   fclose(fp);

 

}

 

Result :

This

is

a

Sample

Text File

 

 

This is a Sample Text File

This is a Sample Text File

 

 

 

Example 02 >

 

#include <stdio.h>

 

main() {

 

   FILE *fp;

   char buff[255];

 

   fp = fopen("FileText_Ex01.txt", "r");

 

   while(!feof(fp)) {

    

     if(fgets(buff, 255, (FILE*)fp) != NULL) {

        printf("%s", buff );

        }

 

   }

       

 

   fclose(fp);

 

}

 

Result :

This

is

a

Sample

Text File

 

 

This is a Sample Text File

 

 

 

Reading a whole text file - fscanf()  =============================================

 

File Name :  FileText_Ex01.txt

This

is

a

Sample

Text File

 

 

This is a Sample Text File

 

 

 

Example 01 >

 

#include <stdio.h>

 

main() {

 

   FILE *fp;

   char buff[255];

 

   fp = fopen("FileText_Ex01.txt", "r");

 

   while(!feof(fp)) {

    

     fscanf(fp,"%s",buff);

     printf("%s", buff);

    

   }

       

 

   fclose(fp);

 

}

 

Result :

ThisisaSampleTextFileThisisaSampleTextFileFile

 

 

Example 02 >

 

#include <stdio.h>

 

main() {

 

   FILE *fp;

   char buff[255];

 

   fp = fopen("FileText_Ex01.txt", "r");

 

   while(!feof(fp)) {

    

     fscanf(fp,"%s",buff);

     printf("%s\n", buff);

    

   }

       

 

   fclose(fp);

 

}

 

Result :

This

is

a

Sample

Text

File

This

is

a

Sample

Text

File

File

 

 

File Name :  FileText_Ex02.txt

X1 0 Y1 0

X2 0.1 Y2 0.099833417

X3 0.2 Y3 0.198669331

X4 0.3 Y4 0.295520207

X5 0.4 Y5 0.389418342

X6 0.5 Y6 0.479425539

X7 0.6 Y7 0.564642473

X8 0.7 Y8 0.644217687

X9 0.8 Y9 0.717356091

X10 0.9 Y10 0.78332691

X11 1 Y11 0.841470985

 

#include <stdio.h>

 

main() {

 

   FILE *fp;

   char label1[20],label2[20];

   float x,y;

 

   fp = fopen("FileText_Ex02.txt", "r");

 

   while(!feof(fp)) {

    

     if(fscanf(fp,"%s %f %s %f",label1,&x,label2,&y) != 0) {

        printf("%s = %f, %s = %f\n", label1,x,label2,y);

        }

    

   }

       

 

   fclose(fp);

 

}

 

Result :

X1 = 0.000000, Y1 = 0.000000

X2 = 0.100000, Y2 = 0.099833

X3 = 0.200000, Y3 = 0.198669

X4 = 0.300000, Y4 = 0.295520

X5 = 0.400000, Y5 = 0.389418

X6 = 0.500000, Y6 = 0.479426

X7 = 0.600000, Y7 = 0.564642

X8 = 0.700000, Y8 = 0.644218

X9 = 0.800000, Y9 = 0.717356

X10 = 0.900000, Y10 = 0.783327

X11 = 1.000000, Y11 = 0.841471

 

 

writing a text file - fpuc()  =============================================

 

Example 01 >

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

   FILE * fp;

 

   char *s = "This is a text output sample";

   char c;

 

   fp = fopen ("TestFile.txt", "w");

   

   do{

        c = *(s++);

        fputc(c,fp);

   }while(c != '\0');

   

   fclose(fp);

   

   return(0);

}

 

Result : TestFile.txt

This is a text output sample

 

 

Example 02 >

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

   FILE * fp;

 

   char *s = "This is a text output sample";

   char c;

 

   fp = fopen ("TestFile.txt", "w");

   

   do{

        c = *(s++);

        if(c == ' ') c = '\n';

        fputc(c,fp);

   }while(c != '\0');

   

   fclose(fp);

   

   return(0);

}

 

Result : TestFile.txt

This

is

a

text

output

sample

 

 

 

writing a text file - fprintf()  =============================================

 

Example 01 >

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

   FILE * fp;

 

   fp = fopen ("TestFile.txt", "w");

   fprintf(fp, "This\nis\na\ntest\nfile\n");

   fprintf(fp, "This is a test file\n");

   fprintf(fp, "X %f Y %f",0.1,0.12345);

   

   fclose(fp);

   

   return(0);

}

 

Result : TestFile.txt

This

is

a

test

file

This is a test file

X 0.100000 Y 0.123450