Thread: Sort by Character Length, Text File

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    6

    Sort by Character Length, Text File

    Here is what I want to do. I want to read in a text file, and copy all certain character length words into a new text file. Such as, I want to copy all 10 character length words from 1.txt, into 2.txt.

    How would I go about the specificity part - the "only" 10 character length detail, and copy it elsewhere?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Read word by word into a sufficiently sized buffer using some input functions then compare the length of that word to your test criteria... write to the output file if it passes the criteria.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by glj12 View Post
    Here is what I want to do. I want to read in a text file, and copy all certain character length words into a new text file. Such as, I want to copy all 10 character length words from 1.txt, into 2.txt.

    How would I go about the specificity part - the "only" 10 character length detail, and copy it elsewhere?
    Some pseudo-code idea's:

    Code:
    initialize two file pointers
    open one file for reading, and the other for writing in text mode
    while((int c = getchar()) != EOF)  /* a tricky bit of code for noobs, perhaps */
       read in each letter and save it to the end of a char Array[]
       if the letter is a space, comma, period, semi-colon, newline  or new page char
           then the letters constitute a word, if they're not hyphenated.
       add the end of string marker to the Array[], overwriting the punctuation/editing            
       commands in the file. to make your collection of letters, a string, in C.   
       
       use len(word) to tell you how long each word is
    
       If the length == 10
          write out the word to your desired file. (which I might call 10letters.txt instead of 2.txt)
    That should get you started.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    while((int c = getchar()) != EOF)  /* a tricky bit of code for noobs, perhaps */
    Assuming you meant that to be read code, it's C99 and not C89.

    Besides, when processing words, it might be easier to read in whole lines at once. Who knows.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    This is what I have so far, just the only part I was unsure about was the copy the string if and only if it is 10 characters long. As of yet, I have it just copy every string over.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MaxStringLength 512
    
    int main( int argc, char **argv )
    {
    	FILE	*fileFrom, *fileTo;
    	char	fileNameFrom[MaxStringLength+1], fileNameTo[MaxStringLength+1];
    	int 	holdingTank;
    		
    	
    	printf( "Enter the name of a file to scan and sort from: " );
    	fgets( fileNameFrom, sizeof( fileNameFrom ), stdin );
    	if( fileNameFrom[strlen( fileNameFrom ) - 1] == '\n' )
    		fileNameFrom[strlen( fileNameFrom ) - 1] = '\0';
    	else
    		while( getchar() != '\n' );
    	
    	fileFrom = fopen( fileNameFrom, "r" );
    	
    	if( fileNameFrom == NULL )
    	{
    		printf( "Unable to open file: %s\n", fileNameFrom );
    		exit( 1 );
    	}
    	
    	printf( "Enter the name of a file to copy the data   to: " );
    	fgets( fileNameTo, sizeof( fileNameTo ), stdin );
    	if( fileNameTo[strlen( fileNameTo ) - 1] == '\n' )
    		fileNameTo[strlen( fileNameTo ) - 1] = '\0';
    	else
    		while( getchar() != '\n' );
    	
    	fileTo = fopen( fileNameTo, "w" );
    
    	while( 1 )
    	{
    		holdingTank = fgetc( fileFrom );
    		if( holdingTank == EOF || holdingTank == 0 )
    			break;
    
    		
    		fprintf( fileTo, "%c", holdingTank );
    	
    	}
    	
    	fclose( fileFrom );
    	fclose( fileTo );
    	
    	return( 0 );
    }
    Just a little bit confused with the copy only string size of 10 over to a new file. Though, thanks for all the help so far!

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    	while( 1 )
    	{
    		holdingTank = fgetc( fileFrom );
    		if( holdingTank == EOF || holdingTank == 0 )
    			break;
    
    		
    		fprintf( fileTo, "&#37;c", holdingTank );
    	
    	}
    See, the trouble is that you're only reading one character at a time. Either you stick the characters one by one into an array which represents a word, or use fgets() and read one line at a time.

    [edit] For example:
    Code:
    #include <ctype.h>  /* for isspace() */
    
    char word[BUFSIZ];
    int c, position = 0;
    
    for(;;) {  /* same as while(1) */
        c = getc(fp);
        if(isspace(c)) {
            word[position] = '\0';
            if(position == 10) puts(word);
            position = 0;
        }
        else word[position++] = c;
    }
    Your code is very nice, BTW. Very nice. You could separate the code to read a string into a function, however. [/edit]
    Last edited by dwks; 01-23-2008 at 05:18 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    *Edit*

    Just saw the rest of your post. Thanks for the compliment.

    Oh, so the code that you wrote, this replaces the majority of my while loop, right? *reads the code through a bit more thoroughly*
    Last edited by glj12; 01-23-2008 at 05:22 PM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Er, not exactly. See my edit.

    Actually, since you're processing words, fscanf() wouldn't be a bad idea . . .
    Code:
    char word[BUFSIZ];
    
    while(fscanf(fp, "&#37;s", word) == 1) {
        if(strlen(word) == 10) puts(word);
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    Maybe I just overcomplicated things a little bit in my head, but from that point at which we gathered all of the 10 char strings and placed it into word, how would I tell it to place it into the file as stated before? Would be along the lines of fprintf( fileTo, "&#37;s", word );

    Or did I miss the point. It's been a while since I have written anything in C.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Oh, sure, that would work. I just used puts(), which is the same as printf() except puts() appends a '\n'. And puts() doesn't support things like &#37;s, of course. In other words, the following are all equivalent:
    Code:
    const char *string = "Hello, World!";
    puts(string);
    printf("%s\n", string);
    fprintf(stdout, "%s\n", string);
    Of course, if you wanted to use a file instead of the screen, you'd pass that file pointer to fprintf() instead of stdout.

    You could also use fputs(). (Unlike puts(), it doesn't append a newline.)
    Code:
    fputs(string, stdout);
    Your terminology is a bit off:
    from that point at which we gathered all of the 10 char strings and placed it into word
    A char is a single character like 'a', which is enclosed in single quotes, if you want to specify it by itself. A char array is an array of chars. (Duh.) A string is an array of chars that is terminated with a '\0' character. A string literal, enclosed in double quotes in a C or C++ program, like "Hello, World!", automatically gets a '\0' appended to the end.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    Thanks for the correction.

    But for this part: while(fscanf(fp, "&#37;s", word) == 1)

    what does the fp stand for?

    Also, so I can simply write what I want to do as such? :

    Code:
    while(fscanf(fp, "%s", word) == 1)
    	{
        		if(strlen(word) == 32) puts(word);
                    fprintf( fileTo, "%s\n", word );
    	}
    Last edited by glj12; 01-23-2008 at 05:43 PM.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    fp stands for File Pointer. It's the standard name for a quick-and-dirty example which uses FILE* variables.

    No, no. You'd use something like this.
    Code:
    while(fscanf(fileFrom, "&#37;s", word) == 1)
    	{
        		if(strlen(word) == 32) {
                        fprintf( fileTo, "%s\n", word );
    		}
    	}
    You want to replace the puts(), which prints something to the screen, with your fprintf/fputs, which prints something to a file.

    You should get into the habit of look up functions yourself. If you go to cppreference.com or cplusplus.com or even google (try searching for "c function fprintf", for example), you should be able to find answers to questions like "what does fp stand for?"
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    My bad, sorry about that, definitely was a bad copy and paste on my part. Meant to leave out the puts(). Either way, finally got it, thank you very much!

    Oh, and for the decent looking code I had written earlier in the post (from a while back, mind you) you can thank my Professor for that - the guy who designed ink well for the Apple Newton.

    Much appreciated! And I will make use of those links next time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM