Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 file reading logic problem
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tubs




PostPosted: Sat Jul 08, 2006 9:42 pm   Post subject: file reading logic problem

how would one go about reading only the numbers if the following was in a text file?

blah
asdfa
16 9 4
ilikecheese
5 6 7
asdfklja
asdfa

usually i just approach the problem with something like:

while (fscanf(in,"%d %d %d", &var1, &var2, &var3) != EOF);

but in this situation that would never equal EOF since it is not at the end of the file. How would i write this so that only the numbers are scanned, and once it reaches the last series of numbers in the text file it would exit the program?
Sponsor
Sponsor
Sponsor
sponsor
r.3volved




PostPosted: Sun Jul 09, 2006 9:09 am   Post subject: (No subject)

are you using C or C++??

I'd say off hand something like:

c++:

ifstream inFile("filename.txt");
while(!inFile.eof())
{
   getline(inFile, newline, '\n');
   vecLines.push_back(newline);
}
inFile.close();


then parse the vector "vecLines" and check if it's a number or letter
This would work fine for a small file, but this becomes redundant quickly when you have large data sets (ie. there's no point in reading the whole file and comparing variable data when you can check with each line parse)

You could do a some error catching and grab data like:
code:

inFile >> newNum;


if it throws an error it's a letter, otherwise the number will be cast to newNum.
Tubs




PostPosted: Sun Jul 09, 2006 12:06 pm   Post subject: (No subject)

it is in C, and they are large text files.
Andy




PostPosted: Sun Jul 09, 2006 12:14 pm   Post subject: (No subject)

its impossible. you're assuming your program can be psychic.. how can you know there are no more numbers left if you dont scan through it?

give us some more info, what is ths program for, and how is the text file generated
r.3volved




PostPosted: Sun Jul 09, 2006 2:52 pm   Post subject: (No subject)

you WILL have to parse the entire file to determine the numbers from the strings...

I suppose you could grab every line and determine if it's a number based on it's ascii code, or go character by character.
It's a simple:
47 < asciiVal < 58

if it's a number, keep it
if it's a letter, ignore it

keep concatinating it until you hit a determined delimiter (spaces and carriage returns)

But as was mentioned, your program can not determine if there are any numbers left without finding the end of file.
Tubs




PostPosted: Sun Jul 09, 2006 3:53 pm   Post subject: (No subject)

more info:

the line to be read will always start with the same characters and be in the same format.

example:

blah
blah
Read this: 16 73 4
blah
Read this: 10 3920 2
blah

i am using this program to go through text files generated by a poker site to convert the hands into a single point based number to analyze the strength of the hands. would a sample txt file help?
r.3volved




PostPosted: Sun Jul 09, 2006 4:28 pm   Post subject: (No subject)

Sample text file always helps...
...at least 10-20 records would be nice

Does it have to be in C?
What compiler are you using?
Windows or Linux?
Tubs




PostPosted: Sun Jul 09, 2006 4:54 pm   Post subject: (No subject)

r.3volved wrote:
Sample text file always helps...
...at least 10-20 records would be nice

Does it have to be in C?
What compiler are you using?
Windows or Linux?


sample text is attached.
line to be read: Dealt to Bewmar [ %c%c %c%c ] reading four characters (two numbers and two suits)

Dev C++ in windows



Table 108026.txt
 Description:

Download
 Filename:  Table 108026.txt
 Filesize:  206.25 KB
 Downloaded:  191 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sun Jul 09, 2006 5:45 pm   Post subject: (No subject)

I would highly advise that you develop a program which can parse all of this information, and store it in some kind of data structure. This is possible in C, but you would find it easier in other languages.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: