Use ifstream and getline To Read File Content [c ++]

Source: Internet
Author: User

Suppose there isData.txtFile, which contains the following content:

Fry: One Jillion dollars.
[Everyone gasps.]
Auctioneer: Sir, that's not a number.
Read and test data.



The following is the data read operation based on data.txt:

# Include <iostream>
# Include <fstream>
# Include <string>

Using namespace std;

// Empty output line
Void OutPutAnEmptyLine ()
{
Cout <"\ n ";
}

// Read method: Read by word, separated by Spaces
// Read data from the file, WOrd BY WOrd
// When used in this manner, we'll get space-delimited bits of text from the file
// But all of the whitespace that separated words (including newlines) was lost.
Void ReadDataFromFileWBW ()
{
Ifstream fin ("data.txt ");
String s;
While (fin> s)
{
Cout <"Read from file:" <s <endl;
}
}

// Read method: Read rows by row, read rows into character arrays, and use carriage return to line feed to distinguish between rows
// If we were interested in preserving whitespace,
// We cocould read the file in LIne- BY- LIne using the I/O getline () function.
Void ReadDataFromFileLBLIntoCharArray ()
{
Ifstream fin ("data.txt ");
Const int LINE_LENGTH = 100;
Char str [LINE_LENGTH];
While (fin. getline (str, LINE_LENGTH ))
{
Cout <"Read from file:" <str <endl;
}
}

// Read method: Read rows by line, read rows into strings, and use carriage return to line feed to distinguish between rows
// If you want to avoid reading into character arrays,
// You can use the C ++ string getline () function to read lines into strings
Void ReadDataFromFileLBLIntoString ()
{
Ifstream fin ("data.txt ");
String s;
While (getline (fin, s ))
{
Cout <"Read from file:" <s <endl;
}
}

// Read method with Error Detection
// Simply evaluating an I/O object in a boolean context will return false
// If any errors have occurred
Void ReadDataWithErrChecking ()
{
String filename = "dataFUNNY.txt ";
Ifstream fin (filename. c_str ());
If (! Fin)
{
Cout <"Error opening" <filename <"for input" <endl;
Exit (-1 );
}
}

Int main ()
{
ReadDataFromFileWBW (); // read strings one by one
OutPutAnEmptyLine (); // empty output line

ReadDataFromFileLBLIntoCharArray (); // read the character array one by one
OutPutAnEmptyLine (); // empty output line

ReadDataFromFileLBLIntoString (); // read strings one by one
OutPutAnEmptyLine (); // empty output line

ReadDataWithErrChecking (); // read with detection
Return 0;
}



Output result:
Read from file: Fry:
Read from file: One
Read from file: Jillion
Read from file: dollars.
Read from file: [Everyone
Read from file: gasps.]
Read from file: Auctioneer:
Read from file: Sir,
Read from file: that's
Read from file: not
Read from file:
Read from file: number.
Read from file: reads data,
Read from file: Test
Read from file :.

Read from file: Fry: One Jillion dollars.
Read from file: [Everyone gasps.]
Read from file: Auctioneer: Sir, that's not a number.
Read from file: Read data, test.

Read from file: Fry: One Jillion dollars.
Read from file: [Everyone gasps.]
Read from file: Auctioneer: Sir, that's not a number.
Read from file: Read data, test.

Error opening dataFUNNY.txt for input
Press any key to continue

From: http://www.cnblogs.com/JCSU/articles/1190685.html

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.