Getting input in fstream

Hey all..i am writing a program in which im using fstream to input and output.
i dont understand how i can get and write inputs in file taking strings with whitespaces and line breaks.
First of all you should post the code. I don't really know what you want to do.

In any case, here's an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
ifstream f("filename.in");
ofstream g("otherfilename.out");
int main()
{    cout<<"Enter a string: ";  char x[100];
      cin.getline(x, 100);
      g<<x; // now you write in file named "otherfilename.out" the string x
      f.close();
      g.close();
      return (0);
}

If is something in the first file, (the file named "filename.in") you can read from it like this:
1
2
3
4
while(!=f.eof())
{  cin>>x;
   //other stuff...
}

I didn't compile the code so there may be some errors i think.
yeah bro,i dint post the code coz it was a query so i tried usin cin strin,gets string ,dint get to work..so i jus asked..

Yours works :D
however it doesnt do the line breaks,when u press enter it terminates..anything for the line bro?
You have to be more exactely. What do you mean by "do line breaks"? You want to write more lines in a file? If so, you can put the all procedure in a loop, like this:
1
2
3
4
5
for(i=1; i<=number_of_lines; i++)
{  cout<<"Enter the "<<i<<" line: ";
    cin.getline(x, 100);
    g<<x;
}
i meant cant we take input like spaces and line breaks..

for example : "my name is

Badshah"
So you want to read from a file 2 lines (or more). You can do it as a said in my first message in that topic. With while(!=f.eof()).
yeah got it bro..

Thanx a ton ! :D
Topic archived. No new replies allowed.