从中提取字符后重新读取我的文件

时间:2017-05-11 00:29:58

标签: c++ file dynamic

我知道这是一个广泛问的问题,甚至在这个网站上也有答案,不幸的是,没有一个答案帮助我解决了我的问题,如下所示:我打开一个文件并循环其字符以查看有多少行我将需要初始化我的动态数组,然后我有另一个循环重新读取文件,发现我有多少列(我知道这个效率如此高效),然后我将这些字符输入到我的2d动态数组。我们可以看到这里我重读了几次文件。我已经尝试过像seekg(),rewind()这样的命令,关闭文件并重新打开它没有用过,这是我的代码:(我们已经提示每行以值0结束,不应该计算。)非常感谢任何帮助。

//------------- include section -------------
  #include <iostream>
  #include <iomanip>
  #include <cstdlib>
  #include <new>
  #include <fstream>
  #include <cstring>
  #include <cctype>
//------------- using section -------------
 using std::cin;
 using std::cout;
 using std::cerr;
 using std::endl;
 using std::ifstream;
 using std::ofstream;
 using std::setw;
//------------- const section -------------
const int END_OF_LINE=-1;
const int LEN=100;
//------------- prototypes section -------------
void open_files(ifstream& if1, ifstream& if2);
void convert_to_matrix (ifstream &inp,int*matrix[],int num_of_rows);
void add_2_matrixes (int*matrix[],int* matrix2[],int num_of_rows,int 
num_of_rows2);
int get_length(ifstream & inp,int &length);
int get_rows(ifstream &inp, int &counter);
//------------- main -------------
 int main(int argc, char *argv[])
 {
   ifstream inp;
   ifstream inp2;
   open_files(inp,inp2);

    int num_of_rows=0,counter=0;
     num_of_rows=get_rows(inp,counter);
    int* matrix[num_of_rows];
    //fill rows and cols dynamically
    convert_to_matrix(inp,matrix,num_of_rows);
    int num_of_rows2=0;
    counter=0;
    num_of_rows2=get_rows(inp2,counter);
    //another matrix
    int* matrix1[num_of_rows2];
    //fill rows and cols dynamically
    convert_to_matrix(inp2,matrix1,num_of_rows2);
    add_2_matrixes(matrix,matrix1,num_of_rows,num_of_rows2);

    return EXIT_SUCCESS;

 }
//-----------------------------
void open_files(ifstream& inp, ifstream& inp2){

    char* st_file;
    char* nd_file;
    st_file=new(std::nothrow) char [LEN];
    nd_file=new(std::nothrow) char [LEN];
    //gets the first file name
    cin >> setw(LEN) >> st_file;

    inp.open(st_file);
    //gets the second file name
    cin >> setw(LEN) >> nd_file;

    inp2.open(nd_file);

    // if the opening command failed
    if ((!(inp.is_open())) || (!(inp2.is_open()))) {

        exit(EXIT_FAILURE);

    }
  }
 //------------------------------------------------
int get_rows(ifstream &inp, int &counter){
  int columns=0;
 while(!inp.eof())
 {
    counter++;
    inp>>columns;
    while (columns!=0)
    {
        inp>>columns;
    }

  }
   //!!!! (problem)
 inp.seekg(0,std::ios::beg);
 return counter;
 }
 //---------------------------------------------
  void convert_to_matrix (ifstream &inp,int* matrix[],int num_of_rows){
   //numbers is how many cols
   int length=0;
   //value of each col
   int number;

   for (int row=0;row<num_of_rows;row++){
      //open an array
      get_length(inp,length);
      matrix[row]=new(std::nothrow) int [length+1];
      //if null we failed
       if (matrix[row]==NULL)
       {
        cerr<<"Cannot allocate memory /n";
        exit(EXIT_FAILURE);
     }
     //else start filling in values
     for (number=0;matrix[row][number]!=0;number++)
     {
        inp>>matrix[row][number];
     }
     //at end of filling values last number is -1
     //to help us know when to stop
     matrix[row][number]=END_OF_LINE;
    }
}
//------------------------------------------------------
int get_length(ifstream &inp,int &length){
  int columns=0;
   inp>>columns;
   length++;
   while(columns!=0)
   {
    inp>>columns;
    length++;
   }
     //!!!!!(problem)
     inp.seekg(0,std::ios::beg);
     return length;
   }

0 个答案:

没有答案