First Letter of Every Line in File

Hello! I'm trying to figure out how to create a program that prints the first letter of every line in a file, but I'm at a bit of a loss as to how to only take in one letter and move to the next line within the file. If someone could let me know as to how to move to the next line within a file, I think I could figure the rest out on my own. Thank you!

(I've attempted the problem, but couldn't get very far.)

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;
ifstream inFile;
inFile.open("F:\\Rida's Stuff\\School\\Grade 10\\Computer Science\\ICS3XI-2016-09-06\\ICS3XI\\Unit 2\\assign_(assign4)_(question4)\\Hello World.txt");
if (!inFile.is_open())
    cout << "could not open";
inFile >> output;
cout << output;

return 0;
}
Did you consider the solution suggested by cire here:

http://www.cplusplus.com/forum/beginner/206769/#msg977174


In your own code, this is an uninitialised pointer:
 
char*output;

and then this line
 
inFile >> output;
attempts to read a string into the memory location pointed to. This gives undefined behaviour - the program is likely to crash - if it doesn't you are unlucky. (better the program crash so you realise something is wrong).

If you want to use a string, then use std::string instead of messing about with character pointers - it is safer - there's no danger of an uninitialised pointer, or of the string being too long to fit.

If someone could let me know as to how to move to the next line within a file,
The simplest way is to use getline() as in the example given.



Last edited on
Strictly speaking OP wants a letter and line[0] could be isdigit, some further checks involving isalpha (or some version thereof) would be required
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>
const std::string letters = {"aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"};
int main()
{
    std::ifstream inFile("D:\\input1.txt");
    while (inFile)
    {
        std::string line;
        getline(inFile, line);
        auto found = line.find_first_of(letters);
        if (found != std::string::npos)
        {
            std::cout << line[found] << '\n';
        }
        else
        {
            std::cout << "No letters on this line \n";
        }
    }
}
Last edited on
The while (inFile) loop is a bit suspect - there really should be a check on the file status after the getline(), to get predictable results.
Yeah, you're right. A more robust approach would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
#include <string>
const std::string letters = {"aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"};
int main()
{
    std::ifstream inFile("D:\\input1.txt");
    if (inFile)
    {
        std::string line{};
        while(getline(inFile, line))
        {
            if(inFile)
            {
                auto found = line.find_first_of(letters);
                if (found != std::string::npos)
                {
                    std::cout << line[found] << '\n';
                }
                else
                {
                    std::cout << "No letters on this line \n";
                }
            }
         }
    }
    else
    {
        std::cerr << "Unable to open file \n";
    }
}



Topic archived. No new replies allowed.