VA301 20/02/2020 10:20 COLOMBO SINGAPORE 10 E AB 15 E CDE 22 E ADF 31 E BCF 35 E ABCD 45 E AB 50 E DEF
These are the details in my file. I want to read this file line by line and store 1st 5 lines into a variable and other lines into 3 char arrays.
VA301 20/02/2020 10:20 COLOMBO SINGAPORE 10 E AB 15 E CDE 22 E ADF 31 E BCF 35 E ABCD 45 E AB 50 E DEF
These are the details in my file. I want to read this file line by line and store 1st 5 lines into a variable and other lines into 3 char arrays.
I don't know why you really want to do so. If you can give me a better explanation, I can give you a better answer.
To read form file you have to use a file stream input. Example:
ifstream infile("thefile.txt");// change thefile to your file name and make sure it's at the same folder with the programe
Now you can use getline()
method to get data from the stream.
string line;
char ch[200];
getline(infile, line);//this to store the line into a string
getline(infile,line,'&'); // the last parameter is the "delimiter"
//getline() will use delimiter to decide when to stop reading data.
infile.getline(ch,200); //this to store the line into a char array
And simply you can read to the of the file using a loop and eof()
method
while (infile.eof( ))//Mean read until the end of file
{
//do something
}
To get everything together:
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream infile("thefile.txt");// change thefile to your file name and make sure it's at the same folder with the programe
string line, var="";
while (infile.eof( ))//Mean read until the end of file
{
getline(infile, line);//this to store the line into a string
var= var + line +'\n';
}
//assuming that they are just 3 other lines
char ch1[200],ch2[200],ch3[200];//you can choose another size
infile.getline(ch1,200);
infile.getline(ch2,200);
infile.getline(ch3,200);
}
For more information you can read: https://en.cppreference.com/w/cpp/string/basic_string/getline https://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm