| |

VerySource

 Forgot password?
 Register
Search
View: 21|Reply: 8

Seeking a good way to teach text file modification in VC

[Copy link]

1

Threads

4

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

Post time: 2020-2-23 19:30:01
| Show all posts |Read mode
I store the structure in a text file (student information record name student number transcript)
For example, the content in the text file is:

MaWenTao
99001
88
LiMing
99002
92
WangFang
99003
89

Now I want to modify LingMing's information record, such as grade 92 to 95, please enlighten, I want to explain that I can not use the database, thank you!
Reply

Use magic Report

0

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-5-6 17:30:01
| Show all posts
Use the string to match the structure header that needs to be modified, and then read out the modified structure content and write it to the file again.
Reply

Use magic Report

0

Threads

75

Posts

27

Credits

Newbie

Rank: 1

Credits
27

 China

Post time: 2020-5-7 06:15:01
| Show all posts
Is the file content search ~ ~
Reply

Use magic Report

1

Threads

4

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

 Author| Post time: 2020-5-7 06:45:01
| Show all posts
I can do it this way, but I have to rewrite the entire file every time. I want to know if there is a method such as using the seek pointer to locate the method that can be directly modified, or there are other libraries that can directly call the method to modify it. Thank you!
Reply

Use magic Report

1

Threads

4

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

 Author| Post time: 2020-5-7 10:45:01
| Show all posts
Consult the master to give the code.
Reply

Use magic Report

0

Threads

75

Posts

27

Credits

Newbie

Rank: 1

Credits
27

 China

Post time: 2020-5-7 15:30:01
| Show all posts
You can define a structure,
Three members: Name Student ID Results

Use the CFILE Read method or the standard library fread function,
Or ifsteam's read method ...

Each time you read the contents of a structure,
Then determine whether the name is a record that needs to be modified,
If yes, modify it,
If not, continue to read the information of the next record.

After modification, just write the record back to the file,
Pay attention to adjust the position of the file pointer ~~
Reply

Use magic Report

0

Threads

53

Posts

42

Credits

Newbie

Rank: 1

Credits
42

 Invalid IP Address

Post time: 2020-5-8 08:30:01
| Show all posts
If the modification is not of equal length, you can only rewrite the entire file at a time
Reply

Use magic Report

1

Threads

4

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

 Author| Post time: 2020-5-8 14:00:01
| Show all posts
Should be modified
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-5-30 13:30:01
| Show all posts
You try
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

typedef struct {
long long ID;
string name;
int r1, r2, r3;
} data, * pdata;
  
  int main ()
  {
data d;
d.ID = 5050379001;
d.name = "4k.grubby";
d.r1 = 85;
d.r2 = 87;
d.r3 = 83;
data d0, d1;
d0.ID = 5050379002;
d0.name = "john";
d0.r1 = d0.r2 = d0.r3 = 90;
d1.ID = 5050379003;
d1.name = "tom";
d1.r1 = d1.r2 = d1.r3 = 87;
  ofstream File ("student.db", ios :: binary); // New binary file
  File.write ((const char *) (&d), sizeof (data));
  File.write ((const char *) (&d0), sizeof (data));
  File.write ((const char *) (&d1), sizeof (data));
  d1.name = "jerry"; // Modify the data
  File.seekp (2 * sizeof (data)); // Move to the third group of data
  File.write ((const char *) (&d1), sizeof (data)); // Overwrite
  File.close ();
  ifstream iFile ("student.db", ios :: binary);
  data d2;
  iFile.seekg (sizeof (data) * 2);
  iFile.read ((char *) (&d2), sizeof (data));
  cout << d2.name << d2.ID << endl;
  iFile.close ();
  }
Reply

Use magic Report

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

Points Rules

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

Quick Reply To Top Return to the list