// 9_6.cpp : Defines the entry point for the console application.
//
//#define _CRT_SECURE_NO_WARNINGS
#include "stdafx.h"
#include <stdio.h>
//#include <cstdio>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define MAX_COMMODITY_NUM 100
#define COM_PERO_LEN 30
struct _comStrc
{
char sNo[COM_PERO_LEN];
char sType[COM_PERO_LEN];
char sWhereCounter[COM_PERO_LEN];
float price;
int iTotal;
long lDate;
};
_comStrc InputCommInfo()
{
_comStrc m_comm;
fflush(stdin);
cout << setw(45) << "商品信息输入:\n";
cout << setw(30) << "商品编号:";
//gets(m_comm.sNo);
gets_s(m_comm.sNo);//安全你妈逼!非要用这破逼东西才编译通过
// getline(cin, m_comm.sNo);
cout << setw(30) << "商品类型:";
gets_s(m_comm.sType);
cout << setw(30) << "存储柜台:";
gets_s(m_comm.sWhereCounter);
cout << setw(30) << "商品单价:";
cin >> m_comm.price;
cout << setw(30) << "商品数量:";
cin >> m_comm.iTotal;
cout << setw(30) << "生产日期:";
cin >> m_comm.lDate;
return m_comm;
}
void WriteCommodityData(char *fname,_comStrc *com,int iLen)
{
FILE *fp;
if (NULL==(fp=fopen(fname,"ab")))//可写二进制数据
{
printf("can not open file!\n");
exit(1);
}
fwrite(&iLen,sizeof(int),1,fp);
if (ferror(fp))
{
cout << "写入商品数量时出错,请检查一下,O(∩_∩)O谢谢!\n";
fclose(fp);
return;
}
fwrite(com, sizeof(_comStrc), iLen, fp);//一次写入多个
fclose(fp);
}
void ReadCommodity(char *fname,_comStrc *com,int &iLen)
{
FILE *fp;
if (NULL==(fp=fopen(fname,"rb")))
{
printf("can not open file!\n");
exit(1);
}
if (feof(fp))
{
cout << "抱歉,文件是空的!";
return;
}
fread(&iLen,sizeof(int),1,fp);
printf("读出的商品个数:%d\n", iLen);
if (ferror(fp))
{
printf("从文件中读商品数量时出错!\n");
fclose(fp);
return;
}
if (fread(com,sizeof(_comStrc),iLen,fp) != iLen)
{
printf("read file error!");
fclose(fp);
exit(1);
}
cout.setf(ios::left);
printf("读出的商品信息如下:\n");
cout << setw(10) << "商品编号" << setw(10) << "商品类型" << setw(10) << "存储柜台" << setw(10) << "商品单价" << setw(10) << "商品数量" << setw(10) << "生产日期" << endl;
for (size_t i = 0; i < iLen; i++)
{
cout << setw(10) << com[i].sNo << setw(10) << com[i].sType << setw(10) << com[i].sWhereCounter << setw(10) << com[i].price << setw(10) << com[i].iTotal << setw(10) << com[i].lDate << endl;
}
cout.unsetf(ios::left);
fclose(fp);
}
#define COMM_LEN 3
int main()
{
int iLen = 0;
char filename[20] = { 0 };
_comStrc m_comm[COMM_LEN], m_rcomm[COMM_LEN];
printf("请输入要写入商品信息的文件名:\n");
scanf("%s", filename);
printf("请输入%d个商品的信息:\n",COMM_LEN);
for (size_t i = 0; i < COMM_LEN; i++)
{
m_comm[i] = InputCommInfo();
/*int iRet = sizeof(m_comm[i]);//for test
iRet = sizeof(_comStrc);
iRet = 0;*/
}
WriteCommodityData(filename, m_comm, COMM_LEN);
ReadCommodity(filename, m_rcomm, iLen);
return 0;
}
10
你商品编号输入了几位?
改成
gets_s(m_comm.sNo, COM_PERO_LEN );
试试
5
char sWhereCounter[COM_PERO_LEN];
fgets(sWhereCounter,COM_PERO_LEN,stdin);
if ("\n"==sWhereCounter[strlen(sWhereCounter)-1]) sWhereCounter[strlen(sWhereCounter)-1]=0;
5
VS2015已经彻底放弃gets了
http://blog.csdn.net/hgj125073/article/details/8282883
5
你的InputCommInfo就不对,使用了浅拷贝,返回了局部变量的地址,所以不要怪get_s函数啦,get_s函数是无辜的
谢谢你的回答,但是假如真是浅拷贝引起的,应该都得不到输入( ⊙ o ⊙ )啊!为什么本人总是结构体的第一个成员 char sNo[COM_PERO_LEN];得不到输入,而后面的5个成员都能正确得到输入呢?
char sType[COM_PERO_LEN];
char sWhereCounter[COM_PERO_LEN];
float price;
int iTotal;
long lDate;涉及到内存管理的问题,具体的实现不太明白。
下面的代码应该可以运行struct _comStrc { _comStrc(const _comStrc &c) { strcpy(c.sNo,sNo); strcpy(c.sType,sType); strcpy(c.sWhereCounter,sWhereCounter); c.price=price; c.iTotal=iTotal; c.lDate=lDate; } char sNo[COM_PERO_LEN]; char sType[COM_PERO_LEN]; char sWhereCounter[COM_PERO_LEN]; float price; int iTotal; long lDate; };你好,本人按你说的,修改结构体定义后,还没编译就报错,提示const类型不兼容
1>– Build started: Project: 9_6, Configuration: Debug Win32 —
1> 9_6.cpp
1>d:\vs2015exercise\文件操作\9_6\9_6\9_6.cpp(27): error C2664: “char *strcpy(char *,const char *)”: cannot convert argument 1 from “const char [30]” to “char *”
1> d:\vs2015exercise\文件操作\9_6\9_6\9_6.cpp(27): note: Conversion loses qualifiers
1>d:\vs2015exercise\文件操作\9_6\9_6\9_6.cpp(28): error C2664: “char *strcpy(char *,const char *)”: cannot convert argument 1 from “const char [30]” to “char *”
1> d:\vs2015exercise\文件操作\9_6\9_6\9_6.cpp(28): note: Conversion loses qualifiers
1>d:\vs2015exercise\文件操作\9_6\9_6\9_6.cpp(29): error C2664: “char *strcpy(char *,const char *)”: cannot convert argument 1 from “const char [30]” to “char *”
1> d:\vs2015exercise\文件操作\9_6\9_6\9_6.cpp(29): note: Conversion loses qualifiers
1>d:\vs2015exercise\文件操作\9_6\9_6\9_6.cpp(30): error C3490: “price” cannot be modified because it is being accessed through a const object
1>d:\vs2015exercise\文件操作\9_6\9_6\9_6.cpp(31): error C3490: “iTotal” cannot be modified because it is being accessed through a const object
1>d:\vs2015exercise\文件操作\9_6\9_6\9_6.cpp(32): error C3490: “lDate” cannot be modified because it is being accessed through a const object
1>d:\vs2015exercise\文件操作\9_6\9_6\9_6.cpp(43): error C2512: “_comStrc”: no appropriate default constructor available
1> d:\vs2015exercise\文件操作\9_6\9_6\9_6.cpp(24): note: see declaration of “_comStrc”
1>d:\vs2015exercise\文件操作\9_6\9_6\9_6.cpp(165): error C2512: “_comStrc”: no appropriate default constructor available
1> d:\vs2015exercise\文件操作\9_6\9_6\9_6.cpp(24): note: see declaration of “_comStrc”
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
好像写错了。
struct _comStrc
{
_comStrc() {}
_comStrc(const _comStrc &c)
{
strcpy(sNo,c.sNo);
strcpy(sType,c.sType);
strcpy(sWhereCounter,c.sWhereCounter);
price=c.price;
iTotal=c.iTotal;
lDate=c.lDate;
}
char sNo[COM_PERO_LEN];
char sType[COM_PERO_LEN];
char sWhereCounter[COM_PERO_LEN];
float price;
int iTotal;
long lDate;
};