-->




copy data from one text file to another c source code

Leave a Comment
Write a c/c++ program which reads one text file and writes its data to another file. Hint use functions gets()  and fputc().

Concept used in this tutorial

Program explanation:
  • We have two files Input(text.txt) and output(text2.txt) data will be read from input file and write to output file which means that input file will be opened in read mode "r" and output file will be opened in write mode "r".
  • We have two if conditions which ensures files are accessible or not.
  • Using getc() function in while loop 'ch' is assigned character by character of input file then write into the output file using fuptc() function until it reaches EOF(end of file)
Source code

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

using namespace std;

int main()
{
    FILE *input;
    FILE *output;

    char ch;

    input = fopen("text.txt","r");
    if(input == NULL){

       printf("\n Required File text.txt failed to open ");

    } else {
       output = fopen("text2.txt","w");
           if(input == NULL){

            printf("\n Required File text2.txt failed to open ");

          } else { //File opened successfully

                while((ch = getc(input))!= EOF) {
                      fputc(ch, output);
                }

             printf("\n Data is Copied ");

           }

    }
    printf("\t\t");

    fclose(input );
    fclose(output);

    cout<<"\n\n\n";
    return 0;
}

To read and write file in your desire director give path in fopen function e.g
fopen("c:/text.txt","r");
It is recommended to do experiment with code and examine the output of program.  
Read More...

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:  ");
    scanf("%s",&fileName);

    fp = fopen(fileName,"a");

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

    printf("\t\t");
    while(ch!=27) {

      ch=getche();
      if(ch==27) {
         putc(' ',fp);
      }
      else {
         putc(ch,fp);
      }
      if(ch==13) {
         cout<<"\n";
         putc('\n',fp);

      }

    }

    fclose(fp);
    cout<<"\n\n\n";
    return 0;
}

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

Read More...

write data into a text file c++ program

4 comments
Write a C++ program in which user will continuously enter data into a text file and program should terminate when user press ESC key. Keep the program as simple as you can use any c++ compiler CodeBlocks is recommended.
Concept used
File pointer  FILE * represents pointer to a variable of FILE type

function fopen() is used to open a file and takes two parameters.
  1. Name of a file with its extension in double quotes it can include file path or not
    for example "NewFile.txt"  or  "c://NewFile.txt" if path is not specified it will open a
    file in current directory where your .c or .cpp file is.
  2. File opening mode it represent the purpose of opening of file and what operation will be perform on the file reading, writing, or editing. In this example file mode will be "w" for
    writing
fopen prototype FILE *fopen(const chr *fileName,  const char *mode );

Program explanation

  • A while loop is used and will terminate when user will press esc key. To get the characters a function getche() is used from conio.h which will store in char variable and store into the file using putc() function. 
  • On pressing esc to avoid any other character written into the file an if condition is used which will write space into the file when user will press escape.
C++ source code

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

using namespace std;

int main()
{
    FILE *fp;
    char ch;
    fp = fopen("text.txt","w");
    cout<<"Type characters to write into file"<<endl;
    cout<<"Press Esc to close file\n\n\n"<<endl;

    while(ch!=27) {

      ch=getche();

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


      if(ch==13) {
         cout<<"\n";
         putc('\n',fp);

      }

    }// end of while loop

    fclose(fp);
    cout<<"\n\n\n";
    return 0;
}


Input Output of program
c++ program to write data into a text file


Recommended: For good learning do experiment with code and examine the output
Read More...

Learn programming basics to advanced

Leave a Comment
In today’s world, technology is everywhere and for millions of us it’s a vital part of our lives. With jobs in the field of technology rapidly crushing the competition and ranking amongst some of the highest paying careers (many positions offering experts in the field annual base salaries of £57,000), there’s never been a better time to escape the grind and enter the world of code. 

At Europe’s #1 developer bootcamp, Makers Academy, our job is to help fast-track you into your role as a developer. Makers Academy is a bustling educational academy located in the heart of “Tech City,” dedicated to teaching our students how to become professional developers in just 12 weeks! Through a mixture of workshops, engaging exercises, projects, pair-programming, classroom break-out sessions, daily lectures and stand-ups and end of the week tests, our students quickly become proficient in HTML, Ruby on Rails, CSS3, Javascript & jQuery, Git and Heroku, Software Design and Agile / Lean methodologies, all while having fun and making life-long friendships. 

Makers Academy students are placed in an immersive environment, supported by expert coaches who are always ready to help. We understand that no two people learn in quite the same way, and this real-life interaction allows us to tailor our teaching to best fit every individual student. Our award-winning curriculum was developed with the finest software engineers and once admitted, students will spend about 15-20 hours per week learning the basics in our 4 week Pre-Course. They’ll then enrol in the Main Course where, on average, they will spend up to 70 hours per week honing their craft by engaging in practical exercises in the presence of trained experts. The final week of our program is dedicated to making you job-ready. This is when our dedicated placement team will closely work with you to help you land your dream job as a Junior Developer. Some of the top coding companies who continue to hire Makers Academy graduates include: PivotalLabs, Globaldev, Newbamboo, Mintdigital, Sky, Springer, Workshare, ThoughtWorks, Deloitte Digital, Mergermarket and many others. In many cases, just weeks after graduation Makers graduates are placed in developer positions earning anywhere from £25,000 to £40,000 per year!

Interested in joining the Makers Academy but can’t make it all the way to London to enrol? Then Ronin might be for you. To provide non-local coders with the same engaging, intensive learning experience, Makers Academy launched Ronin - a breakthrough course that leverages everything we’ve learned at Makers Academy to help people all over the world learn how to code all from the comfort of their home. Students will learn through creative, project-driven development and spend 12-16 weeks simulating life as a real developer. Ronin students will leave the program with at least 3 completed projects and the same dedicated assistance of our placement team to find them amazing jobs after graduation.

A career in code can open doors you’ve never imagined. It did for our graduates and they now work in fulfilling, challenging jobs that they love in an ever-growing industry that isn’t going away anytime soon. At Makers Academy, you won’t just learn syntax; you’ll learn how to become a real engineer. So whether you’ve always had a love for code or simply want to escape your boring career and see what all the buzz is about, apply to Makers Academy today and learn how your life can change for the better in just 12 weeks!  

Visit Makers Academy for pricing details and to learn more! 
Read More...