Чтение определенных значений из файла c ++ - PullRequest
0 голосов
/ 26 января 2012

Мне нужна помощь с получением кода для c ++,

Ситуация такова, что мне нужно прочитать текстовый файл, который содержит что-то вроде:

//THIS LINE IS COMMENTED OUT
//THIS LINE TOO
Variable1 = "1"; //comment for this line
Address = "some text value here"; //comment for this line

Итак, я хочучтобы прочитать этот текстовый файл с помощью c ++ и получить значения как:

Variable1 = 1
Address = some text value here

Итак, как мне это сделать, пожалуйста, нужна ваша помощь специалиста.Мне удалось пропустить только закомментированные строки текстового файла, используя приведенный ниже код, но теперь я не знаю, как читать переменные.Я новичок в C ++

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("text.txt");
if (myfile.is_open())
{
  while ( myfile.good() )
  {
    getline (myfile,line);
    if(line[0]=='/')
      continue;

    cout << line << endl;
  }
  myfile.close();
}

else cout << "Unable to open file";

return 0;
}

1 Ответ

0 голосов
/ 26 января 2012

Поместите цикл while, как показано ниже. Это будет работать определенно ...

while ( myfile.good() )
{
    getline (myfile,line);
    while(line[i]!="\n" || line[i]!='\0')
    { 
        int i=0;

        // we have to check that there is continuously two '/' operator 
        if(line[i]=='/' && line[i+1] == '/')
        {
            while(line[i]!="\n" || line[i]!='\0') // this loop will jump next to comment
                i++;
            break; // this break is used to break the upper one while loop 
        }
        else    
            cout << line[i] << endl; // this line print all character that is before the comment
    }
...