Results 1 to 16 of 16

Thread: Reading from a file

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    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.

  2. #2
    Lively Member
    Join Date
    Oct 1999
    Location
    East Ballina,NSW,Australia
    Posts
    121

    Smile 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.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    so how large of sections of the file do you think I should take at a time?

    Thanks

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169

    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

  6. #6
    Zaei
    Guest
    fstream is just nasty.

    Z.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    So then what should I use to read my file?

  8. #8
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    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 {
          
    fgetc (myfile);
          
    count<<c;
        } while (
    != EOF);

    fclose(myfile); 
    Baaaaaaaaah

  9. #9
    jim mcnamara
    Guest
    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
    }

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  11. #11
    Zaei
    Guest
    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.

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  13. #13
    Zaei
    Guest
    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.

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    jim I got your code to work in a console, but in my windows program it keeps crashing.

    Im using URLDownloadToFile....maybe it can't make the file quick enough before it reads it???

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width