lect29 - PDS CS 11002 Computer Sc Engg IIT Kharagpur $ 1 File Access Lect 29 Goutam Biswas PDS CS 11002 $ Computer Sc Engg IIT Kharagpur 2 stdin stdout

lect29 - PDS CS 11002 Computer Sc Engg IIT Kharagpur $ 1...

This preview shows page 1 - 9 out of 30 pages.

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 & $ % File Access Lect 29 Goutam Biswas
Image of page 1
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2 & $ % stdin & stdout So far all our I/O operations are read from the standard input (keyboard) and write to the standard output (VDU) devices. These are defined and connected to the running program (process) by the OS. In Unix/Linux OS these devices are treated as two files stdin and stdout respectively. The printf(), scanf() functions and the getchar(), putchar() macros are used to access these files. Lect 29 Goutam Biswas
Image of page 2
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 3 & $ % File Access Functions But we like to open other files for read, write or append. Following C library functions are used for this purpose: fopen(), fclose(), fseek(), fprintf(), fscanf(), getc(), putc() etc. Lect 29 Goutam Biswas
Image of page 3
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 4 & $ % Example 1 #include <stdio.h> #include <string.h> #define MAXNO 100 #define ROLL 9 #define NAME 51 struct studData { // fileOpen1.c char rollNo[ROLL] ; char name[NAME] ; float cgpa ; }; #define NOTFOUND -1 Lect 29 Goutam Biswas
Image of page 4
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5 & $ % int binarySearch(struct studData data[], int lInd, int rInd, char rollNo[]) { while(lInd < rInd) { int mInd ; mInd = (lInd + rInd)/2 ; if(strcmp(rollNo, data[mInd].rollNo) <= 0) rInd = mInd ; else lInd = mInd + 1; } if(strcmp(rollNo, data[lInd].rollNo) == 0) Lect 29 Goutam Biswas
Image of page 5
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6 & $ % return lInd ; return NOTFOUND ; } int main() { int noOfStdnt, i, index ; struct studData data[MAXNO] ; char rollNo[ROLL] ; FILE *fp ; fp = fopen("openDat", "r"); fscanf(fp, "%d", &noOfStdnt); for(i=0; i<noOfStdnt; ++i) { fscanf(fp, "%s", data[i].rollNo); Lect 29 Goutam Biswas
Image of page 6
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 7 & $ % fscanf(fp, " %[^0-9]", data[i].name); fscanf(fp, "%f", &data[i].cgpa); } for(i=0; i<noOfStdnt; ++i) { printf("%s ", data[i].rollNo); printf(" %s", data[i].name); printf(" %5.2f", data[i].cgpa); putchar(’\n’); } putchar(’\n’); printf("Enter the Roll No.: "); fscanf(stdin, "%s", rollNo); Lect 29 Goutam Biswas
Image of page 7
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 8 & $ % index = binarySearch(data, 0, noOfStdnt-1, rollNo) ;
Image of page 8
Image of page 9

You've reached the end of your free preview.

Want to read all 30 pages?

  • Summer '19
  • File format, IIT Kharagpur, Indian Institutes of Technology

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes