tellp()和tellg()的关系是什么

tellp()和tellg()的关系是什么,第1张

tellg() ——返回一个int型数值,它表示“内置指针蠢誉”的当前位置。此函数仅当你在读取一个文件时有效。例如:

#include <fstream.h>

void main()

{

//悄喊 假如我们已经在test_file.txt中存有了“Hello”的内容

ifstream File("test_file.txt")

char arr[10]

File.read(arr,10)

// 由于Hello占5个字符,因此这里将返回5

cout <<File.tellg() <<endl

File.close()

}

tellp() —— 与tellg()有同样的功能,但它用于写文件时。总而言之:当我们读取一个文件,并要知道内置指针的当前位置时,应该使用tellg();当我们启档野写入一个文件,并要知道内置指针的当前位置时,应该使用tellp(). 由于此函数的用法与tellg()完全一样

#include<iostream>

#include<fstream>

#include<string>

#include<list>

#include<sstream>

#include<Windows.h>

using namespace std

class Goods{

public:

Goods(string Id="", string Name="", double Price=0, int Number=0) :

id(Id), name(Name), price(Price), number(Number){}

string GetName(){ return name}

string GetId(){ return id}

double GetPrice(){ return price}

int GetNumber(){ return number}

private:

string id

string name

double price

int number

}

class Storehouse{

public:

Storehouse()

void DeleteSize()

void AddSize()

void AddSize(Goods)

void FindName()

void FindId()

void ShowLessNumber()

void ShowAllGoods()

list<Goods>::iterator Begin(){ return goodsList.begin()}

list<Goods>::iterator End(){ return goodsList.end()}

private:

bool CheckId(string)

bool CheckName(string)

list<Goods>goodsList

void ShowGoods(list<Goods>::iterator)

}

Storehouse::Storehouse(){}

void Storehouse::ShowGoods(list<Goods>::iterator iter){

cout <<"编号:" <<iter->GetId() <<endl

cout <<"名称:" <<iter->GetName() <<endl

cout <<"单价:" <<iter->GetPrice() <<厅团 endl

cout <<"数量:" <<iter->GetNumber() <<endl

}

void Storehouse::DeleteSize(){

string name

cout <<"请输入需要删除的货物的名称" <<endl

cin >>name

list<Goods>::iterator beg = goodsList.begin()

list<Goods>::iterator end = goodsList.end()

while (beg!= end){

if (beg->GetName() == name)

break

beg++

}

if (beg == end){

cout <<"你输入的货物不存在" <<endl

}

else{

goodsList.erase(beg)

cout <<"已成功删除该货物信息" <<endl

}

}

void Storehouse::AddSize(){

string id, name

double price

int number

cout <<"添加的货物编号是" <<endl

cin >>id

while (1){

if (CheckId(id))

break

else{

cout <<"携敬输入的货物编号已存在,需辩伏慎重新输入编号:"

cin >>id

}

}

cout <<"货物名称是:" <<endl

cin >>name

while (1){

if (CheckName(name))

break

else{

cout <<"输入的货物名称已存在,需重新输入名称"

cin >>name

}

}

cout <<"货物单价是" <<endl

cin >>price

cout <<"货物数量是:" <<endl

cin >>number

Goods goods(id, name, price, number)

goodsList.insert(goodsList.end(), goods)

}

void Storehouse::AddSize(Goods goods){

goodsList.insert(goodsList.end(),goods)

}

void Storehouse::FindId(){

string id

cout <<"要查询的货物编号是:" <<endl

cin >>id

list<Goods>::iterator beg = goodsList.begin()

list<Goods>::iterator end = goodsList.end()

while (beg != end){

if (beg->GetId() == id)

break

beg++

}

if (beg == end)

{

cout <<"该编号货物不存在"

}

else{

cout <<"该货物信息如下:" <<endl

ShowGoods(beg)

}

}

void Storehouse::FindName(){

string name

cout <<"要查询的货物的名称是" <<endl

cin >>name

list<Goods>::iterator beg = goodsList.begin()

list<Goods>::iterator end = goodsList.end()

while (beg != end){

if (beg->GetName() == name)

break

beg++

}

if (beg == end){

cout <<"该名称的货物不存在" <<endl

}

else

ShowGoods(beg)

}

void Storehouse::ShowLessNumber(){

int number

cout <<"要显示数量少于多少件的货物?" <<endl

cin >>number

list<Goods>::iterator beg = goodsList.begin()

list<Goods>::iterator end = goodsList.end()

cout <<"数量少于" <<number <<"个的商品分别有:" <<endl

while (beg != end){

if (beg->GetNumber() <number){

ShowGoods(beg)

cout <<endl

}

beg++

}

}

void Storehouse::ShowAllGoods(){

list<Goods>::iterator beg = goodsList.begin()

list<Goods>::iterator end = goodsList.end()

int i = 0

cout <<"所有货物的信息:" <<endl

while (beg != end){

i++

cout <<"第" <<i <<"个货物信息:" <<endl

ShowGoods(beg)

cout <<endl

beg++

}

}

bool Storehouse::CheckId(string id){

list<Goods>::iterator beg = goodsList.begin()

list<Goods>::iterator end = goodsList.end()

for (beg != endbeg++){

if (beg->GetId() == id)

break

}

return beg == end

}

bool Storehouse::CheckName(string name){

list<Goods>::iterator beg = goodsList.begin()

list<Goods>::iterator end = goodsList.end()

for (beg != endbeg++){

if (beg->GetName() == name)

break

}

return beg == end

}

void ReadGoods(Storehouse&store){

system("cls")

ifstream inNotes("保存记录.txt", ios::_Noreplace)

string choice

string textName

getline(inNotes, textName)

if (textName.empty()){

cout <<"输入2读取制定路径文件,输入其他直接进入 *** 作页面" <<endl

getline(cin, choice)

}

else{

cout <<"检查到你上次数据保存到" <<textName <<endl

cout <<"输入1读取该文件的内容,输入2读取指定路径文件,输入其他直接进入 *** 作页面" <<endl

getline(cin, choice)

}

if (choice != "1"&&choice!="2")

return

if (choice == "2")

{

cout <<"输入读取路径:"

getline(cin, textName)

}

if (choice == "1"&&textName.empty())

return

ifstream inGoods(textName,ios::_Nocreate)

if (!inGoods.is_open()){

cout <<"无法读取该路径文件,请检查路径是否有误,\n按回车键继续..." <<endl

getline(cin,choice)

ReadGoods(store)

return

}

string text

string name

string id

double price

int number

while (getline(inGoods, text)){

stringstream ss(text)

for (int i = 0i <4i++)

switch (i){

case 0:ss >>idbreak

case 1:ss >>namebreak

case 2:ss >>pricebreak

case 3:ss >>numberbreak

}

Goods goods(id, name, price, number)

store.AddSize(goods)

}

}

void SaveGoods(Storehouse store){

string pathName

cout <<"输入需要保存的路径:"

cin.clear()

cin.sync()

getline(cin, pathName)

ofstream out(pathName)

list<Goods>::iterator beg = store.Begin()

list<Goods>::iterator end = store.End()

while (beg != end){

out <<beg->GetId() <<" " <<beg->GetName() <<" " <<beg->GetPrice() <<" " <<beg->GetNumber() <<endl

beg++

}

ofstream outNotes("保存记录.txt", ios::trunc)

outNotes <<pathName

}

void ToBeEnd(Storehouse&store){

system("cls")

int i

cout <<"1.保存,2.不保存" <<endl

while (1){

cin.clear()

cin.sync()

cin >>i

switch (i)

{

case 1:SaveGoods(store)return

case 2:return

default:cout <<"输入有误,重新输入1或者2,选择是否保存数据" <<endlbreak

}

}

}

void Reback(){

cout <<"\n\n按下回车键返回..." <<endl

string s

cin.clear()

cin.sync()

getline(cin, s)

}

void main(){

bool toBeEnd = false

int i

string s

Storehouse house

ReadGoods(house)

while (1){

system("cls")

cout <<"1.添加货物, 2.删除货物, 3.按编号查询货物, 4.按名称查询货物, \n"

cout<<"5.输出数量不足的货物, 6.显示所有货物信息, 7退出" <<endl

cin >>i

system("cls")

switch (i){

case 1:house.AddSize()break

case 2:house.DeleteSize()break

case 3:house.FindId()break

case 4:house.FindName()break

case 5:house.ShowLessNumber() break

case 6:house.ShowAllGoods()break

case 7:toBeEnd = truebreak

default:cout <<"请输入1至7的数字选择 *** 作\n"break

}

if (toBeEnd)

break

Reback()

}

ToBeEnd(house)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12230803.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-22
下一篇 2023-05-22

发表评论

登录后才能评论

评论列表(0条)

保存