2014-11-12 4 views
-1

с трудным временем нахождения способа назначения каждой строки текстового файла, считываемого для другой переменной, я прокомментировал выше переменные, отображающие, как строка из текстового файла отображается эта переменная будет выглядеть. Я хочу знать, как я могу использовать forloop, чтобы иметь возможность перебирать весь текстовый файл и хранить данные для каждой из переменных, которые я прокомментировал выше, в зависимости от типа данных, которые он должен хранить. Три набора переменных все должны храниться по видам и таким образом, чтобы их можно было манипулировать. Как я могу разбить вектор на набор из трех переменных?Итерация через текстовый файл и назначение переменных для каждой строки

#include "stdafx.h" 
#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

string getInputFileName() 
//retrieves the inputfile 
{ 
    string fileName; 
    ifstream inputfile; 

    //prompting user for filename to be opened 
    cout << "Enter the file name to be opened: "; 
    cin >> fileName; 

    //opening the file for input 
    inputfile.open(fileName, ios::in); 

    //checks to see if writing the input file failed 
    if (inputfile.fail()) 
    { 
     cout << "Opening file..." << fileName; 
     cout << "\n"; 
     cout << "The " << fileName << "could not be opened! \n"; 
     cout << "1. Check if file exists. \n"; 
     cout << "2. Check file path. \n;"; 

    } 
    else 
    { 
     cout << "File: " << fileName << " was successfully opened!" << endl; 
     return fileName; 
    } 

} 
string getOutputFileName() 
//retrieves the inputfile 
{ 
    string fileName; 
    ofstream outputfile; 

    //prompting user for filename to be opened 
    cout << "Enter the file name to be opened: "; 
    cin >> fileName; 

    //opening the file for input 
    outputfile.open(fileName, ios::in); 

    //checks to see if writing the input file failed 
    if (outputfile.fail()) 
    { 
     cout << "Opening file..." << fileName; 
     cout << "\n"; 
     cout << "The " << fileName << "could not be opened! \n"; 
     cout << "1. Check if file exists. \n"; 
     cout << "2. Check file path. \n;"; 

    } 
    else 
    { 
     cout << "File: " << fileName << " was successfully opened!" << endl; 
     return fileName; 
    } 

} 


int main() 
{ 
    //opens clasfication file 
    ifstream inputFile(getInputFileName()); 

    //declaring year and numberOfSpecies 
    int year, numberOfSpecies; 
    string line; 

    if (inputFile.is_open()) 
    { 
     //year of file 
     inputFile >> year; 
     //echo for year 
     cout << year << endl; 

     //number of species of file 
     inputFile >> numberOfSpecies; 
     //echo for number of species 
     cout << numberOfSpecies << endl; 

     string line; 

     //variables i need to assign line by line and be able to manipulate 
     //region of species would look like this in text file: 84 
     //nameOfspecies would like like this in the text file: Spotted Gecko 
     //regionSightings would look like this in the text file: 84 95 30 25 



     vector<string> linesOfData; 

    for (int i = 0; (!inputFile.eof()) || (i <= numberOfSpecies) ; i++) 
    { 

     getline(inputFile, line, '\n'); 
     linesOfData.push_back(line); 

     //echo vector! 
     cout << linesOfData[i] << "\n"; 

     } 
     ofstream outputFile(getOutputFileName()); 

    } 

    return 0; 
} 
+0

Вы пытаетесь решить сразу несколько проблем. Разбейте его, сначала решите более простую проблему. И разработайте новую функциональность * в изоляции *, не встроенную в большой объем другого кода. – Beta

+0

Какая простая проблема была бы первой проблемой, которую я решаю? – user3236101

+0

Я бы использовал 'std :: vector ' для хранения каждой строки текстового файла. Сначала я читал файл по строкам в последовательные элементы этого вектора и закрывал файл. Затем я прохожу и разбиваю прочитанные строки в каждом экземпляре вида, который вы записали. Каждая группа из трех строк будет заполнять каждый из 3-х атрибутов, которые имеет вид. Вам просто нужна специальная обработка для первых двух строк, как вы уже сделали. – enhzflep

ответ

1

Псевдо код для ваших требований. Вам нужно будет заполнить код и выполнить необходимое тестирование. Я использовал подобную логику много раз для своих требований. Я вижу, что это более удобно и масштабируемо.

class spotted_species{ 
    private: 
     int first_val[]; 
     string species_name; 
     int locations[]; 

    // Private methods 
    private: 
     void assign_species_firstVal (String first_line){ 
      Assign the first line after performing required transformations; 
     } 

     int assign_species_name (String Second_Line){ 
      if (the species name exists in predefined array or file) { 
       assign the name; 
      } 
      else { 
       return -1; // to ignore all 3 lines. 
      } 
     } 

     void assign_species_locations (String third_line){ 
      tokenize by space 
      convert each token to int and assign. hardly 2 lines code. 
     } 

    // Public methods 
    public: 
     void show_species(){ 
      std::cout<< "First val"; 
      std::cout<< "Species name"; 
      std::cout<< "Spotted locations"; 
     } 

     int init(String firstline, String secondline, String ThirdLine){ 
      assign_species_firstVal(firstline); 

      int status = assign_species_name (secondline); 
      if (status) { 
       assign_species_locations (ThirdLine); 
      } 

      return status_accordingly; 
     } 
} 


int main(int argc, char *argv[]) 
{ 
    // Create an array of spotted_species to ensure you can hold the required number of 
    // species from the file. 
    run a OS command "WC -l" which gives the total number of line in your file. 
    All file handling could be done here based on the output of the above command. 

    // Calculate the number of objects you would need based on the number of lines. 
    // Rough estimate would be fine for these requirements. 
    int num_species = lines_in_file/3 + 10; // +10 is used to be on safer side. 

    // Create the objects 
    spotted_species species_spotted_in_africa[num_species]; 

    int ctr; 
    while (read file until the last line) 
     // Read 3 lines at a go, 
     // store them in 3 variables. 
     // call init with all 3 arguments. 
    } 

    // Now you have all the species information in seperate objects. 
    // you can access them in a loop or by any other means. 

    // If you need to perform any additional processing on these objects, 
    //  you always can extend this class or 
    //  you could write another class which can process this class data. 

    // Flexible, simple, Maintainable and scalable. 
    return 0; 
} 

Вам нужно будет внести некоторые изменения в соответствии с вашими точными требованиями. Надеюсь, это поможет.

0

Исходя из того, что у вас есть 3 линии данных на вид, например:

84 
Spotted Gecko 
84 95 30 25 

Я хотел бы предложить использовать vector из struct значений вместо string значений, где каждый struct хранит данные для одного вида. Пройдя через входной файл, прочитайте по 3 строки за раз, проанализируйте их в экземпляре struct, а затем вставьте его в * vector.

Попробуйте это:

#include "stdafx.h" 

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

using namespace std; 

struct species 
{ 
    int region; 
    string name; 
    std::vector<int> regionSightings; 
}; 

bool openInputFile(ifstream &inputfile) 
{ 
    string fileName; 

    //prompting user for filename to be opened 
    cout << "Enter the input file to be opened: "; 
    getline(cin, filename); 

    //opening the file for input 
    cout << "Opening file: " << filename << endl; 
    inputfile.open(fileName); 

    //checks to see if opening the input file failed 
    if (!inputfile) 
    { 
     cout << "File: " << fileName << " could not be opened!" << endl; 
     cout << "1. Check if file exists." << endl; 
     cout << "2. Check file path." << endl; 

     return false; 
    } 

    cout << "File: " << fileName << " was successfully opened!" << endl; 
    return true; 
} 

bool openOutputFile(ofstream &outputfile) 
{ 
    string fileName; 

    //prompting user for filename to be opened 
    cout << "Enter the output file to be opened: "; 
    getline(cin, filename); 

    //opening the file for input 
    cout << "Opening file: " << fileName << endl; 
    outputfile.open(fileName); 

    //checks to see if opening the output file failed 
    if (!outputfile) 
    { 
     cout << "File: " << fileName << " could not be opened!" << endl; 
     cout << "1. Check if file exists." << endl; 
     cout << "2. Check file path." << endl; 

     return false; 
    } 

    cout << "File: " << fileName << " was successfully opened!" << endl; 
    return true; 
} 

int main() 
{ 
    //opens clasfication file 
    ifstream inputFile; 
    if (openInputFile(inputFile)) 
    { 
     //declaring year and numberOfSpecies 
     int year, numberOfSpecies; 
     string line; 

     //year of file 
     inputFile >> year; 
     //echo for year 
     cout << "Year: " << year << endl; 

     //number of species in file 
     inputFile >> numberOfSpecies; 
     //echo for number of species 
     cout << "# species: " << numberOfSpecies << endl; 

     //there are three lines per species 
     //region of species, eg: 84 
     //nameOfspecies, eg: Spotted Gecko 
     //regionSightings, eg: 84 95 30 25 

     vector<species> speciesData; 

     for (int i = 0; i < numberOfSpecies; ++i) 
     { 
      species s; 

      if (!getline(inputFile, line)) 
      { 
       cout << "File: " << fileName << " could not read a species region!" << endl; 
       break; 
      } 

      if (!(istringstream(line) >> s.region)) 
      { 
       cout << "File: " << fileName << " could not parse a species region!" << endl; 
       break; 
      } 

      if (!getline(inputFile, s.name)) 
      { 
       cout << "File: " << fileName << " could not read a species name!" << endl; 
       break; 
      } 

      if (!getline(inputFile, line)) 
      { 
       cout << "File: " << fileName << " could not read a species sightings!" << endl; 
       break; 
      } 

      istringstream iss(line); 
      int num; 

      while (iss >> num) 
       s.regionSightings.push_back(num); 

      if (!iss.eof()) 
      { 
       cout << "File: " << fileName << " could not parse a species sightings!" << endl; 
       break; 
      } 

      speciesData.push_back(s); 

      //echo vector! 
      cout << s.region << " " << s.name << " " << line << endl; 
     } 

     cout << "Closing input file" << endl; 
     inputFile.close(); 

     ofstream outputFile; 
     if (openOutputFile(outputFile)) 
     { 
      for (std::vector<species>::iterator iter = speciesData.begin(); 
       iter != speciesData.end(); 
       ++iter) 
      { 
       species &s = *iter; 

       // write species data to outputFile as needed... 
      } 

      cout << "Closing output file" << endl; 
     } 

     cout << "Finished" << endl; 
    } 

    return 0; 
} 
+0

этот код не повторяет вектор по тем или иным причинам? – user3236101

+0

Это только отголоски каждого вида, который успешно читается. Если он не эхо, то он не читает вид. Вы пытались запустить его в отладчике, чтобы узнать, что на самом деле происходит? –

+0

Да, и все идет так, как должно, поэтому я понятия не имею ...... – user3236101

Смежные вопросы