Thread: Parsing File

  1. #1
    Registered User
    Join Date
    Dec 2009

    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:

    #Form = Test
    #Form = Test

    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.

    #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];
      else cout << "Unable to open file";
      return 0;
    i hope anybody could help me

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    What's exactly the problem?

    Don't confuse c-string (null-terminated char array) with std::string. Actually, just use std::string. So:
    string cline;
    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
     cout << cline
    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
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM