Trying to read a file in an array of chars

So, I'm trying to write a program wich reads the first line of a file "Test.txt", converts the string into an array of chars and then compares them to some selected ones, increasing the value of "x" each time it finds a selected char. The problem I got is that it doesnt read the full string of "Test.txt" and instead only reads the first character.

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
32
33
34
35
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <cstring>

using namespace std;

int main()
{
    int x = 0;

    fstream datei("C:\\Users\\Name\\Desktop\\Test.txt", ios::in);  
     //reads the file

    string line; //string

    char zeile[200]; //the array of chars

    for(int i=0;i<201;i++){
            getline(datei,line); //writes the first line to a string

            strncpy(zeile, line.c_str(), sizeof(zeile));
            zeile[sizeof(zeile) - 1] = 0;  //converts the string "line" to chars

        if(zeile[i]=='a'||zeile[i]=='e'||zeile[i]=='i'||zeile[i]=='o'||zeile[i]=='u'){
            x++;
          //compares the char it gets
        }
    }
    datei.close(); //closes the file
    cout << x;     //outputs "x"

    return 0;
}
There is no need to mix std::string and char[], you can use std::string throughout and use the standard library facilities that come with it:
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
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>

bool is_vowel(char c) {
    c = tolower(c);
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}

int main()
{
    std::ifstream inFile ("D:\\input1.txt");
    std::string line{};
    unsigned int x{};
    if(inFile)
    {
        while(getline(inFile, line))
        {
            for (const auto& elem : line)
            {
               if(is_vowel(elem))
               {
                    ++x;
               }
            }
        }
    }
    std::cout << x << '\n';
}

Topic archived. No new replies allowed.