Hàm getline trong file

Mọi người, cho em hỏi làm sao để đọc các giá trị riêng biệt trên một hàng ạ?
Như em muốn lấy biến string gán vào string, int gán vào int trên 1 hàng của file ạ.
Em cám ơn <3
Em làm theo cách dưới nhưng nó không nhận ạ?

 void docfile(string namefile) {
    	cout << "READ FILE" << endl;
    	ifstream re(namefile, ios::in);
    	string line;
    	while (getline(re,line))
    	{
    		istringstream ss(line);
    		string	name, sex;
    		int day, month, year;
    		float one, two;
    		ss >> name >> day >> month >> year >> sex >> one >> two;
    			cout << name << day << month << year << sex << one << two << endl;
    	}
    	re.close();
    }

Đọc trực tiếp luôn.

re >> name >> day >> month >> year >> sex >> one >> two;
re.ignore() // đưa con trỏ file xuống dòng dưới
            // có thể khai báo 1 biến string trash
            // rồi getline(re, trash) để đọc kí tự xuống dòng,

Em không hiểu lắm ạ?
Em sửa thế này nhưng nó k chạy được ạ

void docfile(string namefile) {
	cout << "READ FILE" << endl;
	ifstream re(namefile, ios::in);
	string line;
	while (getline(re,line))
	{
		string	name, sex;
		int day, month, year;
		float one, two;
		re >> name >> day >> month >> year >> sex >> one >> two;
		re.ignore();
			cout << name << day << month << year << sex << one << two << endl;
	}
	re.close();
}

Đã đọc trực tiếp bằng re >> ... thì đừng getline(re, ...) nữa nhé.

while (re >> name >> day >> month >> year >> sex >> one >> two) {
	re.ignore();
	// code
}

à, em có làm theo kiểu này nhưng nó không nhận dòng cuối cùng ấy ạ…
Như:

Johns 6 6 666 Male 7 8
Ohsns 5 6 777 Male 5 9
Jeams 6 8 776 Male 8 8

thì nó chỉ đọc và xuất 2 dòng đầu ạ.

đâu cần ignore làm gì, cứ đọc thẳng luôn

void docfile(const string& filename)
{
    cout << "READ FILE" << endl;
    ifstream re(filename);
    string name, sex;
    int day, month, year;
    float one, two;
    while (re >> name >> day >> month >> year >> sex >> one >> two)
        cout << name << day << month << year << sex << one << two << endl;
}
2 Likes

Full code của em ạ:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void docfile() {
	cout << "READ FILE........." << endl;
	ifstream re("final.txt", ios::in);
	string name, sex;
	int day, month, year;
	float one, two;
	while (re >> name >> day >> month >> year >> sex >> one >> two)
	{
//		re.ignore();
		cout << name << day << month << year << sex << one << two << endl;
	}
	re.close();
}

int main() {
	docfile();
	system("pause");
	return 0;
}

file " final.txt ":

Phan Trong Tinh               6  2  1999 Nam       8    7

Kết quả nó vẫn k nhận k xuất ra dữ liệu ạ:
Capture

giúp em với =(

ko đọc được là vì name có tới 3 tiếng. re >> name chỉ đọc được Phan, tiếp theo nó đọc Trong vào day là số nguyên ko được nên cả dòng nó fail, ko in ra gì cả

viết lại cho kĩ 1 tí :V

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>

void docfile(const std::string& filename)
{
    std::cout << "READ FILE.........\n";
    std::ifstream ifs{filename};
    if (!ifs)
    {
        std::cerr << "Cannot open file `" << filename << "`\n";
        std::exit(1);
    }
    for (std::string line; std::getline(ifs, line);)
    {
        std::string name, sex;
        int day, month, year;
        float one, two;
        std::istringstream iss{line};
        for (std::string token; iss >> token;)
        {
            try
            {
                day = std::stoi(token);
                break;
            }
            catch (const std::exception& ex)
            {
                name += token + ' ';
            }
        }
        name.pop_back(); // bỏ khoảng cách thừa sau cùng khi += token + ' '
        iss >> month >> year >> sex >> one >> two;

        std::cout << "\n"
                  << name << "\n"
                  << day << "/" << month << "/" << year << "\n"
                  << sex << "\n"
                  << one << " " << two << "\n";
    }
}

int main()
{
    docfile("final.txt");
}

đọc vào từng dòng getline(ifs, line), rồi biến dòng đó thành 1 input string stream để đọc vào các biến kia. Vì ko có ký tự đặc biệt gì ngăn cách name với day nên đành phải đọc từ từ từng từ 1 iss >> token rồi thử chuyển token đó thành số nguyên day = stoi(token) nếu gặp lỗi thì thêm từ đó vào name vì chưa phải là day. nếu chuyển thành int thành công thì break ngừng đọc vì mấy token phía sau day đọc vào dễ dàng iss >> month >> year >> sex >> one >> two

3 Likes

Vâng ạ, lỗi ban đầu của em là nó không nhận dòng cuối ấy ạ :disappointed_relieved::disappointed_relieved::disappointed_relieved:
Nên sau vài hồi đi cầu cứu thì em đã kiếm đươc cách giải quyết bằng cách nhập tên với chuỗi không dấu cách và đọc bằng:

string line;
getline(file, line);
while (file && !line.empty())
{
    //Tách các biến bằng sstream và xử lí
    getline(file, line);
}

Em cám ơn nhiều ạ <3

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?