How to print a sentence (including white spaces) from a file ?
Or
How to print entire text file which has a paragraph?

I have tried a lot and the final programs is shown below..

but it can't print sentences exceeding certain length...
it shows unwanted ASCII characters :(

How to overcome this problem?

    #include<iostream.h>
    #include<fstream.h>
    #include<iomanip.h>

    int main()

    {
     char msg[100];
     ofstream ofile("Helloworld.txt");
     cout<<"Enter a sentence: ";
     cin.getline(msg,100);
     ofile<<msg;
     ofile.close();
     ifstream ifile("Helloworld.txt");
     cout<<"File contents:\n\n\n";
     ifile>>msg;
     cout.write(msg,30);
     return 0;
    }

Edited 4 Years Ago by danibecse: Title,coding

danibecse
Deleted Member

Sorry Friends Unfortunately I deleted the danibecse membership
continue with this new one

Well the reason you can't print a full sentence is because you write only 30 characters max:

cout.write(msg,30);

Have a look at the cout.write function: Click Here
Instead you should write the entire msg:

cout<<msg<<endl;

Here, I put an example: this will read an entire file, and will display the content on your screen, each line at a time:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main(){
    cout<<"Filename: ";
    string filename;
    cin>>filename;
    ifstream fin(filename.c_str(), ios::in);
    if (!fin.good()){
        cerr<<"Invalid file.\n";
    }
    else{
        string line;
        int i=0;
        while(getline(fin, line)){
            cout<<"Line ["<<++i<<"]: "<<line<<endl;
        }
    }
    return (0);
}

Edited 4 Years Ago by Lucaci Andrew

Comments
But in my Program if I give string less than 30 chracters, it prints some garbage values ; your programs works correctly, Thanks for your response

How to print a sentence (including white spaces) from a file ?
Or How to print entire text file which has a paragraph?

Assunming that you have a compiler that is not antique:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::string filename ;
    std::cout << "file mame? " && std::cin >> filename ;
    std::ifstream file( filename.c_str() ) ;
    std::cout << file.rdbuf() ;
}
Comments
I'm using Turbo C 4.5

Friends Finally I found the coding from This Website

But String Data Type not supported in some Compilers like Turbo C.
I am using only Turbo c in my college.

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<conio.h>
using namespace std;

int main()
{
 string msg;
 ofstream ofile("Helloworld.txt");
 cout<<"Enter a sentence: ";
 getline(cin,msg);
 ofile<<msg;
 ofile.close();
 ifstream ifile("Helloworld.txt");
 cout<<"File contents:\n\n\n"<<msg;
 ifile>>msg;
 return 0;
 getch();
}

Sorry, The above program by me is not correct it just prints the given array (not from file)
Here is the correct coding

#include<iostream>
#include<fstream>
#include<string.h>
#include<conio.h>
using namespace std;

int main()
{
 char * buffer;
 long size;
 string msg,txt;

 ofstream ofile("Helloworld.txt");
 cout<<"Enter a sentence: ";
 getline(cin,msg);
 ofile<<msg;
 ofile.close();

 cout<<"\nFile contents:\n";
 ifstream ifile("Helloworld.txt");
 // get size of file
 ifile.seekg(0,ifstream::end);
 size=ifile.tellg();
 ifile.seekg(0);

 // allocate memory for file content
 buffer = new char [size];

 // read content of ifile
 ifile.read (buffer,size);

 // write to outfile
 cout.write (buffer,size);

 // release dynamically-allocated memory
 delete[] buffer;

 return 0;
}
This article has been dead for over six months. Start a new discussion instead.