We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,307 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

output whole data

#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main()
{
    //system("clear");
    cout<<"\npls input the file where you want to read the data from\n";
    string loc;
    getline(cin,loc);
    ifstream g;
    g.open (loc.c_str(),ios::in);
    string data;
    g>>data;
    cout<<"\n\n\n\n";
    cout<<"\nthe data read from the file is\n";
    cout<<endl<<data;
    return 0;
}

even if fiole being read contains a full line of entries(separated by say spaces) then too output is the first word only....??

3
Contributors
3
Replies
2 Days
Discussion Span
11 Months Ago
Last Updated
5
Views
Question
Answered
learner_new
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Input using the >> operator is delimited by whitespace, so by default you'll only read the next "word" in the stream. If you want a full line, use getline().

deceptikon
Challenge Accepted
Administrator
3,499 posts since Jan 2012
Reputation Points: 822
Solved Threads: 481
Skill Endorsements: 58

Here's a quick example:

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

int main(){
    string filename;
    ifstream fin(filename.c_str(), ios::in);
    if (fin.is_open()){
        string line;
        while (getline(fin, line)){
            cout<<line;
        }
    }
    else cout<<"Invaild file.";
    return (0);
}

Getline will take the input from the file either till '\n' appears or the EOF (end of file).
So, you put it in a while loop, and you get the output.
You can add delimiters, but here's not the case.
Here's a usefull link about getline.

Lucaci Andrew
Practically a Master Poster
690 posts since Jan 2012
Reputation Points: 108
Solved Threads: 97
Skill Endorsements: 13
Question Answered as of 11 Months Ago by deceptikon and Lucaci Andrew

thankx guyz...

learner_new
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0633 seconds using 2.71MB