任务如下:
创建一个ASCII文本和一个C ++程序来读取它。从ASCII文本打印发货日期,部件号,名字,姓氏,公司名称。
我必须创建自己的ASCII文本。我测试了它,并且能够成功读取它。现在,我无法逐个字符地阅读并发送到阵列,以便我可以过滤掉我不想显示的数据。我将显示我的ASCII文本文件,然后是我用来阅读的代码。
04/12/11 D50625 74444 James Lehoff Rotech
04/12/11 D60752 75255 Janet Lezar Rotech
04/12/11 D40295 74477 Bill McHenry Rotech
04/12/11 D23745 74470 Diane Kaiser Rotech
04/12/11 D50892 75155 Helen Richardson NapTime
(我不知道为什么这种格式不会起作用......对不起......这对任务来说并不重要)
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
//initialize array
const int MAXROWS = 5;
const int MAXCOLS = 64;
int ship_array [MAXROWS][MAXCOLS];
int row,col;
string filename = "/Users/Jimmy/Desktop/shipped.txt";
string line;
ifstream inFile;
inFile.open(filename.c_str()); //opens ASCII file
if (inFile.fail()) //checks for successful open
{
cout << "\nThefile was not successfully opened"
<< "\n Please check that the file currently exists." << endl;
exit(1);
}
cout << "\nThe file has been successfully opened for reading.\n" << endl;
///////////code to read and output the data will go here///////////////////
cout << "Shipped Date Part Number First name Last Name Company Name";
//cycle through rows and columns//
row=0;
while(row<MAXROWS)
{
for (col=1; col<9; col++) // STORE SHIPPED DATE
{
ship_array[row][col] = get(filename.c_str());
cout << ship_array[row][col]; //DISPLAY SHIPPED DATE
}
for (col=22; col<27; col++) //STORE PART NUMBER
{
ship_array[row][col] = get(filename.c_str());
cout << ship_array[row][col]; //DISPLAY PART NUMBER
}
for (col=31; col<36; col++) // STORE FIRST NAME
{
ship_array[row][col] = get(filename.c_str());
cout << ship_array[row][col]; //DISPLAY FIRST NAME
}
for (col=39; col< 49; col++) // STORE LAST NAME
{
ship_array[row][col] = get(filename.c_str());
cout << ship_array[row][col]; //DISPLAY LAST NAME
}
for (col=51; col<65; col++) // STORE COMPANY NAME
{
ship_array[row][col] = get(filename.c_str());
cout << ship_array[row][col]; //DISPLAY COMPANY NAME
}
}
inFile.close();
return 0;
}
我知道错误是我将我的值从ASCII文本存储到我的数组的地方。我认为get()是一次读一个字符,但它不起作用。请指出我正确的方向。
修改
我改变了我的代码。我尝试了一些新的东西这给了我一些零。我甚至不打算再往下走了。我的变量和初始化是对的,我不知道为什么。
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <string>
using namespace std;
int main()
{
//initialize array
const int MAXROWS = 5;
const int MAXCOLS = 64;
int ship_array [MAXROWS][MAXCOLS];
int row,col;
//initialize input stream
string ascii_data = "/Users/Jimmy/Desktop/shipped.txt";
ifstream data;
stringstream ss(ascii_data);
data.open(ascii_data.c_str()); //opens ASCII file
if (data.fail()) //checks for successful open
{
cout << "\nThefile was not successfully opened"
<< "\n Please check that the file currently exists." << endl;
exit(1);
}
cout << "\nThe file has been successfully opened for reading.\n" << endl;
cout << "Shipped Date Part Number First name Last Name Company Name" << endl;
//cycle through rows and columns//
row=0;
while(row<MAXROWS)
{
for (col=1; col<9; col++) // STORE SHIPPED DATE
{
ss >> ship_array[0][col];
cout << ship_array[row][col];//DISPLAY SHIPPED DATE
}
for (col=22; col<27; col++) //STORE PART NUMBER
{
ss >> ship_array[row][col]; //DISPLAY PART NUMBER
}
for (col=31; col<36; col++) // STORE FIRST NAME
{
ss >> ship_array[row][col]; //DISPLAY FIRST NAME
}
for (col=39; col< 49; col++) // STORE LAST NAME
{
ss >> ship_array[row][col]; //DISPLAY LAST NAME
}
for (col=51; col<65; col++) // STORE COMPANY NAME
{
ss >> ship_array[row][col]; //DISPLAY COMPANY NAME
}
row++;
}
data.close();
return 0;
}