Thread: Organize words from a text file

  1. #1
    carpe diem
    Join Date
    Jan 2010
    Posts
    46

    Organize words from a text file

    Hi,
    I am trying to create a code that will find words and organize them in a text file in the following manner:

    For the following input text file:
    Code:
    [PARAMETER: Left Pedal]
        Mnemonic="Left Pedal";
        Operational check=1;
        Word=232;
    [PARAMETER: Right Pedal]
        Mnemonic="Right Pedal";
        Operational check=1;
        Word=232;
    [PARAMETER: Character 1]
        Mnemonic="Character 1";
        Operational check=1;
        Word=192;
    [PARAMETER: Size]
        Mnemonic="Size";
        Decimal places=0;
        Word=192;
    I would like the output to be the following:
    Code:
    Character 1  192
    Size              192
    Left Pedal     232
    Right Pedal   232
    So the code extracted all Mnemonic names from the input file and their word number. It proceeded to organize them in order according to their word number.

    Here is what I have so far:

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
     
    #define MAX_LEN_LINE   300   // ?
    
     
    int main()
    {
    	const char mnemonic[80] = "Mnemonic=";
    	char buffer[MAX_LEN_LINE+2];
        char *buff_ptr, *find_ptr;
    	fpos_t position;
        FILE *fp1, *fp2;
        size_t find_len = strlen(mnemonic);
     
        fp1 = fopen("originalfile.txt","r");
        fp2 = fopen("filecreated.txt","w");
     
        while(fgets(buffer,MAX_LEN_LINE+2,fp1))
        {
            buff_ptr = buffer;
            while ((find_ptr = strstr(buff_ptr,mnemonic)))
            {
    			fgetpos (fp1, &position);
    			char avoid[]=";";
    			int i;
    			i = strcspn (buff_ptr,avoid);
    			fsetpos (fp1, &position);
    
    			fgets ( buffer, (i-2), fp1 );
    
                while(buff_ptr < find_ptr)
                fputc((int)*buff_ptr++,fp2);
    			fputs("\n",fp2);
                fputs(buffer,fp2);
     
                buff_ptr += find_len;
            }
            
        }
     
        fclose(fp2);
        fclose(fp1);
     
        return 0;
    }
    The code above was supposed to extract all parameter names from the input, but for some reason it prints the line proceeding the one where the Mnemonic is found and cuts words.

    Here's my output:
    Code:
        
        Operational check=    
        Operational check=1    
        Operational check=1    
        Decimal plac
    Can anyone please point me in the right direction?
    Last edited by doia; 05-19-2010 at 12:48 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    while ((find_ptr = strstr(buff_ptr,mnemonic)))
    {
        fgetpos (fp1, &position);
        char avoid[]=";";
        int i;
        i = strcspn (buff_ptr,avoid);
        fsetpos (fp1, &position);
    
        fgets ( buffer, (i-2), fp1 );
    As written, the two lines highlighted do nothing. The position saved by the fgetpos function gets the current file position after the line containing "Mnemonic" is read from the file. The fsetpos then does nothing because the current position has not changed at all in between calls. The fgets call then reads the next line after the "Mnemonic" line which is why you are seeing the results you mention.
    "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
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    Thanks for the help hk_mp5kpdw.

    After some struggle I managed to fix it. Here's the corrected code:

    Code:
     
        while(fgets(buffer,MAX_LEN_LINE+2,fp1))
        {
            buff_ptr = buffer;
    		fgetpos (fp1, &position2);
    		while ((find_ptr = strstr(buff_ptr,mnemonic)))
            {
    			i = strcspn (buff_ptr,avoid);
    			i=i+2;// i starts from zero, don't include equal sign
    			position1 = position1 + i;
    			fsetpos (fp1, &position1);
    			i = position2 - position1 - 3;// 3 for characters after the name, including end of line
    			fgets ( buffer, i, fp1 );
    			fputs("\n",fp2);
                fputs(buffer,fp2);
    			
                buff_ptr += find_len;
    			
    	}
            fgetpos (fp1, &position1);
        }
    Now my output is starting to look correct. But I still need to arrange it so that it gets the word numbers and then proceeds to arrange them in the right order...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
            while ((find_ptr = strstr(buff_ptr,mnemonic)))
            {
    			fgetpos (fp1, &position);
    			char avoid[]=";";
    			int i;
    			i = strcspn (buff_ptr,avoid);
    			fsetpos (fp1, &position);
    
    			fgets ( buffer, (i-2), fp1 );
    All of this fgetpos/fsetpos and calling fgets again is totally unnecessary.

    > Mnemonic="Left Pedal";
    fgets() will have stored this whole line in buffer to begin with.
    Identify "Mnemonic" advance past the =" and scan up to the next "
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    Salem,
    are you suggesting I go back to my original code?
    and what should I use to advance past Mnemonic= to the next " ? memmove in conjuction with a strchr?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    while(fgets(buffer,MAX_LEN_LINE+2,fp1)) {
        if ( strncmp( buffer, mnemonic, find_len ) == 0 ) {
            char *p = buffer + find_len;  // p points to ="Left Pedal";
        }
    }
    Once you've got p set-up, there are many things you could do.
    strchr() and strrchr() to find the first and last double quotes for example.



    You can even do the whole thing in one rather complex sscanf call.
    Code:
    while(fgets(buffer,MAX_LEN_LINE+2,fp1)) {
        char field[MAX_LEN_LINE];
        if ( sscanf( buffer, "Mnemonic=\"%[^\"]", field ) == 1 ) {
            // field contains the string between ""
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    Thanks Salem! I figured it would be better to acquire the PARAMETER name instead of the Mnemonic. The code looks a lot cleaner now:

    Code:
    while ((find_ptr = strstr(buff_ptr,mnemonic)))
            {
    	        char field[MAX_LEN_LINE];
    		if (sscanf(buff_ptr, "  [PARAMETER: %[^]]", field)==1)
    		{
    			fputs("\n",fp2);
                            fputs(field,fp2);
    		}
                    buff_ptr += find_len;
    			
    			
            }
    Last edited by doia; 05-20-2010 at 01:24 PM. Reason: fixed it

  8. #8
    Registered User yohannes's Avatar
    Join Date
    Jun 2010
    Posts
    11
    Hei doia. I'm newbie in C programming too. And I have a similar problem with you. I want to compare strings in a text file, make another text file, copy what I got in the first file and add more info about it. I tried your code, but I found that there's segmentation fault (core dumped) error. I used gcc in cygwin to compile your code. I dunno what's wrong with your code. Have you successfully made the program that can organize words? can you please post your complete working code. I want to compare it to my code. thanks before!

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    @yohannes
    organize words
    Post YOUR attempt on your other thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to replace words in a text file with seekp
    By ishould in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2009, 06:17 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  5. Function is called and I am trying to open a file
    By tommy69 in forum C Programming
    Replies: 88
    Last Post: 05-06-2004, 08:33 AM