| |

VerySource

 Forgot password?
 Register
Search
Author: fcuptfwfn

File input and output problems

[Copy link]

1

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-12-17 19:15:01
| Show all posts
That is to say, out and in share a file pointer. After out<<s1;, the file pointer has reached the end of the file, and the content cannot be read when in>>s2. Unless seekp is called or the stream is refreshed, I understand it this way. ?
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-12-17 21:45:01
| Show all posts
No, it is that the operation of out<<s1 first writes the string to the cache. Only when the cache is full or the stream is refreshed, the string is actually written to the file. In other words, when you are in>>s2, the file is still empty.
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-12-18 10:45:01
| Show all posts
Good post
But when using vc6.0, you must #include <sstream> to compile and pass
Not use bbc
Reply

Use magic Report

1

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-12-18 14:00:01
| Show all posts
Then why out<<s1; can call out.seekp(0); afterwards? Doesn't seekp(0) make the pointer point to the beginning of the file?

There is another problem. As mentioned above, I want to create an input and output file stream, but the program does not create a text. What should I do?
The procedure is as follows:
#include<iostream>
#include<conio.h>
#include<fstream>


using namespace std;

int main()
{fstream file("a.txt",ios::in|ios::out);//Create input and output file stream
string s1,s2;

  s1="abcd 1234\n";
  
  file<<s1;
  getline(file,s2);
  
  cout<<"s2="<<s2<<endl;
  
  file.close();

getch();
return 0;
}
Reply

Use magic Report

0

Threads

7

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-12-18 20:00:01
| Show all posts
Try to close the output stream before the input
The file should be empty before closing the output stream
Or like this
ifstream in("filename",ios::in|ios::out);
ostream out(in.rabuf());
This way the input and output streams will use the same buffer
Even if the file is not written, it can be read out (read from the buffer)
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-12-18 21:15:01
| Show all posts
#include<iostream>
#include<conio.h>
#include<fstream>
#include<string>

using namespace std;

int main()
{
ofstream ofile("a.txt");//Create input and output file stream
string s1,s2;

s1="abcd 1234\n";
if(!ofile)
{
cout<<"cannot open!"<<endl;
return 1;
}

ofile << s1;
ofile.close();
ifstream ifile("a.txt");
if(!ifile)
{
cout<<"cannot open!"<<endl;
return 1;
}
getline(ifile,s2);
ifile.close();
cout<<"s2="<<s2<<endl;



getch();
return 0;
}
Reply

Use magic Report

1

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-12-19 10:45:02
| Show all posts
Is it only possible to define ifstream and ofstream?
Is it not enough to define only one fstream to complete the input and output work?
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-12-19 19:45:01
| Show all posts
No way
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-12-19 20:45:01
| Show all posts
// io/rw1. cpp

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

   int main()
   {
       // open file "example.dat" for reading and writing
       filebuf buffer;
       ostream output(&buffer);
       istream input(&buffer);
       buffer.open ("example.dat", ios::in | ios::out | ios::trunc);

       for (int i=1; i<=4; i++) {
           // write one line
           output << i << ". line" << endl;

          // print all file contents
          input.seekg(0); //seek to the beginning
          char c;
          while (input.get(c)) {
              cout.put(c);
          }
          cout << endl;
          input.clear(); //clear eofbit and failbit
       }
   }

To
The output of the program is as follows:

To
   1. line

   1. line
   2. line

   1. line
   2. line
   3. line

   1. line
   2. line
   3. line
   4. line
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-12-19 22:00:01
| Show all posts
First of all, we must understand that the file is opened by streambuf, not istream, nor ostream, so if in/out can operate on the same file, they need to be bound to the same streambuf, such as the filebuf above, so that you can The file is read and written.
Secondly, the two use the same pointer, so if you want to read after writing to filebuf, you have to call seekg(0) and return to the beginning
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list