简体   繁体   中英

How do I read a file in C++?

I am having some problems reading from a file. I have two header files: a and b. b is derived from a,and c is derived from b. Now i want to open a text file.

The whole format is like this:

A john

A johnee

P 123

If the first charcter is 'a' and 'p' is also there, then print the second line, else print the first line.

#include "c.h"
#include <iostream>
# include <fstream>
using namespace std;
c :: c()
{
    ifstream input;
    input.open ("abc.txt");
    ch = input.get();
    input >> ch;
    if (ch ='A')
        a* z =new a();
    else 
    input.close();
}

Can anyone give me some advice on how to accomplish this?

If the first charcter is 'a' and 'p' is also there, then print the second line, else print the first line.

Could you give an example of what this program's output should look like based on the text file you have?

I did notice one thing, though:

if (ch ='A')

Change that to:

if (ch =='A')

You need to use two =s for comparisons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM