| |

VerySource

 Forgot password?
 Register
Search
View: 937|Reply: 5

C ++ file stream objects

[Copy link]

4

Threads

16

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-1-27 16:40:01
| Show all posts |Read mode
Use the same file stream object ifstream in_stream; to open multiple files, open one after reading it, then close it, and then use it to open the other, close, and fail when opening to the third file, why?
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 Invalid IP Address

Post time: 2020-2-21 12:00:02
| Show all posts
Post the code, it should not.
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-2-21 12:45:01
| Show all posts
The state of the stream object is retained internally, and you may need to call the clear, seek, ingor and other interfaces to reset the state.

It's better not to reuse variables.
Reply

Use magic Report

4

Threads

16

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

 Author| Post time: 2020-3-2 16:45:01
| Show all posts
Code:
 ifstream in_stream;

  in_stream.open ("stock library.txt");
  if (in_stream.fail ())
  {
      cout << "Opening file fail 1111!" << endl;
      exit (1); // Exit abnormally
  }

  string strtemp;
  int i;
  
  string strGoodsName; // Item name
  string strModelNum; // Product model
  string strGoodsNum; // Article number
  int nCount; // Number of products
  double dwPrice; // commodity purchase price (unit price)
  double dwProfits; // commodity profit ()
  string strProvider; // provider
  string strManName; // Operator
  
  // The following is the initial purchase warehouse
  in_stream.seekg (0, ios :: beg);

  for (i = 0; i <8; i ++)
in_stream >> strtemp; // Ignore the first line
  for (i = 0; i <12; i ++) // Read the content formally
  {
in_stream >> strGoodsName >> strModelNum >> strGoodsNum >> nCount >> dwPrice
>> dwProfits >> strProvider >> strManName;

m_InWareHouse.Add (CMerchandise (strGoodsName, strModelNum, strGoodsNum, nCount,
dwPrice, dwProfits, strProvider, strManName));
  }

  in_stream.close ();

  // The following is the initialization inventory
  in_stream.open ("Inventory.txt");
  if (in_stream.fail ())
  {
      cout << "Opening file fail 2222!" << endl;
      exit (1); // Exit abnormally
  }
  
  in_stream.seekg (0, ios :: beg);

  for (i = 0; i <7; i ++)
in_stream >> strtemp; // Ignore the first line

   for (i = 0; i <12; i ++) // Read the content formally
  {
in_stream >> strGoodsName >> strModelNum >> strGoodsNum >> nCount >> dwPrice
>> dwProfits >> strProvider;

m_InWareHouse.Add (CMerchandise (strGoodsName, strModelNum, strGoodsNum, nCount,
dwPrice, dwProfits, strProvider, ""));
  }

  in_stream.close ();

  // The following is the initialization sales library
  
   in_stream.open ("sales library.txt");

  if (in_stream.fail ())
  {
   cout << "Opening file fail 3333!" << endl;
      exit (1); // Exit abnormally
  }

  in_stream.seekg (0, ios :: beg);

  for (i = 0; i <7; i ++)
in_stream >> strtemp; // Ignore the first line

   for (i = 0; i <12; i ++) // Read the content formally
  {
in_stream >> strGoodsName >> strModelNum >> strGoodsNum >> nCount >> dwPrice
>> dwProfits >> strProvider;

m_InWareHouse.Add (CMerchandise (strGoodsName, strModelNum, strGoodsNum, nCount,
dwPrice, dwProfits, strProvider, ""));
  }

   in_stream.close ();
  
The basic code is the above, and the result is "Opening file fail 3333!"
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-3-5 21:00:02
| Show all posts
Agreesuperandy
Add a sentence after close:
in_stream.clear ();
Clear all status flags

in_stream.fail () returns true, indicating that a failbit or badbit is set. And when in_stream reads
End-of-file will set up eofbit and failbit at the same time
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-19 15:00:01
| Show all posts
It turned out to be like this, I understand. I also encountered this problem, and I still don't understand it. Experiment.
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