C plus plus assignment help service

Append data into text file c programming source code

Leave a Comment
Write a C/C++ program to append a file in which user will enter the file name to open it then enters data to append it. Program should terminate on pressing escape key. When user press enter data should be enter in new line in text file. Use any Compiler CodeBlocks recommended.
Concept used:
Char Array for file name
Loop: while loop for enter data till escape is not pressed
File pointer  FILE * fp; pointer to file
getche() : To get character from console

For basic file input read: write data into  a text file c++ program

Program Explanation:
  • A character array of filename[20] having size equal to 20 used to enter the name of file which has to be appended. If a file is not present of name user entered then a new file will be created in the directory of your .cpp file
  • To append file "a" mode is used in fopen function
  • While loop is used to enter character till escape key is not pressed. When user presses escape a space is added into text file
  • If user presses enter key then an end line is added to text file
C Source Code

#include <iostream>
#include <stdio.h>
#include <conio.h>

using namespace std;

int main()
    FILE *fp;
    char ch, fileName[20];

    printf("Enter name of file with extension to append data e.g test.txt:  ");

    fp = fopen(fileName,"a");

    if(fp == NULL){
        printf("File Does Not Exist");
        }else {
        printf("Type data press escape to finish: \n\n");

    while(ch!=27) {

      if(ch==27) {
         putc(' ',fp);
      else {
      if(ch==13) {



    return 0;

Input output of program
append data into text file c programming source code


Post a Comment