我想把一个读入数据并处理,然后存储数据的代码变成可执行文件.exe的形式。要求是在.exe执行时,输入需要读取和存入的文件位置,然后执行,平台为VS2010,c++。代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<fstream>
#include<sstream>
#include <complex>
#include <string.h>
#include <iomanip>
using namespace std;
typedef struct record
{
int serial_number;
char code_name[128];
char name[128];
int number;
char material[128];
float single_weight;
float total_weight;
char material_code[128];
char remark[128];
}record;
#define MAX_LINE 1024
static record records_[MAX_LINE];
int main()
{
for(int j=0;j<MAX_LINE;j++)
{
char line[1024];
int ret,i,count;
string str1;
str1="D:\\zhao\\0725\\处理前\\";
ostringstream out;
//out.setf(ios::showpoint);
out<<str1<<j<<".txt";
str1 = out.str();
out.str("");
const char* A = str1.data();
FILE* f = fopen(A, "rb");
if(!f)
continue;
else
{
cout<<" "<<'\n';
cout<<"读取"<<str1<<'\n';
}
for (i = 0; i < MAX_LINE; i++)
{
if(!fgets(line, 1024, f))
break;
ret = sscanf(line, "%d\t%[^\t]\t%[^\t]\t%d\t%[^\t]\t%f\t%f\t%[^\t]\t%s", &records_[i].serial_number,
records_[i].code_name, records_[i].name, &records_[i].number, records_[i].material,
&records_[i].single_weight, &records_[i].total_weight, records_[i].material_code, records_[i].remark);
//if(ret == 8)records_[i].remark[0] = 0;
//else if(ret != 9)break;
if(records_[i].serial_number == 0)
break;
}
fclose(f);
count = i;
printf(" line count %d\n", count);
/*for (i = 0; i < count; i++)
{
cout<<"i="<<i<<'\t'<<records_[i].serial_number<<'\t'<<records_[i].remark<<'\n';
} */
string str2;
str2="D:\\zhao\\0725\\处理后\\";
ostringstream out1;
//out.setf(ios::showpoint);
out1<<str2<<"明细表"<<j<<".csv";
str2 = out1.str();
out1.str("");
ofstream of1(str2);
//of.setf(ios::fixed);
if(!of1)
{
cout<<"不能存储文件"<<" "<<str2<<'\n';
cin.get();
}
if(!f)
{
continue;
}
else
{
cout<<" "<<'\n';
cout<<"存储 "<<str2<<'\n';
of1<<"序号"<<","<<"物料码"<<","<<"名称"<<","<<"代号"<<","<<"材料"<<","<<"数量"<<","<<" "<<","<<"备注"<<'\n';
for(i>0;i--;)//nnp:particle name//
{
of1<<records_[i].serial_number<<","<<records_[i].material_code<<","<<records_[i].name<<","<<records_[i].code_name
<<","<<records_[i].material<<","<<records_[i].number<<","<<" "<<","<<records_[i].remark<<'\n';
}
}
}
cout<<"Complete the calcula"<<'\n';
system("pause");
return 0;
}
也就是说在.exe可执行文件中首先输入37行代码“str1”的内容,即:
然后输入第73行代码“str2”的内容,即:
用于测试代码的文件如下链接,提取码1234。在我自己电脑上是成功编译的,请注意VS2010 C++。