为什么结果没有写在文本文件上?
Why the result doesn't write on the text file?
我已经创建了csis.txt
文件,但是当我查找时,它是空的。谁能教我为什么文件创建没有结果?以下是我的代码:
//ZipCode.cpp
#include <iostream>
#include "ZipCode.h"
#include <fstream>
#include <string>
#include <math.h>
#include <sstream>
using namespace std;
extern ofstream csis;
ZipCode::ZipCode(int _zipcode) {
zipcode = _zipcode;
}
ZipCode::ZipCode(const char *barcode) {
int value;
zipcode = 0;
for (int i = 0; i<5; i++) //repeat x5 positions
{
value = 0;
for (int j = 0;j<5;j++) //repeat x5 letters
{
if (barcode[i * 5 + j] == '1') //if '1' is letter
{
value += base[j]; //add position(5)
}
}
if (value >10)
{
value = 0;
}
zipcode = zipcode * 10 + value;
}
}
string ZipCode::getBarCode()
{
char barcode[26] = "";
int zip = zipcode;
int value;
for (int i = 0;i<5;i++) //repeat x5 positions
{
for (int j = 0;j<4;j++) //repeat x4(last barcode position 0)
{
//10^position * barcode position goes to value
value = (int)(pow(10.0, 4 - i) * base[j]);
if (zip / value)
{
barcode[i * 5 + j] = '1'; //set as '1'
zip = zip % value;//zip is now remainder
}
else
{
barcode[i * 5 + j] = '0'; //set as '0'
}
}
barcode[i * 5 + 4] = '0';
}
return barcode;
}
int ZipCode::getZipCode()
{
return zipcode;
}
//ZipCode.h
#ifndef _ZIPCODE_H
#define _ZIPCODE_H
using namespace std;
class ZipCode {
private:
int zipcode;
int base[5] = { 7,4,2,1,0 }; //barcode positions
public:
ZipCode(int _zipcode); //convert zipcode to barcode
ZipCode(const char *barcode); //convert barcode to zipcode
string getBarCode(); //get barcode
int getZipCode(); //get zipcode
};
#endif
//main.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include "ZipCode.h"
#include <sstream>
using namespace std;
ofstream csis;
int main()
{
csis.open("csis.txt");
ZipCode zip1(99504);
ZipCode zip2(12345);
ZipCode zip3(67890);
ZipCode zip4("100101010011100001100110001");
ZipCode zip5("110100001011100001100010011");
ZipCode zip6("100011000110101000011100101");
cout << "Digits" << " " << "Bar Code" << endl;
cout << zip1.getZipCode() << setw(35) << zip1.getBarCode() << endl;
cout << zip2.getZipCode() << setw(35) << zip2.getBarCode() << endl;
cout << zip3.getZipCode() << setw(35) << zip3.getBarCode() << endl;
cout << endl;
cout << zip4.getZipCode() << setw(35) << zip4.getBarCode() << endl;
cout << zip5.getZipCode() << setw(35) << zip5.getBarCode() << endl;
cout << zip6.getZipCode() << setw(35) << zip6.getBarCode() << endl;
return 0;
}
您可能希望编写文件而不是std::cout
只需替换:
cout << "Digits" << " " << "Bar Code" << endl;
csis << "Digits" << " " << "Bar Code" << endl;`
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 已修改的LinkedList未在文本文件本身中更新
- C++文本文件的获取线
- 为什么C++的文件 I/O 在读取文本文件时忽略初始空行?我怎样才能让它不这样做?
- 打印逐行存储的文本文件
- C++文本文件输入