我有一個文件,每行一個記錄(名稱,名稱,ID號,等級,等級,等級,等級,等級,等級) I必須根據64位或更少的數字進行驗證:名稱,ID:9位和0 <等級< 100. 我想存儲在tempArray [9]中,然後存儲在realArray [9] [200]中。我認爲我的主要問題是我嘗試存儲時。數據沒有從tempArray []存儲到realArray [] []
tempArray在std::cerr << tempArray[i] <<std::endl;
幾乎無處不在測試,它包含正確的數據。
但是realArray也進行了測試,只包含第一條記錄。我將realArray傳遞給以下函數,以便當我到達storeData時,可以將tempArray傳遞給realArray,並根據lineNumber使用列標記。
我知道有錯誤和編程「不要做的」大概TONNS,但我需要知道
1)如果我想要做的可以做 2)爲什麼我的realArray是隻獲得第一個記錄。
/添加後:我知道它沒有被存儲在realArray中,因爲我的增量變量是const int
。但是爲什麼它接受存儲第一條記錄對我來說沒有意義。是否因爲行號被初始化爲零? 如果storeData第一次接受lineeNumber,爲什麼第二次不接受呢?/
代碼:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include "A4prototypes.h"
#include "search.h"
#include "sort.h"
using namespace std;
void getFile(std::string realArray[][200], const int ROWS)
{
std::string filename, line, token;
int row(0);
int lineNumber(0);
const int MAX_RECORDS (200);
const int TEMP_ROWS(9);
const int ZERO(0);
std::string tempArray[TEMP_ROWS];
std::cout << "Please enter the desired filename with it's extension:\t ";
std::cin >> filename;
std::ifstream input(filename.c_str(), std::ios::in);
if (input.is_open())
{
getline (input,line);
while (input.good() && lineNumber < MAX_RECORDS)
{
std::istringstream inputss (line);
while (getline(inputss, token, ',') && row < ROWS)
{
tempArray[row] = token;
row++;
}
row = ZERO;
/*I know I don't need to send the rows size of both of these arrays, but ... */
validateData (lineNumber, tempArray, TEMP_ROWS, realArray, ROWS);
lineNumber++;
getline (input,line);
}
}
else
{
std::cout << "The file did not open correctly. \n\nPlease enter a valid filename.\n";
}
if (lineNumber == MAX_RECORDS)
{
std::cout << "The maximum number of records to be read (" << MAX_RECORDS << ") has been reached.\n";
}
}
void validateData (int lineNumber, std::string tempArray[], const int ROW, std::string realArray[][200], const int ROWS)
{
int j(0);
//Validate Data functions...
// Pass tempArray and realArray along with lineNumber to update realArray.
storeData(lineNumber, tempArray, ROW, realArray ,ROWS);
}
int storeData(int record, std::string tempArray[], const int ROWS, std::string realArray[][200], const int ROW_SIZE)
{
int k(0);
std::string tempstr;
record-=1;
for (k; k < ROWS; k++)
{
tempstr = tempArray[k].data();
realArray[k][record]=tempstr;
}
return 0;
}
int main()
{
/* There should be a pointer here that gets sent to getFile and incriminates with the record line, gets sent to store data and the
rest instead of just lineNumber,????...*/
int i(0), j(0);
const int ROWS(9);
const int COLUMNS(200);
/* int * const rows = &ROWS; => It says in the book you can do this and pointer isn't const, but you could do *rows =10, which is what I want to
do with the column, but it wont work... */
std::string realArray[ROWS][COLUMNS]={}; // Declare array for storing the data once it's been validated so I don't keep unecessary data.
// Pass realArray to getFile so I can have access to it from main but it can be changed by getFile was the plan so fn's dont have to all be related to main.
getFile(realArray,ROWS);
return 0;
}
而這裏的頭文件
#ifndef _h
#define _h
void getFile(std::string [][200], const int);
void validateData (int,std::string [], const int, std::string [][200], const int);
int storeData(int, std::string [], const int, std::string [][200], const int);
#endif
@Natlie:Yikes ...你不知道爲什麼頭文件是必要的,但你試圖讓一個不平凡的文件數據存儲程序運行?嗯...也許你應該重新考慮你選擇的學習材料。 (頭文件提供了函數和變量聲明,所以編譯器知道,例如'validateNames'是一個需要三個'int'作爲參數並返回一個'int'本身的函數。* *該函數在第一次調用之前。) – DevSolar 2011-04-04 07:08:46
否, 我知道我知道 。 。 。太久了!那對不起,看起來很遲鈍。我實際上比我想象的要多得多。 。 。隨你。 PS這是土木工程必修課......我是唯一一個真正想要做到這一點的罪人之一......我正在努力! @其餘的,我編輯了代碼,拿出了無用的fn'並放入我的修改過的東西。 PPS還有一個用while循環獲取有效文件名的概率。如果輸入無效,循環不會停止!就好像ifstream不會將文件名更改爲新的輸入...? – Natlie 2011-04-04 07:31:14
嗨,嗨,我想我們的老師試圖讓我們爲他找到我們自己的成績...... – Natlie 2011-04-04 07:36:45