| |

VerySource

 Forgot password?
 Register
Search
View: 3769|Reply: 26

File input and output problems

[Copy link]

1

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-12-16 15:30:01
| Show all posts |Read mode
The following program can only write the content of s1 to a file, but cannot read it from the file. What is wrong?
#include<iostream>
#include<conio.h>
#include<fstream>


using namespace std;

int main()
{ofstream out("a.txt");
ifstream in("a.txt");
string s1,s2;

  s1="abcd 1234\n";
  out<<s1;
  in>>s2;

  cout<<"s2="<<s2<<endl;//s2 is empty~~~~

out.close();
in.close();
getch();
return 0;
}
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-12-16 16:45:01
| Show all posts
a.txt file needs path
Reply

Use magic Report

0

Threads

25

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

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


using namespace std;

int main()
{ofstream out("a.txt");
ifstream in("a.txt");
string s1,s2;

  s1="abcd 1234\n";
  out<<s1;
out.close();
in>>s2;

  cout<<"s2="<<s2<<endl;//s2 is empty~~~~


in.close();
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-16 18:30:01
| Show all posts
No need, a.txt is in the local project folder
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-12-16 18:45:01
| Show all posts
The path is not a problem, but my vc6.0 cannot be compiled
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-12-16 19:45:01
| Show all posts
Is there any lib to add?
Reply

Use magic Report

1

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-12-16 23:45:02
| Show all posts
I want to create a file stream that can both input and output, so that I don’t need to create ifstream and ofstream. I don’t know what to do? The a.txt file is not created when the following program is running, so I don't know what is wrong.
#include<iostream>
#include<conio.h>
#include<fstream>


using namespace std;

int main()
{fstream file("a.txt",ios::in|ios::out);
string s1,s2;

  s1="abcd 1234\n";
  file<<s1; //Cannot write into a.txt
  file>>s2;
  cout<<"s2="<<s2<<endl;//s2 is empty
  
  file.close();

getch();
return 0;
}
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-12-17 10:45:02
| Show all posts
Problems with output buffering
#include<iostream>
#include<conio.h>
#include<fstream>
#include<string>

using namespace std;

int main()
{ofstream out("a.txt");
ifstream in("a.txt");
string s1,s2;

  s1="abcd 1234\n";
  out<<s1;
  out.close();//or out.flush();
  getline(in,s2);

  cout<<"s2="<<s2<<endl;//s2 is empty~~~~

in.close();
getch();
return 0;
}
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-12-17 11:15:01
| Show all posts
Before the file is closed and the buffer is not full, the string is not actually written to the file unless you manually refresh the stream.
Reply

Use magic Report

0

Threads

25

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-12-17 15:15:01
| Show all posts
out<<s1;
  out.seekp(0);
// or out.close();
  in>>s2;
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