I have some problems reading specific data from a file. The file has 80 characters on the first and second line and an unknown number of characters on the third line. The following is my code:
int main(){
ifstream myfile;
char strings[80];
myfile.open("test.txt");
/*reads first line of file into strings*/
cout << "Name: " << strings << endl;
/*reads second line of file into strings*/
cout << "Address: " << strings << endl;
/*reads third line of file into strings*/
cout << "Handphone: " << strings << endl;
}
How do i do the actions in the comments?
char strings[80]can only hold 79 characters. Make itchar strings[81]. You can forget about the size altogether if you usestd::string.You can read lines with the
std::getlinefunction.The code above ignores the information that the first and second lines are 80 chars long (I’m assuming you’re reading a line-based file format). You can add an additional check for that if it’s important.