c++ text file processing
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!
Since you say C++ rather than C, just use an ifstream and use the extraction operator (operator >>). It will extract tokens based on whitespace.
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;}
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 >>.
vector<Name> Names;
EDIT: The << is backwards as well. They should all be >>.
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:
a) Read line from file
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 :)
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:
a) Read line from file
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 :)
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)
i think i can answer my own question since i know that text files are processed line by line (until EOL)
duh
duh
Old-skool is best!
Didn't run it through the compiler, but it ought to Just Work (tm).
This better not be a homework question, by the way :-)
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 :-)
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement