View Single Post
Old 07-24-2006, 01:34 PM   #15 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
hmm.. from what I can see, the first column is containing 25 chars, the second 11 chars, the third 25 chars, the fourth 11 chars, the fifth 25 chras, the sixth 11 chars, the seventh 25 chars and the eight 11 chars.
And theres 4 rows describing each seperate "four infos"

So from my point of view, the definition of it is something like:
Code:
typedef struct{
    char first[25];
    char second[11];
}info; 
std::ifstream ifp("in_file.txt");
std::ofstream ofp("out_file.txt);
std::vector <info> list;
std::string str;
while(! ifp.eof()){
/* read four lines at a time */
  for(int i=0; i < 4; ++i){
    std::getline(ifp, str);
    info tmp;
  /* split each line up into four columns consisting of two combined infos */
    for(int j=0; j < 4; ++j){
        strncpy(tmp.first, str.substr(j*25+j*11, 25), 25);
        strncpy(tmp.second, str.substr(25+j*25+j*11, 11), 11);
        list.add(tmp);
    }
  }
/* rearange the locations of the read info, to reflect the wanted format */
  for(int i=0; i < 4; ++i){
    ofp << list[i].first << ", " <<  list[i].second << ", " 
          << list[i+4].first << ", " << list[i+4].second << ", "
          << list[i+8].first << ", " << list[i+8].second << ", "
          << list[i+12].first << ", " << list[i+12].second << std::endl;
  }
/* make sure nothing is left in our list, so we wont repeat the output */
  list.clear();
}
Now I havn't tested any of this, but it looks to me like you have to assume there will allways be these restrictions on the input strings, so I left out all the error checking.
If you wanted you could see if the info stored in first/seond is all spaces, then skip printing it, and you'll end up with your comma seperated info, where the empty fields wont be represented.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote