|
-
Nov 6th, 2001, 08:07 PM
#1
Thread Starter
Frenzied Member
Reading from a file
I need to read from a text file. It is the html of a search engines results page, so its a lot of html
what would be the best way to load it and be able to take parts out etc (im stripping the links out)? I don't think loading it into a huge variable would be very effiecient.
-
Nov 6th, 2001, 08:48 PM
#2
Lively Member
file reading
Hi Steve,
You'll need to directly access the text file, read a chunk of it and store it in the temporary value then check if the chunk has a link in it if so then store it to where ever you need to store the link to.
You'll need to open the file using fstream.h, use a for statement to keep reading until EOF. You'll be able to identify what a link is because A "href=" will be in front of the link.
I hope this helps.
Best Regards,
Ben Smith.
-
Nov 6th, 2001, 11:45 PM
#3
Thread Starter
Frenzied Member
so how large of sections of the file do you think I should take at a time?
Thanks
-
Nov 7th, 2001, 06:48 AM
#4
About 1 MB maybe? It really depends. If you allocate the buffer on the stack you shouldn't use more than 500 k because the stack is only 1 mb large. I usually read in the whole file no matter how large it is. You can also learn about VirtualAlloc or file mappings. Both should help you.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 7th, 2001, 12:34 PM
#5
Monday Morning Lunatic
Re: file reading
Originally posted by Smithy006
You'll need to open the file using fstream.h, use a for statement to keep reading until EOF. You'll be able to identify what a link is because A "href=" will be in front of the link.
Actually, fstream on its own.
fstream.h is part of the old iostreams library and is officially deprecated.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 7th, 2001, 02:37 PM
#6
fstream is just nasty.
Z.
-
Nov 7th, 2001, 03:03 PM
#7
Thread Starter
Frenzied Member
So then what should I use to read my file?
-
Nov 7th, 2001, 03:24 PM
#8
PowerPoster
You can also use the C, FILE, class and other functions to work with the files.
Here is an example:
PHP Code:
FILE *myfile;
char c[5];
myfile = fopen("myfile", "r");
do {
c = fgetc (myfile);
count<<c;
} while (c != EOF);
fclose(myfile);
-
Nov 7th, 2001, 03:50 PM
#9
Code:
#include <stdio.h>
// I just chose 8193 because 8192 byes are sector size on my work box
void main(void){
char big[8193];
FILE *in;
in = fopen("myfile","r");
while (!feof(in)) {
if ( fgets(big,sizeof(big),in) != NULL{
process(big);
}
}
}
void process(char *t){
// do stuff to your string here
}
-
Nov 7th, 2001, 04:12 PM
#10
Monday Morning Lunatic
Although you should really use iostreams for I/O in C++...otherwise you're using C.
Just get Kedaman in here, he's been straightening me out on the whole "get your act together and write OOP C++ rather than just bunging things into classes" (my words, not his )
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 7th, 2001, 05:14 PM
#11
I refuse to write slow C++ code, when I can just as easily write fast C code inside of a class (if that makes any sense =). fstream would require lots of fun looping stuff to write lots of data, while C style File IO can spit it out a single call (most cases).
Z.
-
Nov 7th, 2001, 05:17 PM
#12
Monday Morning Lunatic
Originally posted by Zaei
I refuse to write slow C++ code, when I can just as easily write fast C code inside of a class (if that makes any sense =). fstream would require lots of fun looping stuff to write lots of data, while C style File IO can spit it out a single call (most cases).
Z.
Umm...iostreams aren't limited to >> and <<. You can still use .read and .write I think, or something similar.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 7th, 2001, 05:33 PM
#13
Yeah, you can (now that I take the time to look), but I find it a WHOLE lot easier to use C Style functions (guess its just an opinion thing).
Z.
-
Nov 7th, 2001, 05:41 PM
#14
Monday Morning Lunatic
Yeah, nobody's stopping you - everyone can make their own choices 
Although admittedly, if I'm going for size I have a small wrapper class around CreateFile
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 7th, 2001, 10:45 PM
#15
Thread Starter
Frenzied Member
-
Nov 8th, 2001, 09:34 AM
#16
CreateFile is the official WinAPI way. Then there are the file stream classes, Ansi C file operations and Win16 OpenFile. I think it's just a matter of taste. CreateFile will probably be the fastest because the windows CRT and C++RT are just wrappers around API calls (ever looked at the CRT source of printf?)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|