How the program reads the file-by character, by line, by word

Source: Internet
Author: User

How the program reads the file--one character for reading

 #include <iostream> #include  <fstream> using  namespace   STD;  int   main () { char      ch; FStream fp (  " a.txt  "  );  while  (! if  (Fp.get   <<CH;    } fp.close ();  return  0  ;}  
The way the program reads the file--read by line
#include <iostream>#include<fstream>using namespacestd;intMain () {Charline[ -]; FStream FP ("a.txt");  while(!fp.eof ()) {Fp.getline (line, -); cout<<line<<Endl;    } fp.close (); return 0;}

How the program reads a file--a word that reads a word

#include <iostream>#include<fstream>using namespacestd;intMain () {Charstr[ -];//string str;FStream FP ("a.txt");  while(!fp.eof ()) {FP>>str; cout<<str;    } fp.close (); return 0;}

It is recommended to use line-by-row reading, or a character-by-word reading method. Word-by-phrase reading is not a good solution because it does not read a new line of information,
So if you have a new line in your file, it will not display those new lines, but instead add them to the text that has already been printed. and using Getline () or
Get () will show you the true nature of the file.

How the program reads the file-by character, by line, by word

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.