How to identify a new line in C++?

前端 未结 4 662
猫巷女王i
猫巷女王i 2021-01-25 05:14

I have to take integer input to an integer array. I have to identify the newline also in input. To be more clear, I have an example. The input I am giving is:-

2         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-25 06:00

    though you mention c++ here is something that i often use to read in an array of doubles from a file

     char line[512+1];
    
      unsigned int row = 0 ;
      unsigned int col = 0 ;
    
      while( fgets(line, sizeof(line), file) )
        {
          col = 0 ;
          tempChr = strtok(line, delimters);
    
          // ignore blank lines and new lines
          if ( tempChr[0] == '\n' )
        {
          continue ;
        }
    
          tempDbl = atof(tempChr);
    
          data[row*numCols+col] = tempDbl ;
    
    
          for(col = 1 ; col < numCols ; ++col)
        {
          tempDbl = atof(strtok(NULL, delimters));
          data[row*numCols+col] = tempDbl;
        }
    
          row = row + 1;
          if( row == numRows )
        {
          break ;
        }
        }
    

提交回复
热议问题