简体   繁体   中英

how to read File line by line and store data in separate arrays

how to read File line by line and store data in separate array like: all the names in one array and marks in another array? can someone help following is my text file, it output only the first name from the list i dont know what to do? My text file is: SARA,DS,A,4 GHANIA,CS,D,3 SIDRA,SE,C,4 JAMIL,AI,A,6 FATIMA,CS,C,4 KOMAL,CS,B,4 FIZA,SE,A,9 ZARA,SE,A,4 HANII,CS,B,4 FOZIA,SE,C,7

void storeData(string studentRecords)

{

string sData;
ifstream recordFile(studentRecords);
if (recordFile)
{
    getline(recordFile, sData, ',');
    cout << sData;
}

}

You can do something like this:

#include <fstream>
#include <iostream>
#include <vector>

int main(void) {

  std::vector<std::string> data;
  std::fstream file("temp.txt", std::ios::in);
  std::string line;

  while (!file.eof()) {
    std::getline(file, line);
    line = line.substr(0, line.find_first_of(","));
    std::cout << line << "\n";
    data.push_back(line);
  }
  return 0;
}

And use vectors instead of array .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM