Finding word in file

This is a discussion on Finding word in file within the C Programming forums, part of the General Programming Boards category; Suppose I have list in a text file formatted in such a way where the list is numbered 1A, 1B, ...

  1. #1
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Finding word in file

    Suppose I have list in a text file formatted in such a way where the list is numbered 1A, 1B, 1C, 2A, 2B, ...:

    Code:
    *
    *
    *
    
    7C
    Guy
    Fawkes
    
    *
    *
    *
    What would be the most efficient way to find and replace the first and last names with given values?

    Here's my attempt:

    Code:
    int assign ()
    {
    	char ch, str[3];
    	int j, number1, number2, redo = 0;
    
    	while (redo == 0)
    	{
            printf("\nInput seat number: ");
            scanf("%d", &number1);
            scanf("%c", &ch);
            getchar();
    
            if (number1 <= 30)
            {
                switch (ch)
                {
                    case 'a':
                    case 'A':
                    number2 = number1; redo = 1; break;
    
                    case 'b':
                    case 'B':
                    number2 = number1 + 10; redo = 1; break;
    
                    case 'c':
                    case 'C':
                    number2 = number1 + 20; redo = 1; break;
    
                    default:
                    printf("Invalid seat number.\n");break;
                }
            }
    
            else
    		printf("Invalid seat number.\n");
    	}
    
        if (seat[number2].flag == 0)
        {
            printf("Insert first name: ");
            gets(seat[number2].fname);
            printf("Insert last name: ");
            gets(seat[number2].lname);
            sprintf(seat[number2].no, "%d%c", number1, ch);
            seat[number2].flag = 1;
            printf("\n");
    
            if (fgets(str, 3, f) == seat[number2].no)
            {
                fseek(f, +1L, SEEK_CUR);
                fputs(seat[number2].fname, f);
                fputc('\n', f);
                fputs(seat[number2].lname, f);
            }
        }
    
        else
        printf("This seat has already been assigned.\n");
    
        return 0;
    }
    The fgets part doesn't seem to work.
    Last edited by 843; 12-03-2010 at 03:48 AM.

  2. #2
    Registered User g8ortech14's Avatar
    Join Date
    Oct 2007
    Location
    Orlando FL
    Posts
    22
    Quote Originally Posted by 843 View Post
    Suppose I have list in a text file formatted in such a way where the list is numbered 1A, 1B, 1C, 2A, 2B, ...:

    Code:
    *
    *
    *
    
    7C
    Guy Fawkes
    
    *
    *
    *
    What would be the most efficient way to find and extract the first and last names into variables?
    You are talking about strings. Characters and words to strings. The problem with that is that you would be writing them to an Array meaning that the array would have to contain all of the same data types. If you are using data that is of different types then using a struct with pointers may be more beneficial.

  3. #3
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Yeah, I'm using a struct. Sorry, just edited my post.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, you have a text file, and you want to replace Guy Fawkes with some other name.

    How big is the text file? How many rows of text do you expect it to have, max?

  5. #5
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    30 seats, 10 rows and 3 columns. The file first lists the sequence from 1A to 10A, then 1B to 10B and finally 1C to 10C. Upon initialization, the names would simply be "Unassigned", but it will be replaced by names like "Guy Fawkes". While can be done in one line, I don't know how to parse a string, so I split it into two. Even if I know how to parse, I have to account for people with multiple last names or the likes, so I suppose this way is easier. Having two lines of "Unassigned" wouldn't make sense, and listing "First name: " beforehand complicates things a lot given my limited coding skills.

    It looks like this:

    Code:
    1A
    Unassigned
    
    2A
    Unassigned
    
    3A
    Unassigned
    
    4A
    Unassigned
    
    5A
    Unassigned
    
    6A
    Unassigned
    
    7A
    Unassigned
    
    *
    *
    *

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So an array of structs would be ideal. Each struct has a seat designation, and a name (or Unassigned), given to it.

    Have you prototyped the struct above main() yet? That's a very convenient way to handle that.

    Then you can read in your data into the array of structs, make your changes as needed, and write out the new data to a temp.txt file. Then close the files, remove the old file, and name the temp.txt file, into the original text file name.

    I know this sounds insanely difficult, but it's way easy. Harder to describe it than to do it.

    You can always change the data right inside the file, but then you run a bit more risk of goofing up the file.

  7. #7
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    It's already in an array of structs. My problem is not with data handling, but more of text manipulation in the file. I can post the full code (about 400 lines) here if you wish, but it's nowhere near complete yet.

    Here's the structure identification:

    Code:
    struct info{
    	char no[3];
    	char fname[20];
    	char lname[20];
            int flag;
    	};
    The flag is used to tell if the seat is assigned.
    Last edited by 843; 12-03-2010 at 04:44 AM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Text manipulation?

    Please specify in detail.

  9. #9
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    I would like the program to search the file for a specific seat (which is user-defined), then replacing the "Unassigned" string into first and last names, which may or may not be split into two lines.

    Another function would be reading the file and storing all those info into internal memory (the struct elements).

    Finally, I need to find a way to flag the assignment on the external file. Perhaps if (fgets != "Unassigned") may work, but I haven't implemented it yet.

    The code attempt is in my first post.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to make up your mind about the names - two lines is easiest, but either can work. Code to handle one line, won't work for two lines, however.

    For this small amount of data a simple sequential search is fine:

    Code:
    /* be sure to take in the seat number, as a string, NOT as an integer */
    for(i=0;i<SEATS;i++) {
      if(targetSeat==array[i].no) {
         //found the seat number
         strcpy(array[i].fname, newfName); 
         //handle last name as needed
      }
    }
    What has you stumped with reading in the data from the file?

    You can use a simple format like:

    Code:
    fscanf(yourFilePointer, "%s%*c", array[i].no); //etc.
    for each struct member. Again, you have to have the format of the names (one line or two), decided on, and adjust the code for that.

  11. #11
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Ah, I'm an idiot to loop through the actual text file instead of the arrays. This should make things easier. Thanks for the help.
    Last edited by 843; 12-03-2010 at 05:54 AM.

  12. #12
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Could anyone please tell me what's wrong with this code? The output for 10A, 10B, and 10C is somehow missing its seat[10].fname string. I've narrowed the culprit down to the following code.

    Code:
        for (i = 1; i <= 30; i++)
        {
            if (i <= 10)
            {sprintf(seat[i].no, "%dA", i);
            printf("%s%d", seat[10].fname, i); // the string still holds here
            printf("%s%d", seat[10].lname, i);}
    
            else if (i <= 20)
            sprintf(seat[i].no, "%dB", i - 10);
    
            else if (i <= 30)
            sprintf(seat[i].no, "%dC", i - 20);
        }
    If I changed the code to

    Code:
        for (i = 1; i <= 30; i++)
        {
            if (i <= 10)
            sprintf(seat[i].no, "%dA", i);
    
            else if (i <= 20)
            {sprintf(seat[i].no, "%dB", i - 10);
            printf("%s%d", seat[10].fname, i); //string disappears!
            printf("%s%d", seat[10].lname, i);}
    
            else if (i <= 30)
            sprintf(seat[i].no, "%dC", i - 20);
        }
    EDIT: Got it! I forgot about '\0' in the declaration.
    Last edited by 843; 12-03-2010 at 11:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 05:44 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21