-3
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.

user4581301
  • 29,019
  • 5
  • 26
  • 45
  • 2
    Have a look at [`std::getline()`](https://en.cppreference.com/w/cpp/string/basic_string/getline) – G. Sliepen Jan 10 '20 at 17:40
  • 1
    What did you try ? What is your issue ? SO is not a free coding service. Moreover, details are missing (how do you plan to split the "other lines" in 3 arrays for example). – Fareanor Jan 10 '20 at 17:42
  • 2
    Does this answer your question? [Read file line by line using ifstream in C++](https://stackoverflow.com/questions/7868936/read-file-line-by-line-using-ifstream-in-c) – Mahmoud Hendi Jan 10 '20 at 17:44
  • It looks like you want to read the first 4-lines into into members of an outer `struct` and the rest into a `std::vector` member. What have you tried so far? – David C. Rankin Jan 10 '20 at 18:06
  • Unfortunately this is one of the "How do I do it?" questions where there are a multitude of approaches that can be taken to read and store the data. A C++ approach would be as described above and [Read Location Data](https://susepaste.org/46591390) which is a simple class implementation taking advantage of the overload of `>>` and `< – David C. Rankin Jan 10 '20 at 18:55

1 Answers1

1

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

Mahmoud Hendi
  • 139
  • 11
  • Well this appends all lines to `var`. Question wants just first 5 lines, then put the others in `char` array for some reason – Cruz Jean Jan 10 '20 at 17:45
  • I edited it so it does what he wants. – Mahmoud Hendi Jan 10 '20 at 17:58
  • @user4581301 I added more explanation, and references to read from. What else can I do? – Mahmoud Hendi Jan 10 '20 at 18:39
  • 1
    Much improved, but watch out for [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Jan 10 '20 at 18:44
  • What should I do when I have 2 or more data sets in my file? Think above set is a data set in my file and each set is separated by a blank line. Ex: 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 **************************** VA532 25/02/2020 12:25 COLOMBO NEW DELHI 5 B AE 9 B CDE 11 E AF 16 E ABC 23 E DE 29 E DEF 31 E ACE 39 E CDEF 43 E ABC 50 E BCDE – Harshana Walpita Jan 10 '20 at 19:06
  • If you know how many lines you want to read before the blank line, you can easily put a second loop which loops exactly the number of lines before the blank space. example: int n=5;//read 5 lines while (infile.eof( ))//Mean read until the end of file { for(int i=0;i – Mahmoud Hendi Jan 10 '20 at 19:14