File Handling in C (12)

Solve the following assignments in LAB

1.  Create a file "filename.txt".
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;

// Create a file
fptr = fopen("filename.txt""w");
// Close the file
fclose(fptr);

}

2. Write "Some text" in "file.txt" file.

#include<stdio.h>
#include<conio.h>
void main()

{

FILE *fptr;
// Open a file in writing mode
fptr = fopen("filename.txt""w");
// Write some text to the file
fprintf(fptr, "Some text");
// Close the file
fclose(fptr);

}

3. Add more data in file "file.txt".

#include<stdio.h>
#include<conio.h>
void main()

{

FILE *fptr;
// Open a file in append mode
fptr = fopen("filename.txt""a");
// Append some text to the file
fprintf(fptr, "\nHi everybody!");
// Close the file
fclose(fptr);

}

4. Read and display content of "file.txt" file.

#include<stdio.h>
#include<conio.h>
void main()

{
FILE *fptr;

// Open a file in read mode
fptr = fopen("filename.txt""r");
// Store the content of the file
char myString[100];
// If the file exist
if(fptr != NULL) 
{
  // Read the content and print it
  while(fgets(myString, 100, fptr)) 
{
    printf("%s", myString);
  }
// If the file does not exist
else 
{
  printf("Not able to open the file.");
}
// Close the file
fclose(fptr);

}

5. Write a c program to create a file to store name, address and phone in text file.

#include <stdio.h>

#include<conio.h>

int main() 

{

    char name[50], address[100], phone[20];

    FILE *fp;

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

    if (fp == NULL) {

        printf("Error: could not create file.\n");

        return 1;

    }

    printf("Enter name: ");

    fgets(name, 50, stdin);

    printf("Enter address: ");

    fgets(address, 100, stdin);

    printf("Enter phone number: ");

    fgets(phone, 20, stdin);

    fprintf(fp, "Name: %sAddress: %sPhone: %s", name, address, phone);

    fclose(fp);

    printf("File created successfully.\n");

    return 0;

}

 

6. Write a c program to add some more records in above file.

#include <stdio.h>

int main() 

{

    char name[50], address[100], phone[20];

    FILE *fp;

    fp = fopen("contacts.txt", "a");

    if (fp == NULL) {

        printf("Error: could not open file.\n");

        return 1;

    }

    printf("Enter name: ");

    fgets(name, 50, stdin);

    printf("Enter address: ");

    fgets(address, 100, stdin);

    printf("Enter phone number: ");

    fgets(phone, 20, stdin);

    fprintf(fp, "Name: %sAddress: %sPhone: %s", name, address, phone);

    fclose(fp);

    printf("Record added successfully.\n");

    return 0;

}


7. Write a program to display those records

#include <stdio.h>

int main() 

{

    char line[200];

    FILE *fp;

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

    if (fp == NULL) {

        printf("Error: could not open file.\n");

        return 1;

    }

    while (fgets(line, 200, fp) != NULL) {

        printf("%s", line);

    }

    fclose(fp)

    return 0;

}

Comments

Popular posts from this blog

Sample Question Bank Grade 12

Important Questions for XII

Question Collection-11