Parsing data from File

This is a discussion on Parsing data from File within the C++ Programming forums, part of the General Programming Boards category; suppose i have a text file named "input.txt" with these : Code: .......garbage data.....http://mysite.html?id=1234&n=john_me.........garbage data....... http://mysite.html?id=56482&n=john_me.........g data... Objective: 1.Parse all ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    Angry Parsing data from File

    suppose i have a text file named "input.txt" with these :

    Code:
    .......garbage data.....http://mysite.html?id=1234&n=john_me.........garbage data.......
    http://mysite.html?id=56482&n=john_me.........g data...
    Objective:
    1.Parse all url and store in output file.
    2.Parse id no along regarding url in that same file.

    i tried this:[plz say what to modify or add]

    Code:
    ifstream fin("input.txt",ios::in);
    if(!fin){cout<<"cant open input.txt"; return 1;}
    
    fin.unsetf(ios::skipws);
    
    int k;char ch;
    for(int i=0; !fin.eof() ;i++)
    {
    fin>>ch;
    if(ch=='h' && fin.peek()=='h'.....)
    
    then what?
    }
    what to do to parse these url and stose in an array.?? actually i am syntax less here...:[

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    A little unclear on the structure of your input file. Is it
    URL Garbage
    Are there spaces?

    if it's like the above case, thats 2 words per line (URL's are just one long sequence since there are no spaces). You can read in each of the 2 words.

    Code:
    string url, dump;
    while(fin >> url >> dump){
           //do whatever you need with them.
          //search the url string for the id using string find functions
    }
    Once you have the url saved you can easily search it for the id and grab everything between = &

    If it's completly random garbage all over. Read in one word at a time. Then test the string using find() and see if it contains http
    Code:
    string dump
    while(fin >> dump){
           //test for http
                if yes //find id
         
    }
    Don't use eof().
    Last edited by gamer4life687; 09-17-2009 at 06:39 AM.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    I'd grab the whole file in one go into a single string object and then use the container's find member function repeatedly in a loop to parse out the URLs and the other stuff.

    To grab the whole file into a string in one go:
    Code:
    #include <string>
    #include <sstream>
    
    ...
    
    std::ifstream file("your_file_name_here");
    std::stringstream sstr;
    std::string data;  // Store complete file contents here
    
    sstr << file.rdbuf();  // Get whole file into string stream obj
    data = sstr.str();  // "data" now contains the whole file
    You can store the URLs into a std::vector<std::string> container if needed.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Quote Originally Posted by hk_mp5kpdw View Post
    I'd grab the whole file in one go into a single string object and then use the container's find member function repeatedly in a loop to parse out the URLs and the other stuff.

    To grab the whole file into a string in one go:
    Code:
    #include <string>
    #include <sstream>
    
    ...
    
    std::ifstream file("your_file_name_here");
    std::stringstream sstr;
    std::string data;  // Store complete file contents here
    
    sstr << file.rdbuf();  // Get whole file into string stream obj
    data = sstr.str();  // "data" now contains the whole file
    You can store the URLs into a std::vector<std::string> container if needed.

    Actually i can do this within file.
    But my my problem is parsing uls , ids and save them into array
    I am sorry but wd u kind plz write a complete parsing code


    Actually i want this output like in array:
    Code:
    url[0]="http://mysite.html?id=1234&n=john_me";
    id[0]="1234";
    
    url[1]="http://mysite.html?id=5684&n=john_me"
    id[1]="5684";
    
    ......
    ......
    Last edited by lighten123; 09-17-2009 at 07:53 PM.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    i GOT MY PROBLEM SOLVED.

    THANK U ALL FOR UR TIME.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 02:42 AM
  4. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 10:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21