Thread: Taking and skipping chars from files

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Taking and skipping chars from files

    What I'm trying to do it open up a large text file, and remake it elsewhere without spaces, but I'm only getting a fraction of my file. Here's my code :

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <ctype.h>
    
    ifstream fin;
    ofstream fout;
    
    char cont[10001000];
    
    int main()
    {
        fin.open("C:\\windows\\system32\\shell32.dll", ios::nocreate | ios::in);
        fout.open("hello.txt", ios::out);
        for(long i = 0; i < 10000000; i++)
        {
            fin.get(cont[i]);
            while(isalnum(cont[i]))
            {
                fout << cont[i];
                i++;
            }
        }
        return 0;
    }

    I'm looping until one million, but I want to know if I can use something like fin.EOF and what's its syntax ? Then, of course, why do I only get 220 bytes from a 12.5 megabyte file ? There are much more alphanumerical characters in it than I'm getting.

    Thanks for any help.

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <ctype.h>
    
    //no need for that large of a buffer.  If buffers really have to be that big, consider dynamic strings.
    
    int main()
    {
    
        char c;
        ifstream fin;
        ofstream fout;
        fin.open("C:\\windows\\system32\\shell32.dll", ios::nocreate | ios::in);
        fout.open("hello.txt", ios::out);
        while(fin.get(c)) //this will keep going until it has no more chars to grab, i.e. EOF
        {
    	if(c!=' ') //if its not a space, go ahead and output it
    	    fout<<c;
        }
        fout.close(); fin.close(); //because its better
        return 0;
    }
    If all your trying to do is remove just spaces...

    oh and I believe the reason your getting only a fraction of your file is because that file is NOT filled with alphanumeric characters. Very few are...
    if you want to copy over all alphanumeric...
    Code:
    int main()
    {
    
        char c;
        ifstream fin;
        ofstream fout;
        fin.open("C:\\windows\\system32\\shell32.dll", ios::nocreate | ios::in);
        fout.open("hello.txt", ios::out);
        while(fin.get(c)) //this will keep going until it has no more chars to grab, i.e. EOF
        {
    	if(isalnum(c)) 
    	    fout<<c;
        }
        fout.close(); fin.close(); 
        return 0;
    }
    -LC
    Last edited by Lynux-Penguin; 07-23-2003 at 02:15 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Again, that gets me lots of spaces, and still, the file is only 2Kb long. I'm thinking that these spaces aren't really spaces, but characters with other ASCII values, no ? Using your code, I got :
    Code:
    MZ                @                                       	!
    L!ThisprogramcannotberuninDOSmode.
    
    $       ~FsFsFsFsGsPBsFs5vP	WsPPLsP
    (rP/GsPUGsP:sP-GsRichFs        PE  L ~4;        
     !    Na             i           
              ~                          ܤ      D^                  
    } ĝ %                                                @  @ 
                      .text   n                       `.data   H    
    x   
                 @  .rsrc   D^    ^               @  @.reloc    }    \|             @  B                                                                                                                                                                                                                                                                                                                  
                                                                           ( D X j 
    |             
      _  ~ l \ F 0      
       f J 4
    Last edited by Korhedron; 07-23-2003 at 02:18 PM.

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    try the last (edited) version of my post

    the characters you grabbed are not spaces but rather characters that have other values like '\n' '\t' etc. characters thar are below
    10 on the ASCII table.

    So you're right. If all you want to see is the alpha-numeric. Use isalnum()
    but the .dll file is loaded with JUNK that you're not meant to really read. you will get very little from it.
    Although, if all your doing is removing spaces... I wonder why its still so small...

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    perhapse this part will shed some light:
    Code:
    ThisprogramcannotberuninDOSmode.
    (at least the spaces were removed ^_^)

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  6. #6
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Yes, this also works in my alnum program, however, I simply don't understand why the file is so small. The file my alnum program outputs is fine, with only characters with lots of accents here and there, but alphanumeric none the less. I'm stuck as to why I don't get at least a megabyte of it. This is, after all, why I used system32.dll, because it's huge ( 12.5Mb of text ) and because most computers ( Windows ) have it.

  7. #7
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Salem,

    I have two questions:

    1- Do we have to write ios::in, I think it's default for ifstrem.

    2- Do we have to open in binary mode because the file was origionally created in binary mode?
    none...

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Thanks Salem, things are clearer now.
    none...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM