You must Sign In to post a response.
  • C program to manipulate the file operations.

    Finding difficult to write a C program? Looking out for solution to manipulate file operations? Scroll through this page for program solutions from experts.

    Following is the question asked in Dr. Babasaheb Ambedkar Marathwada University B.Sc Computer Science (Honours)(C programming II Bsc computer science 2nd sem) question paper for October 2018 examination.
    This question is asked for 10 marks and hence requires some explanation along with program code. Please let me know the solution.

    Q)Write a program in c to open file named test.txt, add Name and address of university, close the file. Reopen the file and read the file and display the data. [10 Marks]
  • Answers

    2 Answers found.
  • #include stdio.h
    int main()
    FILE *fp;
    char s[100];
    fp = fopen("test.txt", "w");
    printf("Enter the name and address of the university");
    scanf("%s", s, &n);
    fprintf(fp, "%s", s);
    fp = fopen("test.txt", "r");
    fscanf(fr, "%s%d", s, &n);
    printf("%s %d", s);
    return 0;

    Stdio.h is a header file used to declare the variables and functions. This should be used with angular brackets. (Here it is not used as it is considered as HTML tag and not working)
    Int main() – This is the main function where the program will be called and executed
    fp is the file pointer
    char s[100] – declaring the string value s and can have a maximum of 100
    fp=fopen() – Using the fopen function to open a file (also to create a file is it is not existing)
    w – it is the mode that is being used. w represents write functionality, r represents read functionality
    printf – printf function is used to print the value which is been entered
    scanf – scanf function is used to get the value which is being entered by the user
    fprintf – frpintf function is used to write the value (here it is name and address of the university) into the file
    fclose() – fclose() function is used to close the opened file (here it is test.txt)
    Again, we are opening the file test.txt in reading mode.
    fscanf function is used to read the content from the file (name and address of the university)
    printf function is used to display the file content on the screen (name and address of the university)
    closing the file using fclose function
    return 0 indicates the program is done and this is the exit status for the program

    Jagannathan S
    ISC Member

  • In C programming a method is used to store the data on disk and retrieve the same data as when required from the disk without disturbing the data. To achieve this goal we use a concept called as file.
    In C programming we use three types of I/O functions(input/output functions).
    1) Console I/O functions
    2) Disk I/O functions
    3) Port I/O functions
    The disk I/O functions are used while manipulating the files and hence it is also called as file manipulation. While writing file manipulation programs we have to use data structure called as FILE which is defined in the library of standard I/O functions. Different file I/O functions are available. Functions like fopen(), fclose(), getc(), putc(), fprintf(), fscanf(), getw(), putw(), fseek(), ftell(), rewind() are normally used in file handling. Like file handling functions there are different file opening modes like r, w, a, r+, w+ and a+ mode are used.
    below is the program which open the file test.txt and accept name and address of university and display the contents of the text file back on the screen.
    Please include stdio.h, conio.h and stdlib.h header files before main function.

    void main(void)
    FILE * fp;
    char ch;
    printf("\nEnter name and address of your university\n");
    printf("\nPress ctrl + z key and enter key to stop inputting\n");
    printf("\nOutput is as follows which shows name and address of your university\n");

    printf("Error in opening the file\n");

    A word file is attached herewith which include the output of the above program.

    In above program firstly fp pointer of type FILE is declared. Using this FILE pointer we open the file test.txt in w+ mode first. Here w+ mode means if file with name test.txt is already exist then it destroy the previous contents of the file and open the file for writing. If file in not already exist then it creates the new file for writing.
    When out inputting is over we have to press ctrl + z character so that compiler put end of character at the end of the file. This character is very important to recognize the end of file situation.
    We use getchar() to accept character by character input from keyboard and using putc() function we write character by character data to file test.txt.
    After writing is over we use fclose() function.
    Now we reopen the test.txt file in reading mode. We read the file character by character using getc() and immediately print that character on monitor using printf().. When EOF(end of file) is encountered reading from file is over.
    After reading the contents of file once again we use fclose().


    Delete Attachment

  • Sign In to post your comments