getline() not working

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int password::pass_search_in_file(char* name)
{
    char c_line[20] = {0};
    int n_line = 0;
    ifstream filex;
    filex.open("password");
    int i;
    while (i<3)
    {
        filex.getline(&c_line[0], 20);

        cout << c_line;

        i++;
    }

    filex.close();
    return i;
}


I opened the "password" file and i see
1
2
dfas
dfas

But how comes the c_line[] didn't hold any things? Does this error related to the "ignore" stuff?
Im new to programming but maybe its because you didn't give 'i' a value. How does your loop execute with no value?
Last edited on
I do not believe so. You should check the file and make sure that what is in it matches what is on the screen. I remember that when I was a beginner, I would get correct read-outs, but the problem was with the way the program was writing to the file, lol. Being more practiced, I no longer have those issues.
try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
	fstream file_writer ("D:\\read this.txt");
	string output = "";
	while (!file_writer.eof()) {
		getline (file_writer, output);
		cout << output << endl;
	}
	cin.get();
	return 0;
}
Topic archived. No new replies allowed.