-
Parsing File
Hello Everybody,
i'm happy to be here and i say hello to all :)
I have a Poblem. I wont to open a File in c++, Parsing it and then close the file. The goal is. I have a file where some configuration are defined and i want to get differnt parameters, store it in an other file (because i dont need them all)
For example:
Code:
[TestScript]
Username=test/test@Testdb
Form=Start
#Form = Test
Otherparams=szendtime=13:00
PageTitle=Hello
[TestScript_2]
Username=test2/test2@Testdb2
Form=Start
#Form = Test
Otherparams=szendtime=14:00
PageTitle=Hello
so i want to pars the file and start when the programm "see" this [....] (so i know there is a new config part). i want to get only the username, the other stuff i dont need. The programm should also store the username in an other file
The Programm should also know if there is a #, it should jump to the next line.
when the next [] comes it should start again to get the username and store it in an other file and so on.
i have this now. It is not much but i have a problem to convert the string to a char.
Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
long v_nBegin,v_nEnd;
float v_nProzentSatz = 1;
char cline[255];
string sline;
ofstream ConfigFile ("Config\\Config.txt");
ifstream myfile ("Config\\example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,sline);
strcpy(cline, sline);
for(int i = 0; i <= 255; i++)
{
cout << cline[i];
};
}
myfile.close();
}
else cout << "Unable to open file";
cin.get();
return 0;
}
i hope anybody could help me
-
What's exactly the problem?
Don't confuse c-string (null-terminated char array) with std::string. Actually, just use std::string. So:
If you ever want to turn std::string to a c-string just use the c_str() method, like
no need to copy.
Also, strcpy() is a c-function. You just use "=" with std::string. And note that it is wrong the way you use it. You don't include string.h and the parameters of strcpy are wrong as well.
The second thing is that you can simply do this
instead of using a loop. The way you use the loop is a bit wrong as well. You would do this with a c-string, printing only the useful information
Code:
for(int i = 0; cline[i] != '\0' && i < 255; i++)
{
cout << cline[i];
};
I guess that is your problem, you print every 255 value in cline, when you wanted only the ones copied to it.
In any case, you can use [] with a std::string to get a character. So can simply check sline[0] == '#' or sline[0] == '[' and do your job.
Check std::string for more member functions so you can use what you want easily and quickly