C ++ - Does getline have the maximum length of a string or character?

My question relates to the assignment I'm working on. There seem to be several ways to get closer to the assignment.

The program I am writing will be a filter for text files. The goal of the assignment is to gain experience with fstream and getline.

Requirements:

  • Read one text file (in any way possible, not necessarily all at once)
  • Writing to a separate text file (in any way possible, adding or writing character by character)
  • It is assumed that each offer ends for a period.
  • The first letter of each sentence must be uppercase.
  • Everything except the first letter of each sentence must be lowercase. (nouns too are a trivial example)

I have a working draft of a program that I wrote, but getline does not match the way it reads my text file. Basically, he will read one line as a line, which is what I want. As he reads in the second line; however, the program throws a run-time error halfway along the line, and Windows disables it.

Does getline have a buffer that fills up and needs to be cleared after each line?

My pseudo code for the program:

  • Use getline to read in a row from line x, stopping over a period (.).
  • Iterate over character strings, the top of the first letter, and then the bottom of the rest.
  • Read on another line that continues after the last period (.) In the text file.
  • Repeat until the text file is read.
  • Writing to a second text file.

I implement getline as follows:

getline(fileIN, str1, '.') 

str1 is a line that is read from each line.

Am I using getline correctly? Do I think this problem is correct and effective?

* I realized how I ended this extended question / section that getline can use more memory for the characters "\ r" or "\ n" at the ends of lines or for reasons not related to memory, getline is wrong (according to my goals ) sentences that wrap on new lines. Does getline really not handle sentence / hyphenation?

Also, is there a way to dynamically tell getline to read the first line before the period (.) Or new line ('\ n') that FIRST ever appears?

Thank you for your time and attention.

+4
source share
2 answers

Yes, you are using getline correctly. Be sure to use it as a condition for a while or other conditional:

 while(std::getline(fileIN, str1, '.')) { // process str1 } 

and don’t make the mistake that many others tried to use fileIN.good() or !fileIN.eof() or anything else (this will only lead to headaches and heartaches).

The str1 buffer does not need to be flushed from you, since it is controlled by the string class. It will expand as needed and free itself when the variable goes beyond. This is one of the reasons why we love the standard library classes and think twice before using raw arrays.

In addition, there are no artificial restrictions on the capacity of a string . The only limiting factor is the available system memory, so with virtual memory it can be slightly less than 4 GB on a 32-bit system or slightly less than 2 64 bytes on a 64-bit system.

+6
source

The getline limit (from the C library) is limited by resources (i.e. memory), so you should not worry so much (unless you have a file with one line in a billion bytes).

STL C ++ getline has similar limitations.

That way, you probably don't need that much if you don't want to make a bulletproof program (and then you might be interested in other things, such as I / O errors, the full state of the disk, ...).

+1
source

Source: https://habr.com/ru/post/1385701/


All Articles