01/10/2018, 13:57

Thắc mắc khi Làm việc với File (Đọc File)

Em gặp lỗi ở chỗ này, không sao hiểu được nó sai, a/c giúp đỡ em với ạ !!

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int M = 100;

struct hs {
	int MSSV;
	char ten[100];
	int diemSo;
};

//struct name
//{
//	char ten[100];
//};

void docFile(ifstream &file)
{
	int N;
	file >> N;//số học sinh.
	
	char spam[M];
	hs a[M]; int h = -1;
	for (int i = 0; i < N-1; i++)
	{
		file >> a[i].MSSV, 100;
		cout << a[i].MSSV << endl;
		h++;
		file >> spam[h];
		file.getline(a[i].ten, 100, "|"); //Chỗ này dấu chấm "." ở chỗ file.getline bị lỗi.

	}
	file.getline(a[N - 1].ten, 100,'');
	cout << a[N - 1].ten << endl;
}

int main()
{
	ifstream inFile;
	inFile.open("INPUT.txt", ios_base::in);
	if (inFile.fail())
		cout << "File nay khong ton tai!!" << endl;
	else
	{
		docFile(inFile);
	}
	inFile.close();
	return 0;
}

Hello World viết 16:02 ngày 01/10/2018

Giúp em đi ạ.

viết 15:58 ngày 01/10/2018

Argument thứ 3 phải là char chứ ko phải char[]

*grab popcorn* viết 16:11 ngày 01/10/2018
getline(a[i].ten, 100, "|");

Hmmmm, nhìn kỹ tham số t3 nhé

istream& getline (char* s, streamsize n, char delim );

http://www.cplusplus.com/reference/istream/istream/getline/

Hello World viết 16:07 ngày 01/10/2018

Oh thank anh chị nhiều hì hì

Hello World viết 15:59 ngày 01/10/2018

Nhân tiện cho em hỏi luôn là: Với code như v, dấu “|” thứ 2 ở mỗi dòng nó sẽ đi đâu ạ ??

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int M = 100;

struct hs {
	int MSSV;
	char ten[100];
	int diemSo;
};

//struct name
//{
//	char ten[100];
//};

void docFile(ifstream &file)
{
	int N;
	file >> N;//số học sinh.
	
	char spam[M];
	hs a[M]; int h = -1;
	for (int i = 0; i < N; i++)
	{
		file >> a[i].MSSV, 100;
		cout << a[i].MSSV << endl;

		h++;
		file >> spam[h]; cout << spam[h] << endl;

		file.getline(a[i].ten, 100, '|'); 
		cout << a[i].ten << endl;

		file >> a[i].diemSo;
		cout << a[i].diemSo << endl;
	}
	/*file.getline(a[N - 1].ten, 100,'\0');
	cout << a[N - 1].ten << endl;*/
}

int main()
{
	ifstream inFile;
	inFile.open("INPUT.txt", ios_base::in);
	if (inFile.fail())
		cout << "File nay khong ton tai!!" << endl;
	else
	{
		docFile(inFile);
	}
	inFile.close();
	return 0;
}

Vũ Thanh viết 16:10 ngày 01/10/2018

Tài liệu tham khảo:
– Mở File: https://youtu.be/fFTEea2bJTI
– Đọc File: https://youtu.be/ojvs8rCZ6Lg
– Ghi File: https://youtu.be/8JlC4HQMvPs
– Tìm hiểu fseek ftell rewind: https://youtu.be/pG7lbpNYv1E

viết 16:10 ngày 01/10/2018

file.getline ăn thịt nó mất rồi

để ignore cái thứ nhất ko cần 1 mảng char spam đâu, cần 1 ký tự ignore là được rồi:

char ignore;
file >> ignore; //bỏ qua ký tự | thứ nhất
Hello World viết 16:06 ngày 01/10/2018

Ăn thịt luôn ak :3
ok thks a/c nhiều

Hello World viết 16:05 ngày 01/10/2018

Tuyệt vời !!

Hello World viết 16:11 ngày 01/10/2018

Nếu muốn bỏ qua nhiều thì for hay có cách j hay hơn ko anh

viết 16:12 ngày 01/10/2018

bỏ qua nhiều là sao?

Hello World viết 16:02 ngày 01/10/2018

như bỏ qua nhiều kí tự ấy

viết 16:01 ngày 01/10/2018

file.ignore(100, '|');
bỏ qua tối đa 100 ký tự, hoặc tới khi gặp | thì dừng. Vậy bỏ qua 1 ký tự xài file.ignore() luôn khỏi cần tạo 1 ký tự làm gì cho mất công…

Bài liên quan
0