Jump to content
  • Advertisement
Sign in to follow this  
GameMasterXL

How do i read names from files?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do i read names from a file like if my file had Orange, Bannana, Apple. How would i read each individual name? because i am wanting to read in the names and check if they are recognised and then if they are output them if not ignore them.

Share this post


Link to post
Share on other sites
Advertisement

Hi,

We might need more information. What language are you using? Is the file format regular ascii or binary?

Later,

GCS584

Share this post


Link to post
Share on other sites
However, the premise is the same:
+You take in the data from the file and dump it into a variable
+You then 'tokenize' the data. This means that if you have a string like
"Orange Bannana Apple"
and you want to get the individual words you use a white space as the token (' ') and do the following loop:

-Find the first instance of the token and tokenize (like split the string into components). You basically copy all the data before the token and the data after and dump the first section of the string into one variable and dump the data after the token into another variable and you then do the loop again on the second variable.
Well, thats how I do it anyway.

eg pseudo code (assuming you have read in the data into the var Data:
var Data, String1, String2;
Data = FileRead();
int pos;
pos = Data.Pos(' ');
String1 = substr(Data,0,pos-1);
String2 = substr(Data,pos+1,Data.Length());
loop
{
pos = String2.Pos(' ');
String1 = substr(String2,0,pos-1);
String2 = substr(String2,pos+1,String2.Length());
if(String2 == "")
break;
}

Well, thats kinda how you do it off the top of my head.

Hope thats helps in a kinda pseudo way

Share this post


Link to post
Share on other sites
Quote:
Original post by MotionCoil
-Find the first instance of the token and tokenize (like split the string into components). You basically copy all the data before the token and the data after and dump the first section of the string into one variable and dump the data after the token into another variable and you then do the loop again on the second variable.
Well, thats how I do it anyway.


Well, I'll make the supposition that the names are in the file with line breaks [smile] as follows:


Apples
Oranges
Bananas



But hey, I could be wrong!

Later,

GCS584

Share this post


Link to post
Share on other sites
Yeah the names are in seperate lines. It is in C++. I just need to get each individual name, i maby could read each character then when i reach whitespace exit and go to a new line but i don't know if that will work, what if it was all on one line though?

Share this post


Link to post
Share on other sites
Ah, this makes it a bit easier. Use IOStream objects:
string data;
ifstream file;
file.open(Filename, ios_base::in);

if (!file.is_open())
return -1;


while(getline(file, data))
{
//Do your processing here
}

Quote:

what if it was all on one line though


Then you use the method I stated before but use the above method to get the different lines. You need to do some string processing and tokenize the information to get the individual names out of different lines.

An example of string tokenizing

The String class is your friend[smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by MotionCoil
Quote:

what if it was all on one line though


Then you use the method I stated before but use the above method to get the different lines. You need to do some string processing and tokenize the information to get the individual names out of different lines.


Yeah, he's quite right. There is also a nifty function called strok(..). It makes that somewhat dirty stuff really easy.

A description and example of the function can be found here. In all honesty, this is my most favorite function in C.

Later,

GCS584

[EDIT:] So basically, your best bet for my recommendation above (if the fruit is all on one line) would be to read all the characters in a single array and then split.

Share this post


Link to post
Share on other sites
Quote:

strok(..)


Indeed, if you want to just stick with chars or if you want it to look nice and neat then definitly use strtok. My method is somewhat bruteforce [smile] Comes from many years of Borland C++ Builder before Visual C++ [wink]

Anyway, you should now be fully equipped to parse any string no matter what kinda format its in.
If you need practice to remember how to do it, try parsing some INI files, they are really easy [smile]

[Edited by - MotionCoil on May 25, 2005 6:10:39 PM]

Share this post


Link to post
Share on other sites
Thanks for all the help, i was just stuck because i am building a parser and up to now i could do this basic command P(Hello Worl) that would display Hello World. But my parser ignored whitespace so if i did put P(Hello World) it would read it in like this HelloWorld, so i removed the part which ignored whitespace and it worked but if i added a space in between the function name and its brackets P (Hello Worl) i would get a parse error :(. So i think this function will help because now i will be able to read in my whole string from the function to be outputed and be hopefully able to process if, esle, for ect.



[Edited by - GameMasterXL on May 26, 2005 2:44:52 PM]

Share this post


Link to post
Share on other sites
Sorry about this but i have come into yet another problem. I want to validate the input from the file so like this:
if
so like if i wanted to see if their is an if in a file how would i check this when i am reading individual characters? this is what i am having problems with since i can't read whole words and check if that word is recognised. I could only read individual characters so i don't know if they are going to be the correct word.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!