0

How can I read from a text file line by line , then use the same array to save them ..

HRJ
  • 1
  • 5

2 Answers2

2

Firstly, it seems that you are using using namespace std; in your code. This is highly discouraged. Here is how I would use std::vector. First, you have to import the <vector> header file.

std::ifstream in_file("file.txt");
if (!in_file) { ... } // handle the error of file not existing

std::vector<std::string> vec;
std::string str;

// insert each line into vec
while (std::getline(in_file, str)) {
    vec.push_back(str);
}

std::ifstream's .close() method is handled in its destructor, so we don't need to include it. This is cleaner, reads more like English, and there are no magical constants. Plus, it uses std::vector, which is very efficient.

Edit: modification with a std::string[] as each element:

std::ifstream in_file("file.txt");
if (!in_file) { ... } // handle the error of file not existing

std::vector<std::string[]> vec;
std::string str;

// insert each line into vec
while (std::getline(in_file, str)) {
    std::string array[1];
    array[0] = str;
    vec.push_back(array);
}
Community
  • 1
  • 1
Ryan Dougherty
  • 528
  • 6
  • 21
  • I kinda understand what you are saying but i never used , Do you have any ideas if i should save each line into a different array . – HRJ Nov 16 '14 at 03:39
  • 1
    Why would you want each `std::string` line to be in a different array? – Ryan Dougherty Nov 16 '14 at 03:40
  • because i want to use those data into different class of object for example the first line should be a record for a student – HRJ Nov 16 '14 at 03:41
  • You can then use the `[]` operator or `.at(int)` provided by `std::vector` to access individual elements. For example, if the first `std::string` of the vector is a student's name, then you would have `std::string student_name = vec[0];`. – Ryan Dougherty Nov 16 '14 at 03:42
  • I am really bad with vector , do you know any way for array please where i could save every line into an different array , i know will have to convert the last part of the array to integer to computer those number – HRJ Nov 16 '14 at 03:45
  • Then just use `std::vector>` and have each inner vector contain one element (each line). – Ryan Dougherty Nov 16 '14 at 03:46
  • please help me with the array not vector :( – HRJ Nov 16 '14 at 03:51
  • What reason would one need to use an array instead of a vector? – Ryan Dougherty Nov 16 '14 at 03:51
  • I don't know how to use vector , but i am much better with array ,my question how to save each line into an array – HRJ Nov 16 '14 at 03:54
  • See the edit above. It can be modified to your use case, but I would strongly encourage learning `std::vector`. – Ryan Dougherty Nov 16 '14 at 03:56
  • no vector please :(, I know i will have 10 arrays ,where in my loop should i said that when we are the last item in the line to go to the next line then save it into a new array – HRJ Nov 16 '14 at 03:59
  • I re-read your original post. By "line" do you mean separate lines or separate words on one single line? – Ryan Dougherty Nov 16 '14 at 04:00
  • basically in the first line each item should be save into in array[10] then once we reach the end of the line go to the next one then save each item into a secondarray[10] , the same process should be repeated until we read those 10 lines – HRJ Nov 16 '14 at 04:03
  • So basically you need an array of arrays. For splitting each individual line up, see this: http://stackoverflow.com/a/237280/3232207. Otherwise, it will be up to you to figure out how to have 10 (or for that matter, any number) different arrays. – Ryan Dougherty Nov 16 '14 at 04:05
  • its all vector , i am screwed :( – HRJ Nov 16 '14 at 04:07
  • Go through this tutorial: http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm – Ryan Dougherty Nov 16 '14 at 04:07
  • so basically you mean that if i used vector ,i will be able to save every line and access each item of each line – HRJ Nov 16 '14 at 04:12
  • Yes, that is exactly what I'm saying. Also, normal arrays have a fixed size - what if your input file has a variable number of lines? – Ryan Dougherty Nov 16 '14 at 04:13
0

Reference to getline says that data is appended to the string

Each extracted character is appended to the string as if its member push_back was called.

Try resetting the string after the read

Edit

Each time you call getline, line keeps the old content and adds the new data at the end.

line = "";

Will reset data between each read

Eric
  • 19,525
  • 19
  • 84
  • 147