开发者

C++ length of file and vectors

开发者 https://www.devze.com 2022-12-15 17:52 出处:网络
Hi I have a file with some text in it. Is there some easy way to get the numb开发者_开发问答er of lines in the file without traversing through the file?

Hi I have a file with some text in it. Is there some easy way to get the numb开发者_开发问答er of lines in the file without traversing through the file?

I also need to put the lines of the file into a vector. I am new to C++ but I think vector is like ArrayList in java so I wanted to use a vector and insert things into it. So how would I do it?

Thanks.


There is no way of finding the number of lines in a file without reading it. To read all lines:

1) create a std::vector of std::string

3 ) open a file for input

3) read a line as a std::string using getline()

4) if the read failed, stop

5) push the line into the vector

6) goto 3


You would need to traverse the file to detect the number of lines (or at least call a library method that traverse the file).

Here is a sample code for parsing text file, assuming that you pass the file name as an argument, by using the getline method:

#include <string>
#include <vector>
#include <fstream>
#include <iostream>

int main(int argc, char* argv[])
{
  std::vector<std::string> lines;
  std::string line;

  lines.clear();

  // open the desired file for reading
  std::ifstream infile (argv[1], std::ios_base::in);

  // read each file individually (watch out for Windows new lines)    
  while (getline(infile, line, '\n'))
  {
    // add line to vector
    lines.push_back (line);
  }
  // do anything you like with the vector.  Output the size for example:
  std::cout << "Read " << lines.size() << " lines.\n";

  return 0;
}

Update: The code could fail for many reasons (e.g. file not found, concurrent modifications to file, permission issues, etc). I'm leaving that as an exercise to the user.


1) No way to find number of lines without reading the file.

2) Take a look at getline function from the C++ Standard Library. Something like:

string line;
fstream file;
vector <string> vec;
...   
while (getline(file, line)) vec.push_back(line);


Traversing the file is fundamentally required to determine the number of lines, regardless of whether you do it or some library routine does it. New lines are just another character, and the file must be scanned one character at a time in its entirety to count them.

Since you have to read the lines into a vector anyways, you might as well combine the two steps:

// Read lines from input stream in into vector out
// Return the number of lines read
int getlines(std::vector<std::string>& out, std::istream& in == std::cin) {
  out.clear(); // remove any data in vector
  std::string buffer;
  while (std::getline(in, buffer))
    out.push_back(buffer);

  // return number of lines read
  return out.size();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消