​​​​​​​​FILE NAMES​


​​​​​​​​​​​Hey everyone, I just found something new in C++… It’s regarding files.​
​​
​Though I have been using C++ from 2 years or more now, I never thought of making text or other files from a ​
​program whose name wasn’t predefined i.e. the name of the file that has to be made is asked by the user. This ​
​idea came to me when I was trying to do the same thing in python. And after I did it in python I tried to do in C++ ​
​too, and to my astonishment, I did that too… congrats to me!!! :)​
​​
​​
​​
​​​​​​​In Python:​
​​
​​
​​
​​
​​​​​a=input(“Enter the file name”)​
​f=open(a+’.txt’,’w’)​
​a=input(“Enter data to be written to the file:”)​
​f.write(a)​
​print(“Data has been entered…”)​
​a=input(“Enter the file name”)​
​f=open(a+’.txt’)​
​print(f.read())​
​​
​​
​​
​​​​​​​Explanation:​
​​
​​
​​
​​
​​

    ​​

  1. ​​​​​The first line gets the name of the file the user wants.​​​​​​​​​
    ​​
    ​​
    ​​
  2. ​​​​​Second line makes f as the file pointers, creates a “.txt” file in the same folder as the program with the ​
    ​​
    ​​
    ​​​​​same name as acquired from the user in the first line and opens it for writing.​​​​​​​​​
    ​​
    ​​
    ​​
  3. ​​​​​Third and fourth line get the data from the user to be written in the file and writes it.​​​​​​​​​
    ​​
    ​​
    ​​
  4. ​​​​​Fifth line declares that the data has been entered in the file.​​​​​​​​​
    ​​
    ​​
    ​​
  5. ​​​​​Sixth line again asks the user for the name of the file. This line can be omitted easily with some changes ​
    ​​
    ​​
    ​​​​​in variable names.​​​​​​​​​
    ​​
    ​​
    ​​
  6. ​​​​​Seventh and eighth line again open the file for reading and then reads out all the contents of the file.​​​​​​​​​
    ​​
    ​​
    ​​

​​​​​​
​​
​​
​​
​​​​​​​In C++​​​​​​​​​​ (The program has been made in Dev C++ 4.9.9.2):​
​​
​#include<iostream>​
​#include<conio.h>​
​#include<fstream>​
​using namespace std;​
​int main(){​
​  char a[20],b[40];​
​  cout<<“Enter a file name:\n”;​
​  gets(a);​
​  ofstream f;​
​  strcat(a,”.txt”);​
​  f.open(a,ios::app);​
​  cout<<“Enter data to be written:\n”;​
​  gets(b);​
​  f.write((char *)&b, sizeof(b));​
​  f.close();​
​  return 0;​
​  }​
​​
​​
​The program has some limitations like unlike the program of python:​
​​
​​
​​

    ​​

  1. ​​​​​The file created here is appended. (i.e. the file’s previous contents are not deleted here, though this can ​
    ​​
    ​​
    ​​​​​be ammended by using “ios::in” instead of “ios::app”)​​​​​​​​​
    ​​
    ​​
    ​​
  2. ​​​​​The data to be written is limited to 40 letters as the array’s size is 40 only. Moreover, if the data entered ​
    ​​
    ​​
    ​​​​​is less than 40 letters then the data written is random i.e. the data entered by you is written as it is and ​
    ​​
    ​​
    ​​​​​rest of the space is occupied by random letters.​​​​​​​​​
    ​​
    ​​
    ​​

​​​​​​
​The above limitation can be easily handled. Instead of taking a string we can take a character a time and write ​
​it to file. This will end by using a flag character which can be anything. For eg.​
​​
​char b;​
​  while(b!=’.’){​
​                cin>>b;​
​                f<<b;​
​                }​
​Here the writing to the file ends when we enter a full stop because we have created a check for a ‘.’ in the while ​
​loop. This can also be changed to anything you had liked by replacing it with what you like.​
​​
​​
​​​

Advertisements
Explore posts in the same categories: C++

Tags: , ,

You can comment below, or link to this permanent URL from your own site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: