【问题标题】:How to read multiple lines in c++ [duplicate]如何在c ++中读取多行[重复]
【发布时间】:2023-03-19 11:44:02
【问题描述】:

我想在 c++ 中一口气读完很多行。我知道 getline() 方法,但它完全读了一行。这是我想做的事情。一口气读完整件事

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

PS:- 当我使用 getline() 方法时,它只读取第一行,我得到的字符串长度为 50,但我想要这 1000 个字符的整个字符串。有什么帮助吗?

【问题讨论】:

    标签: c++ string


    【解决方案1】:

    如果您知道每行的字符数,以及要读取的行数,则可以使用freadistream::read 读取特定数量的字符。

    请记住行尾。

    【讨论】:

    • 好的,我同意,这对这个问题有好处。但我问的是一般情况
    • 一般情况是什么,如果您想将完整的文件读入字符串,您可以使用此答案中描述的方法:stackoverflow.com/a/2602060/1784021
    • 一般情况意味着如果我不知道。每行的字符数
    • 我上面链接的答案将为您解决这个问题。读取特定数量的未知长度的行,并将它们全部放在一个字符串中。只需使用 getline() 并将每一行附加到您的最终字符串。
    【解决方案2】:
    1. 使用 ifstream 方法获取文件 N 的大小。

      myfile.seekg (0, ios::end);
      int N = myfile.tellg();
      
    2. 读取 N 个字节。

    【讨论】:

      【解决方案3】:

      当您想读取这 1000 个字符的字符串时,请使用 read(buffer,size) 一口气。

      #include <iostream.h>
      #include <fstream.h>
      #include <stdlib.h>
      #include <string.h>
      int main () 
      {
      
             char buffer[1000];
             long size;
      
             ifstream infile ("numbers.txt",ifstream::binary);
      
             // get size of file
             infile.seekg(0,ifstream::end);
             size=infile.tellg();
             infile.seekg(0);
      
             //reset buffer to ' '
             memset(buffer,32,sizeof(buffer ));
      
             // read file content into buffer
             infile.read (buffer,size);
      
             // display buffer
              cout<<buffer<<"\n\n";
      
             infile.close();    
      
      
      
        cout<<" \nPress any key to continue\n";
        cin.ignore();
        cin.get();
      
      
        return 0;
      }
      

      输出:

      73167176531330624919225119674426574742355349194934
      96983520312774506326239578318016984801869478851843
      85861560789112949495459501737958331952853208805511
      12540698747158523863050715693290963295227443043557
      66896648950445244523161731856403098711121722383113
      62229893423380308135336276614282806444486645238749
      30358907296290491560440772390713810515859307960866
      70172427121883998797908792274921901699720888093776
      65727333001053367881220235421809751254540594752243
      52584907711670556013604839586446706324415722155397
      53697817977846174064955149290862569321978468622482
      83972241375657056057490261407972968652414535100474
      82166370484403199890008895243450658541227588666881
      16427171479924442928230863465674813919123162824586
      17866458359124566529476545682848912883142607690042
      24219022671055626321111109370544217506941658960408
      07198403850962455444362981230987879927244284909188
      84580156166097919133875499200524063689912560717606
      05886116467109405077541002256983155200055935729725
      71636269561882670428252483600823257530420752963450
      
      
      Press any key to continue
      

      【讨论】:

        猜你喜欢
        • 2016-10-17
        • 1970-01-01
        • 1970-01-01
        • 2015-11-23
        • 2021-05-10
        • 2017-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多