• facebook
  • whatsapp
  • telegram

17. డేటాని ఫైల్ లోకి రాయడం ఎలా?

17.1 Introduction

ఇప్పటి వరకు మనం రాసిన అన్ని ప్రోగ్రాంలు కావల్సిన దానిని keyboard నుంచి తీసుకొని ఫలితాన్ని స్క్రీను మీద చూపించాయి. ఈ పాఠంలో ఒక file నుంచి డేటా రీడ్ చేసి, ఫలితాన్నిfile లోకి రాయడం ఎలాగో తెలుసుకుందాం. దీనికి మనం FILE * టైప్ variablesను ఉపయోగించాలి. వీటిని చేసేందుకు చాలా readymade functionలు ఉన్నాయి. అందులో కొన్నింటి గురించి తెలుసుకుందాం.

fopen
ఒక fileను ఓపెన్ చేయడానికి, fopen అనే function ను ఉపయోగిస్తాం. దానికి file పేరు (దాని PATHతో కలసి), ఎందుకు ఆ fileను ఓపెన్ చేద్దామనుకుంటున్నాం అనేవి ఈ కింది విధంగా రెండు string టైప్ argumentలను పంపుతూ చెప్పాలి. రెండో దానిని mode string అని అంటారు. ఈ function fail అయితే 0 రిటర్న్ చేస్తుంది.

FILE* fopen( char *path, char *mode)
ఉదాహరణకు, “results” అనే fileను డేటా రాయడం కోసం ఓపెన్ చేయాలంటే, ఈ కింద ఇచ్చిన విధంగా ఒక FILE* టైప్ variableను declare చేసి, fopen functionను call చేయాలి.
FILE *p2;
p2=fopen(“results”,”w”);

ఈ కింద ఇచ్చిన టేబుల్, ఏ mode file ఓపెన్ చేయాలంటే, ఏ mode string ఉపయోగించాలో తెలుపుతుంది.

The fprintf() & fscanf() functions
వీటిని ఉపయోగించి ఫైల్ నుంచి డేటా రీడ్ లేదా ప్రింట్ చేయవచ్చు. printf, scanf లతో పోలిస్తే, వీటికి first argument గా మనం రీడ్/రాయాలని అనుకుంటున్న ఫైల్ కి సంబంధించినFILE pointer ఇవ్వాలి.
వీటి declarations ఈ కింద విధంగా ఉంటాయి.
fprintf(fp,”format string”, list of arguments to be printed);
fscanf(fp,”format string”, addresses of arguments into which data has to be read);

Other I/O Functions
int fgetc(FILE *ip);
ఈ functionకు first argument గా రీడ్ చేయాలని అనుకుంటున్న ఫైల్ కి సంబంధించినFILE pointer ఇస్తే అది ఒక characterను దాంట్లోనుంచి రీడ్ చేసి రిటర్న్ చేస్తుంది.

int fputc(char V, FILE *ip);
ఈ functionకు arguments గా ఒక character, మనం రాయాలని అనుకుంటున్న ఫైల్ కి సంబంధించినFILE pointer ఇస్తే అది ఆ characterను ఫైల్ లో రాస్తుంది.

char *fgets(char *s, int n, FILE *ip);
ఈ functionకు argumenst గా ఒక string variable address (name), ఒక integer, రీడ్ చేయాలనుకుంటున్న ఫైల్ కి సంబంధించినFILE pointer ఇస్తే అది ఒక stringను దాంట్లోనుంచి రీడ్ చేసి ఇచ్చిన string లో పెడుతుంది.

fputs(char *s, FILE *ip);
ఈ functionకు arguments గా ఒక string, మనం రాయాలని అనుకుంటున్న ఫైల్ కి సంబంధించినFILE pointer ఇస్తే అది ఆ stringను ఫైల్ లో రాస్తుంది.

The fwrite(), fread() functions
ఇవి binary mode లో file మీద I/O చేయడానికి వాడతారు. ఇంతకు ముందు చర్చించిన functions I/O చేస్తున్నప్పుడు ASCII formలో చేస్తాయి. అదే ఈ functions అయితే, binary pattern ను ditto లాగా I/0 లో తీసుకుంటాయి.
int fread( void *ptr, size_t sz, size_t n, FILE *ftr);
int fwrite( void *ptr, size_t sz, size_t n, FILE *ftr);

ఈ fread function sz bytes ఉండే n elementsను, ఫైల్ pointer ftr point చేస్తున్న ఫైల్ లోనుంచి ptr point చేస్తున్న memoryలో పెడుతుంది.
ఈ fwrite function sz bytes ఉండే n elementsను, ఫైల్ pointer ftr point చేస్తున్న ఫైల్ లోపలికి ptr point చేస్తున్న memoryలో నుంచి కాపీ చేస్తుంది.

A Note on Formatted/Un-formatted I/O
మనం fprintf(), fputs(), లతో రాస్తే వచ్చే file ను text file టైప్, దానిని notepad, wordpad వంటి editors సహాయంతో చూడొచ్చు, ఎడిట్ చేయొచ్చు. వీటిద్వారా ఫైల్ లోకి ఏం రాసినా వాటిని ASCII లోకి మార్చి రాస్తాయి. ఉదాహరణకు, ఒక integer variable, x=32113, అనేది memoryలో (RAMలో) 2 bytes లో ఉంటుంది. దానిని fprintf() ద్వారా ఫైల్ లోపలికి రాస్తే 5 bytes తీసుకుంటుంది. అదే fwrite ద్వారా రాస్తే ఫైల్ లోకూడా 2 bytes మాత్రమే తీసుకుంటుంది. fwrite ద్వారా రాసిన ఫైల్ లను un-formatted or binary ఫైల్ లు అని అంటారు. ఇవి తక్కువ స్పేస్ తీసుకుంటాయి కాబట్టి I/O వేగవంతమవుతుంది.

Closing a file
int fclose(FILE *stream)
ఈ functionకు argument గా మనం క్లోజ్ చేయాలని అనుకుంటున్న ఫైల్ కి సంబంధించినFILE pointer ఇస్తే అది ఆ ఫైల్ ను క్లోజ్ చేస్తుంది.

int fflush(FILE *stream)
ఈ functionకు argument గా మనం flush చేయాలని అనుకుంటున్న ఫైల్ కి సంబంధించినFILE pointer ఇస్తే అది ఆ ఫైల్ వాడుతున్న అన్ని buffersను flush చేస్తుంది.

Stream Posistioning
void rewind( FILE *stream);
ఈ functionకు argument లాగా మనం రీడ్/రాయాలని అనుకుంటున్న ఫైల్ కి సంబంధించినFILE pointer ఇస్తే అది దానిలో ఉండే file కి సంబంధించినinvisible pointerను file beginingకు తెస్తుంది.

fseek(file pointer,offset, position);
దీని ద్వారా ఫైల్ లో invisible pointerను ఎక్కడ కావాలంటే అక్కడ పెట్టవచ్చు. దీనికి మొదటి argument మనం సీక్ చేయాలని అనుకుంటున్న ఫైల్ కి సంబంధించినFILE pointer, రెండోది ఒక integer, మూడోది ఎక్కడ నుంచి పొజిషన్ ను చెపుతున్నాం. దీని గురించి కింద ఏ విలువ ఇస్తే ఏం వస్తుందో ఇచ్చాం.
Value Meaning
0 Beginning of the file
1 Current position
2 End of the file.

int ftell(FILE*);
ఈ functionకు first argument గా రీడ్ చేయాలని అనుకుంటున్న ఫైల్ కి సంబంధించినFILE pointer ఇస్తే అది ఒక long నంబరును రిటర్న్ చేస్తుంది.ఇది ఆ ఫైల్ లో రీడింగ్ pointer, beginning నుంచి ఎన్ని bytes దూరంలో ఉందో ఇస్తుంది.

int feof(FILE *);
ఈ functionకు first argument గా ఓపెన్ చేసిఉన్న ఫైల్ కి సంబంధించినFILE pointer ఇస్తే అది ఫైల్ pointer చివరలో ఉంటే 1 లేకపోతే 0 రిటర్న్ చేస్తుంది.

int ferror(FILE *);
ఈ functionకు first argument గా రీడ్/రాసిన ఫైల్ కి సంబంధించినFILE pointer ఇస్తే అది 1 లేక 0 రిటర్న్ చేస్తుంది.ఆ ఫైల్ మీద చేసిన ఏదైనా ఆపరేషను error అయి ఉంటే 1 లేకపొతే 0 రిటర్న్ అవుతుంది.

Example 1: ఈ కింది ప్రోగ్రాం fopen, fprintf, scanf, fclose, fflush functions ను ఎలా ఉపయోగించాలో చూపిస్తుంది. ఇది మూడు integersను keyboard నుంచి రీడ్ చేసి TEST అనే fileను open చేసి దానిలోనికి రాస్తుంది. ఆ ఫైల్ ను క్లోజ్ చేసి మళ్లీ ఓపెన్ చేసి దానిలో ఉండే మూడు విలువలను రీడ్ చేసి (వేరే variables లోకి ) స్క్రీన్ మీద చూపిస్తుంది..
#include
int main()
{
     int x,y,z, p,q,r;
     FILE *IPP;
     printf("Enter Three Integers\n");
     scanf("%d%d%d", &x, &y, &z);
     IPP=fopen("TEST", "w");
     fprintf(IPP,"%d\n%d\n%d\n", x,y,z);
     fflush(IPP); /* Not always needed*/
     fclose(IPP);
     IPP=fopen("TEST", "r"); /*re-opened the file but in reading mode */
     fscanf(IPP,"%d%d%d", &p, &q, &r);
     printf("Numbers read from the file are=%d %d %d\n", p,q,r);
     return 0;
}

Example 2: ఈ ప్రోగ్రాం ఒక ఫైల్ ను ఓపెన్ చేసి దాని contentను line number తో కలిపి ప్రింట్ చేస్తుంది. ఇక్కడ fgets ను ఉపయోగించి ఫైల్ నుంచి ఒక్కో లైన్ రీడ్ చేసి , ఒక కౌంటరు విలువను ప్రింట్ చేస్తున్నాం. లైను రీడ్ చేసినప్పుడల్లా కౌంటరు విలువను ఒకటి పెంచుతున్నాం.

#include
#define LINE_LENGTH 512
int main(int argc, char *argv[])
{
     FILE* fpp;
     char line[LINE_LENGTH];
     int count=0;
     fpp=fopen(argv[1],"r");
     /* Count up the lines here. */
     while ( fgets(line, LINE_LENGTH, fpp) != NULL)
     {
     printf(“%3d %s\n”, count++. Line);
     }
     fclose(fpp);
     return 0;
}

Example 3: ఈ ప్రోగ్రాం file nameని command line ద్వారా తీసుకుని దాని size ను ప్రింట్ చేస్తుంది. ఫైల్ ని ఓపెన్ చేసిన తర్వాత fseek() వాడి file pointerను end of the file, అంటే చివర లో పెట్టి అప్పుడు ftell() call చేస్తే file size వస్తుంది.

#include
int main(int argc, char*argv[])
{
     FILE *ipp;
     if(argc!=2)
     {
     printf("Only one argument is needed\n");
     exit(-1);
     }
     ipp=fopen(argv[1], "r");
     if(ipp==0) {
     printf("Error in opening file");
     exit(-1);
     }
     fseek(ipp,0,2); /* positioning the reading pointer at EOF*/
     printf("File Size=%ld\n", ftell(ipp)); /* Calling ftell() function*/
     fclose(ipp);
     return 0;
}

Example 4: ఈ ప్రోగ్రాం operating system లో ఉండే COPY command లా పనిచేయడనికి ఉద్దేశించినది. అంటే, ఇది source file name ను first command line argument గా , duplicate file name ను second command line argumentగా తీసుకొని source file లో ఉండేదానిని destination file లోపలికి కాపీ చేస్తుంది. ఇక్కడ fgetc ని ఉపయోగించి character తర్వాత characterను రీడ్ చేసి fputc తో EOF వచ్చేంత వరకు చేస్తాం.
#include
int main(int argc, char*argv[])
{
     FILE *ipp, *opp;
     if(argc!=3)
     {
     printf("Two arguments are needed\n");
     exit(-1);
     }

     ipp=fopen(argv[1], "r");
     if(ipp==0)
     {
     printf("Error in opening file");
     exit(-1);
     }
     opp=fopen(argv[2],"w");
     while(!feof(ipp))
     fputc(fgetc(ipp),opp);
     fclose(ipp);
     fclose(opp);
     return (0);
}

Example 5: ఈ exampleను Formatted I/O,binary I/O ల మధ్య ఉండే తేడాను చూపేందుకు తీసుకున్నాం. ఇక్కడ, ఒక integer array ఉన్న విలువలను fprintf() ద్వారా ఒక ఫైల్ లోపలికి, fwrite() ద్వారా ఇంకొక ఫైల్ లోపలికి రాశాం. ప్రోగ్రామ్ పూర్తయిన తర్వాత రెండింటిని notepad వంటి editor ద్వారా ఓపెన్ చేస్తే fwrite() ద్వారా రాసిన ఫైల్ లో ఉండేది చూడలేం. ఎందుకంటే, అది binary ఫైల్. అంతేకాకుండా, ఈ binary ఫైల్ ను ఓపెన్ చేసి అందులో ఉన్న విలువలను fread ద్వారా రీడ్ చేసి ప్రింట్ చేస్తుంది.

#include
int main()
{
     int x[5]={31111,31100,31001,31011,31000}, N,i;
     FILE *IPP, *OPP;
     printf("Writing into file using Formatted manner\n");
     IPP=fopen("TEST1", "w");
     for(i=0;i<5;i++)fprintf(IPP,"%d\n", x[i]);
     fflush(IPP);
     fclose(IPP);
     printf("Writing into file using Un-Formatted(Binary) manner\n");
     OPP=fopen("TEST2", "w+");
     fwrite(x, sizeof(int), 5, OPP);
     fflush(OPP);
     rewind(OPP);
     printf("Reading Numbers From Binary File\n");
     for(i=0;i<5;i++)
      {
     fread(&N, sizeof(int),1,OPP);
     printf("%d\n", N);
     }
     return 0;
}

Example 6: ఈ ప్రోగ్రాం ఒక input file (“student.dat”) ఓపెన్ చేయాలి. అందులో ఎంతమంది విద్యార్థులు ఉన్నారనేది మొదటి లైనులో ఉంటుంది. తర్వాత, ఒక్కో లైనులో ఒక్కో విద్యార్థి ఐదు test మార్కులు ఈ కింది విధంగా ఉంటాయి.
5
90    20    20    20    90
90    99    90    99    89
43    33    33    22    29
44    40    40    40    40
90    90    90    90    90

ఈ ప్రోగ్రాం మార్కులు, గ్రేడ్స్ ఈ కింది విధంగా ఇవ్వాలి. ఇంతకు ముందు చాప్టరులలో గ్రేడింగ్ రూల్స్, పాయింట్స్ గురించి చర్చించాం. ఇందులో కూడా వాటినే ఉపయోగించాం.

90 A    20 D    20 D    20 D    90 A    6.400000
90 A    99 A    90 A    99 A    89 A    10.000000
43 C    33 D    33 D    22 D    29 D    4.400000
44 C    40 C    40 C    40 C    40 C    6.000000
90 A    90 A    90 A    90 A    90 A    10.000000

#include
char Grade(int n)
     {
     return('A' + (4 - n/20) +( n ==100));
     }
     int Points(char g)
     {
     return(2 * ( 70-g) );
     }
int main()
{
     int a[10][5], n, i, j;
     char v;
     float s;
     FILE *IPP;
     IPP=fopen("student.dat", "r");
     fscanf(IPP,"%d", &n);
     printf("Reading Students Marks in five tests\n");
     for(i = 0; i < n; i++)
     for(j = 0; j < 5; j++)
     fscanf(IPP,"%d", &a[i][j]);

     for(i = 0; i < n; i++){
     s = 0;
     for(j = 0; j < 5; j++){
     v = Grade(a[i][j]);
     /* Here, a[i][j] is ith student jth test marks. Which is integer*/
     s += Points(v);
     printf("%d %c\t", a[i][j], v);
     }
     printf("%f\n", s/5);
     }
     fclose(IPP);
     return (0);
}

పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఈ విధంగా ఉంటుంది.

Example 7: ఈ ప్రోగ్రాం కొన్ని STUD అనే structure టైప్ variables informationను binary mode లోఒక ఫైల్ లోకి రాసి మళ్లీ రీడ్ చేసి స్క్రీన్ మీద ప్రింట్ చేస్తుంది. అంతేకాకుండా, ఇలాంటి ఫైల్ మీద random accessing ఎలా చేయాలో కూడా చూపిస్తుంది.

#include
struct DATE
     {
     int d;
     int m;
     int y;
     };
     struct STUD
     {
     char name[20];
     int RNo;
     struct DATE DOB;
     };

     void READSTUD(struct STUD *A)
     {
     scanf("%s%d%d%d%d", A->name, &A->RNo, &A->DOB.d, &A->DOB.m, &A->DOB.y);
     }

     void PRINTSTUD(struct STUD *A)
     {
     printf("%s\t%d\t%d\t%d\t%d\n", A->name, A->RNo, A->DOB.d, A->DOB.m, A->DOB.y);
     }
int main()
     {
     struct STUD STD[5],X;
     int i,n=5,p;
     FILE *OPP;
     for(i=0;i      READSTUD(&STD[i]);
     }
     OPP=fopen("stud.dat", "w");
     fwrite(STD, sizeof(struct STUD), 5, OPP);
     fclose(OPP);

     OPP=fopen("stud.dat", "r");
     printf("Students Details Read from the file\n");
     for(i=0;i      fread(&X, sizeof(struct STUD),1,OPP);
     PRINTSTUD(&X);
     }

     printf("Now we are doing random accessing on the file\n");
     printf("Enter Which Student Details We want\n");
     scanf("%d", &p);

     rewind(OPP); /* rewinding the file*/
     fseek(OPP, sizeof(struct STUD)*(p-1), 0);
     fread(&X, sizeof(struct STUD),1,OPP);
     PRINTSTUD(&X);
     fclose(OPP);
     return 0;
}


 

 

 

NB Venkateswarlu
M.Tech(IIT-Kanpur)
Ph.D (BITS),PDF(UK),
Sr.Prof., CSE, AITAM, Tekkali

Posted Date : 04-02-2021 .