Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从文件中提取字符后重新读取文件_C++_File_Dynamic - Fatal编程技术网

C++ 从文件中提取字符后重新读取文件

C++ 从文件中提取字符后重新读取文件,c++,file,dynamic,C++,File,Dynamic,我知道这是一个被广泛询问的问题,甚至在这个网站上也有答案,不幸的是,没有一个答案能帮助我解决以下问题:我打开一个文件并循环它的字符,看看初始化动态数组需要多少行,然后我有另一个循环来重新读取文件,找出我有多少列(我知道这个指令非常有效),然后我将把这些字符输入到我的2d动态数组中。我们可以看到这里我重读了几次文件。我曾尝试过seekg()、revind()、关闭文件并重新打开文件等命令。这些命令都不起作用,下面是我的代码:(我们一直在提示,每一行都以值0结尾,不应计算在内。)非常感谢您的帮助 /

我知道这是一个被广泛询问的问题,甚至在这个网站上也有答案,不幸的是,没有一个答案能帮助我解决以下问题:我打开一个文件并循环它的字符,看看初始化动态数组需要多少行,然后我有另一个循环来重新读取文件,找出我有多少列(我知道这个指令非常有效),然后我将把这些字符输入到我的2d动态数组中。我们可以看到这里我重读了几次文件。我曾尝试过seekg()、revind()、关闭文件并重新打开文件等命令。这些命令都不起作用,下面是我的代码:(我们一直在提示,每一行都以值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;
   }
/-----------包括部分-------------
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//-------------使用部分-------------
使用std::cin;
使用std::cout;
使用std::cerr;
使用std::endl;
使用std::ifstream;
使用std::of流;
使用std::setw;
//-------------常数段-------------
const int END_OF_LINE=-1;
常数int LEN=100;
//-------------原型组-------------
作废打开的_文件(ifstream&if1、ifstream&if2);
void convert_to_矩阵(ifstream&inp,int*矩阵[],int num_行);
void add_2_矩阵(int*matrix[],int*matrix2[],int num_行数,int
行数2);
int get_length(ifstream&inp,int&length);
int获取_行(ifstream&inp,int&counter);
//-------------主要-------------
int main(int argc,char*argv[])
{
ifp;
if2;
打开_文件(inp、inp2);
int num_of_行=0,计数器=0;
行数=获取行数(inp,计数器);
int*矩阵[行数];
//动态填充行和列
将_转换为_矩阵(inp,矩阵,_行的数量);
int num_of_rows2=0;
计数器=0;
num_of_rows2=获取_行(inp2,计数器);
//另一个矩阵
int*matrix1[行数2];
//动态填充行和列
将_转换为_矩阵(inp2,matrix1,第2行的num_);
添加2个矩阵(矩阵,矩阵1,行数,行数2);
返回退出成功;
}
//-----------------------------
作废打开的_文件(ifstream&inp、ifstream&inp2){
char*st_文件;
char*nd_文件;
st_file=new(std::nothrow)char[LEN];
nd_file=new(std::nothrow)char[LEN];
//获取第一个文件名
cin>>setw(LEN)>>st_文件;
输入打开(st_文件);
//获取第二个文件名
cin>>setw(LEN)>>nd_文件;
inp2.打开(nd_文件);
//如果打开命令失败
如果(!(inp.is_open())| |(!(inp2.is_open())){
退出(退出失败);
}
}
//------------------------------------------------
int get_行(ifstream&inp,int&counter){
int列=0;
而(!inp.eof())
{
计数器++;
inp>>列;
while(列!=0)
{
inp>>列;
}
}
//!!(问题)
输入seekg(0,标准::ios::beg);
返回计数器;
}
//---------------------------------------------
void convert_to_矩阵(ifstream&inp,int*矩阵[],int num_行){
//数字是多少列
整数长度=0;
//每列的值
整数;
对于(int row=0;row>columns;
长度++;
while(列!=0)
{
inp>>列;
长度++;
}
//!!!!!!!(问题)
输入seekg(0,标准::ios::beg);
返回长度;
}

请阅读。请记住,当到达文件末尾时,流处于“不好”状态。在重新定位或再次读取之前,您需要清除或重置任何负面条件。