c Problems with using getline to read files

Updated on technology 2024-02-28
15 answers
  1. Anonymous users2024-02-06

    Note that the getline function uses a carriage return as a terminator, but it does not take a carriage return into account. in the first, 5); At the end of the end, a carriage return is kept in the cache when executed to the second, 5); The function reads the carriage return that is kept in the cache, and thinks that the read has ended, so the second string of five characters entered after that is not read into ch, of course, ch is empty, and can be used; Solve this problem; Here's how:

    #include

    using namespace std;

    int main()

    char ch[20];

    cout<<"enter a sentence:"<,5);

    cout<<"part 1:"<,5);

    cout<<"part 2:"in, 5); added later; You can read the extra carriage return then.

    below, 5); can read correctly.

  2. Anonymous users2024-02-05

    I tried it, and it should be the first time to enter less than 4 characters.

    getline(char *ch,int n,char=''The meaning of the function is to give the pointer ch n characters and end the input when there is a carriage return without n characters.

    When you enter 4 characters for the first time and press enter, then move on to the next one, 5); , read the return into the ch and end the input, and it seems that the program ends running without waiting for you to enter the character a second time.

    You can try to enter the following program into 4 characters, and run it in a single step, and look at the value of ch.

    #include

    #include

    int main()

    char ch[20];

    cout<<"enter a sentence:"<,5,'');

    cout<<"part 1:"cout<<"part 2:"

  3. Anonymous users2024-02-04

    Because getline has three parameters, the third parameter is the terminator of the string, that is, when getline encounters this terminator, it will not accept input, for example, it is written as getline(cin,st,'s');Even if abcsabc is entered, then only abc will be displayed, so for the case above, getline starts with a carriage return by default ('') as a terminator, pressing enter the first time indicates the end of the string, and pressing enter the second time to start the output.

    You can release this carriage return by reading char more than once. This is explained in detail in C++ Programming Ideas, which you can look for.

  4. Anonymous users2024-02-03

    Because you type the whole sentence before you execute the first one, 5);

    When the cout << is executed"part 1:""You have to wait for the input, you need to enter it twice.

  5. Anonymous users2024-02-02

    It is good to enter less than or equal to 4 characters at a time.

  6. Anonymous users2024-02-01

    Because the file pointer is already at the back.

  7. Anonymous users2024-01-31

    This may be because the file pointer has gone to the back...

  8. Anonymous users2024-01-30

    There are essentially two GetLine functions in C++, one in the header file, which is a member function of the iStream class. One in the header file, which is a normal function.

    1. There are two overloaded forms of the getline function in

    istream& getline (char* s, streamsize n );

    istream& getline (char* s, streamsize n, char delim );

    The function is to read up to n characters from istream and save them in the array corresponding to s. Even if you haven't read enough n characters, if you encounter a line break''(first form) or delim (second form), then the read terminates''or delim will not be stored in the array corresponding to s.

    2. There are four overloaded forms of the getline function in

    istream& getline (istream& is, string& str, char delim);istream& getline (istream&& is,string& str, char delim);istream& getline (istream& is, string& str);

    istream& getline (istream&& is, string& str);

    The usage is similar to the previous one, but the istream to be read is passed in as a parameter is. The read string is stored in a str of type string.

  9. Anonymous users2024-01-29

    char s[1000];A number in parentheses less than 1000 may be wrong, but not necessarily wrong, for reasons mentioned above).

    or char* s=new char[1000];

    or char a[1000]; char * s=a;

    or char a[1000]; char *s=&a[0];

  10. Anonymous users2024-01-28

    getline

    Syntax: The getline() function is used to input the stream, reading characters into the buffer until the following happens:

    num - 1 character has been read in, touched by a line break flag, touched by an eof, or, arbitrarily read in until the read character character character will not be put into the buffer.

  11. Anonymous users2024-01-27

    GetLine function getline is a member function of a String class object, which belongs to the String IO operation. Used to read entire lines of text.

    getline parameter getline(cin,str); cin: input stream str: string object.

    GetLine indicates that the getline function reads from the next line of the input stream and saves the read content to str, but does not include line breaks. The getline function takes the istream parameter as the return value, and uses it as a judgment condition just like the input operator. Example:

    #include #include using namespace std;int main() result: Enter line1 -- enter the line return and show empty enter line2 --enter the line entry line 1 enter line3 --enter the line 2 -- and so on.

  12. Anonymous users2024-01-26

    istream& getline ( istream& ,string& )

    They all read the characters of the input stream into the string, the top one ends with the terminator you set, and the bottom one ends with a line break''As an end. If you read an EOF or encounter an error, it will also end.

  13. Anonymous users2024-01-25

    GetLine is an input function in C++ that reads entire rows of data from standard input and saves them as an array of characters (char* or cha).

    There are two types of overloads for getline:

    1、 istream& getline(char* s, int n);

    This is done by reading the entire line of data from the input and ending the input when a line break is encountered. If, during the input process, the actual number of characters entered exceeds the value of the parameter n, the input will also be terminated. The input result is stored in the space pointed to by the character pointer S.

    2、istream& getline(char* s, int n, char delim);

    This overload has one more character delim than in 1, and its function is to set the ending character.

    When reading in, the input ends when the character is equal to the delim value. Except for that, the others are the same as in 1.

    i.e., getline(s,n);

    Equivalent to getline(s,n,'');

  14. Anonymous users2024-01-24

    Get the data of the open file in a row.

  15. Anonymous users2024-01-23

    In C++, the GetLine function is used to read an entire row of data from the standard input stream Cin. That is, by default, getline does not read which file it reads, but the keyboard input in the command line.

    To read a file using getline, the first thing to do is to redirect CIN to the file you want to read. This can be achieved with the following **.

    streambuf *backup;

    ifstream fin;

    );Open the file you want to read.

    backup = ;Back up the buffer of the CIN itself.

    Redirect the buffer of the cin to the file.

    Perform the required read operations, including getline.

    Restore the original CIN buffer.

    After execution, you can use getline to read the entire line of data from the file.

    GetLine can be called in the following way:

    getline(char

    buf,int

    size);

    If the data length exceeds the length specified by size, only the size byte data is read.

Related questions
6 answers2024-02-28

Don't understand what you mean by startmark and enterstumark. >>>More

5 answers2024-02-28

It's best not to move the things of the C drive, and don't mess with things (don't install other software into it) It's better to reinstall the system and redivide the disk, it is conceivable that your C drive is too small, which will affect the operation performance and even speed of the system! If that's right, your C drive is about 10g. It's best to divide it a little more according to the size of the hard drive, maybe you didn't have one-click restore installed at all, or maybe you didn't make a backup. >>>More

5 answers2024-02-28

System documents generally include: manuals, program documents, records, operating procedures, rules and regulations, and job responsibilities. The system documents are generally divided into three or four levels, and the laboratory can be specified according to its own monitoring needs and habits. >>>More

8 answers2024-02-28

Tool: ES File Explorer.

Steps: 1. Open the ES file browser, touch the menu button of the phone, find the remote manager in the tool and click it. >>>More

10 answers2024-02-28

The data connection is to use the mobile phone as a USB flash drive, and the offline mode of the mobile phone must be used to use this mode! I also use 6120c, and my phone's dictionary works fine! Are you not using the memory card originally sent by the mobile phone, and replaced it with a new card, right, the dictionary data is stored on the original attached card, you need to use the new card, the new card is re-checked, and then on the computer, copy the files on the randomly attached card, including folders, to the new card, and then plug in the mobile phone to use, I believe that your dictionary can be used! >>>More