# c++ text file processing

This topic is 5413 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

its been quite a while since i've had to do any file operations, so i'm a bit rusty. i'm trying to read a list of names from a file. the names separated by a whitespace and are in groups of two on each line. the first one in each group will begin with a capital letter, the second with a lowercase. for example: Bob tom Sue jim Pete dan The only way i can think to do this would be to use a combination of fscanf and fseek (or ftell) to read until a whitespace, set the new postion, and repeat. I'm not exactly sure if this will work though. Also, i do NOT know ahead of time how big the file is, but i know i can use a function to get the filesize. cheers!

##### Share on other sites
Since you say C++ rather than C, just use an ifstream and use the extraction operator (operator >>). It will extract tokens based on whitespace.

##### Share on other sites
Maybe something like this? I don't know if this would work, I'm writing it off the top of my head and not testing it, but something like that should work.

#include <string>#include <fstream>#include <vector>using namespace std;struct Name{  string First;  string Last;};int main(void){  vector<string> Names;  ifstream fin;    fin.open(FileName);  while(!fin.eof())  {    Name TempName;        fin << TempName.First << TempName.Last;    Names.push_back(TempName);  }  return 0;}

##### Share on other sites
Sorry, I posted anonymously accidentally and it wouldn't let me edit the post, but that should be:

vector<Name> Names;

EDIT: The << is backwards as well. They should all be >>.

##### Share on other sites
I'd be tempted to use regular expressions with boost::regex for this little problem, however it might be a bit overkill given it's nature.

If using C++, I recommend looking into std::string for your string class and std::ifstream to load the files. The process would be broken down into:

b) Tokenise the string into words by using the space character as a delimiter
c) Do whatever with the names
d) Repeat until EOF

I'm not going to throw some code at you because as you said, you wish to learn [grin]. Instead, here's some useful resources:

1) std::ifstream reference
2) HOWTO: C++ Chapter 7, std::string class
3) CodeProject: std::string tokenizer class

Have fun :)

cool thnx

##### Share on other sites
one more question: how would i test a string to see if the first letter is a capital letter? or, by using tokens, how could i test if a token is the first token on a line. (the first name on each line, i.e. the one beginning with a capital letter, is going to be put into an array in my program)

##### Share on other sites
i think i can answer my own question since i know that text files are processed line by line (until EOL)

duh

##### Share on other sites
Old-skool is best!

char line[1024];while( true ) {  line[0] = 0;  fgets( line, 1024, stdin );  if( !line[0] ) {    break;  }  char name_a[1024], name_b[1024];  if( 2 != sscanf( line, "%s %s", name_a, name_b ) ) {    file_syntax_error();  }  else {    process_name_pair( name_a, name_b );  }}

Didn't run it through the compiler, but it ought to Just Work (tm).

This better not be a homework question, by the way :-)

##### Share on other sites
hahah. no, its not homework. i graduated last may ;) just a little project i'm working on

your explanation might work, but not for my implementation, which i guess i should explain better: i want to create an adjacency list - each original name in the file will be entered into array. each array member will have an associated linked list. for example, the file

Bob tom
Bob jim
Jim bob

would produce an adjacency list like this:

0 Bob | tom --> jim --> NULL
1 Jim | bob --> NULL

0 and 1 being the array indices

• 18
• 29
• 11
• 21
• 16