Thread: Spliting a file

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    64
    Rep Power
    12

    Spliting a file


    Hi, how would you go about taking an open file and split it by spaces, then assign it to variables like y[2] y[3] and so on like php and perl ? I can't seem to find anything on a split function on google. Thnx in advance.:)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    in C: strtok()

    in C++, spaces are default delimiters for streams. You could use:
    y[1]<<file
    y[2]<<file
    y[3]<<file
    ...

    Attn: contrary to php and perl, C++ variables are strictly typed. i.e.:
    A file with the content "123" contains the *string* 123, not a number. so "(int var)<<file" does not work here. AFAIK you have to convert numbers manually.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    64
    Rep Power
    12
    It doesnt seem to be working... Before i had used input from the cmd line and it worked fine,

    Code:
    hpanel.cpp: In function `int main()':
    hpanel.cpp:18: invalid types `char[int]' for array subscript
    hpanel.cpp:19: invalid types `char[int]' for array subscript
    hpanel.cpp:19: parse error before ``'
    hpanel.cpp:24: invalid types `char[int]' for array subscript
    hpanel.cpp:26: invalid types `char[int]' for array subscript
    hpanel.cpp:26: invalid types `char[int]' for array subscript
    hpanel.cpp:27: invalid types `char[int]' for array subscript
    hpanel.cpp:27: invalid types `char[int]' for array subscript

    Code:
    int main()
    {
    
    std::ifstream  fifoArgv("hpaneld.fifo");
    std::ifstream fifoIn("hpaneld.fifo");
    std::ofstream fifoOut("hpaneld.fifo");
    
    char y;
    y[1]<<fifoArgv;
    y[2]<<fifoArgv;`    
    y[3]<<fifoArgv;
    
    	std::string x;
    
    	x=y[1];
    	if (x == "stats"){ sysstat(); }
    	else if(x == "newuser"){  newUser(y[2], y[3]); }
    	else if(x == "service"){ service(y[2], y[3]); }
    
    	else { fifoOut << "Invalid Option." << std::endl; }
    
    
    
    
    
    return 0;
    }
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    Before i had used input from the cmd line and it worked fine,
    ...
    char y;
    y[1]<<fifoArgv;
    I am sure this did not work with the commandline either... y is not declared as an array, but you are using it as such.
    Also you canīt compare char* with the "==" operator. "==" tests if the pointers point to the same memory location, not if the strings are the same.
    In C++ you should prefer std::string to char* where possible. For char*, your case needs special treatment since you donīt know the length of the string yet.
    Code:
    std::string y[3];
    y[0]<<fifoArgv;
    y[1]<<fifoArgv;
    y[2]<<fifoArgv;
    if (y[0].compare("stats")==0){ sysstat(); }
    else if(y[0].compare("newuser")==0){  newUser(y[2].c_str(), y[3].c_str()); }
    else if(y[0].compare("service")==0){ service(y[2].c_str(), y[3].c_str()); }
    else { fifoOut << "Invalid Option." << std::endl; }
    (I would also make newUser() and service() accept std::string instead of char*, but this is up to you)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    64
    Rep Power
    12
    im still getting errors :\

    hpanel.cpp: In function `int main()':
    hpanel.cpp:18: no match for `basic_string<char,string_char_traits<char>,__defaul
    t_alloc_template<false,0> > & << ifstream &'
    hpanel.cpp:19: no match for `basic_string<char,string_char_traits<char>,__defaul
    t_alloc_template<false,0> > & << ifstream &'
    hpanel.cpp:20: no match for `basic_string<char,string_char_traits<char>,__defaul
    t_alloc_template<false,0> > & << ifstream &'

    EDIT: 18, 19, and 20 are

    y[0]<<fifoArgv;
    y[1]<<fifoArgv;
    y[2]<<fifoArgv;
    Last edited by ZeRO; July 13th, 2003 at 09:08 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    do you have a "#include <iostream>"? (without .h!)
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    64
    Rep Power
    12
    Yup.

    I have these

    #include <iostream>
    #include <string>
    #include <fstream>
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    Sorry, I was wrong on the stream operators.
    Itīs:
    Code:
    fifoArgv>>y[0];
    fifoArgv>>y[1];
    fifoArgv>>y[2];
    :)

IMN logo majestic logo threadwatch logo seochat tools logo