Files - Skipping lines

Hello! I'm attempting to read the first letter of every line, and I'm not overly sure as to how I'd get to that point. I've been able to read the first letter of the first line, but I don't know how to go about getting to the next line and whatnot. If anyone could help me out, I'd really appreciate it! Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    char output[1];
ifstream inFile;
inFile.open("Hello World.txt");
if (!inFile.is_open())
    cout << "could not open";
inFile>>output;
cout << output;

return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <fstream>
#include <iostream>
#include <string>

int main() {
    std::string line;

    std::ifstream in("Hello World.txt");

    if ( !in.is_open() ) { std::cerr << "No dice!\n"; return 0; }

    while ( getline(in, line) ) {
        if ( !line.empty() ) std::cout << line[0];
        std::cout << '\n' ;
    }
}
Thank you! Is there any chance you could explain how you got the program to read the next line? I've been having some trouble with that
Is there any chance you could explain how you got the program to read the next line?

I used getline in a loop? The program reads the entire file line by line and outputs the first letter of each as it is read.

The code in the OP tried to stuff a white space delimited token into a single char and didn't repeat via loop.
Topic archived. No new replies allowed.