Thread: How would I get the next word in the list?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    308

    How would I get the next word in the list?

    Hi Adak,

    I have updated my syllogism program in the main and search functions.

    Now the search sends main a sentence in a file.
    Main asks if the syllogism was good, if not the sentence main read is written to bad.txt.
    Then the next time the program is run serach opens bad.txt and if it finds a match the statements are not run.
    This was the bad.txt doesn't fill up with duplicates.

    But I want the search function to give me the next word which will be written to a file where it will be opened my main and the cycle starts again.
    I haven't done this yet though, and I don't know how, that's why I'm posting this for an idea on how to get the next word in the list.

    Here is main:
    Code:
    /* the main program */ 
    int main ()
    {   
    	/* declaring and initiaizing variables */
    	FILE *read_to_review;
    	FILE *bad_list;
    	FILE *book;
    	FILE *a;
    	FILE *b;
    	FILE *c;
    	char * pch = malloc(300);
    	char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	char buffer[LINES];
    	char copy_review[REVIEW];
    	int period = 0;
    	int test_result = 0;
    	int x = 0;
    	int good = 0;
    	int one = 1;
    	char sentence[4096] = {0};
    	/* The function is called so I don't have to type getch */
    	atexit(MyExit);    
    
    	/* X recieves 1 so the program can run */
    	printf("Type 1 to run the program\n");
    	scanf("%d", &x);
    	
    	while(!good) 
    	{
    		switch(x)
    		{
    		case 1:
    			{
    				/* The sentence generator section is run*/
    				sentence_generator();
    
    
    				/* Now the syllogism section begins */
    
    				/* The function is called and creates the text file */
    				split_by_sentence();
    
    				/* The text file just created is opened */
    				book = fopen("readtext.txt", "r");
    				read_to_review = fopen("m_and_s.txt", "r");
    				bad_list = fopen("bad.txt", "a+");
    				if(!book)   
    				{
    					perror("Error: file readtext.txt was not found or opened");   
    					return 0;   
    				}
    
    				/* read from file */
    				while(fgets(buffer, sizeof(buffer), book)!=NULL)
    				{ 
    					/* join two sentences */
    
    					strcat(sentence, buffer);
    					strcat(sentence, " ");
    
    					/* counting the periods in the input text file*/
    
    					if (strstr(buffer, "."))
    					{
    						period++;
    					}
    
    					/* After two periods are counted the while loop is exited */
    
    					if(period > one)
    					{
    						/* I find the alphabet in pch after it's run through strtok */
    						Camestros_percentage_calculation_numbers(sentence);
    						Baroco_percentage_calculation_numbers(sentence);
    						Darii_percentage_calculation_numbers(sentence);
    						Ferio_percentage_calculation_numbers(sentence);
    						Bocardo_percentage_calculation_numbers(sentence);
    						Celarent_Celaront_Felapton_percentage_calculation_numbers(sentence);
    						Darapti_Barbari_Barbara_percentage_calculation_numbers(sentence);
    
    						/* The printf asks if the syllogism was fine, if not copy_review is written to bad.txt */
    						/* Then the search function opens the bad.txt and decides if it runs or not */
    
    						fgets(copy_review, sizeof(copy_review), read_to_review);
    						printf("Was the syllogism logical? 1 for yes, 0 for no: \n");
    						scanf("%d",&test_result);
    						if(test_result==0)
    							fprintf(bad_list, "%s\n", copy_review);
    						/* The printf asks if I want to keep running the program or close it */
    						printf("Close program? [1=yes, 0=no]: ");
    						scanf("%d",&good);
    						if(period > one)
    						{
    							memset(&sentence[0], 0, sizeof(sentence));
    						}
    						period = 0;
    						break;
    					}
    				}
    				fclose(book);
    				fclose(bad_list);
    				fclose(read_to_review);
    				/* emptying the files so the program doesn't build them up each time it's used */
    
    				a = fopen("readtext.txt", "w");
    				if(!a)   
    				{
    					perror("Error: file readtext.txt was not found or opened");   
    					return 0;   
    				}
    				b = fopen("readtext1.txt", "w");
    				if(!b)   
    				{
    					perror("Error: file readtext1.txt was not found or opened");   
    					return 0;   
    				}
    				c = fopen("writelist.txt", "w");
    				if(!c)   
    				{
    					perror("Error: file writelist.txt was not found or opened");   
    					return 0;   
    				}
    				fprintf(a, "");
    				fprintf(b, "");
    				fprintf(c, "");
    				fclose(a);
    				fclose(b);
    				fclose(c);
    			}
    		}
    	}
    
    	return 0;
    }
    Here is search:
    Code:
    void search(char *src, char *a, char *b, char *c)
    {
    	FILE *sp;
    	FILE *read_bad;
    	FILE *write;
    	FILE *backup;
    	int number = 0;
    	// int n = 0; // This variable is used for the switch statement.
    	char used[300] = {0};
    	char byte [4] = {0};
    	char *filedata = malloc(300);
    	char *list_of_bad = malloc(300);
    	char *copy = malloc(500);
    	sp = fopen("readfile.txt", "r");
    	if(!sp)
    	{
    		perror("Error: file readlist.txt was not found or opened");
    		return;
    	}
    	backup = fopen("backup.txt", "w");
    	write = fopen("m_and_s.txt", "w");
    	read_bad = fopen("bad.txt", "r");
    	delete_char(c, '\n', 0);
    	// The printf and scanf are for the switch statement code.
    	/*printf("\nEnter a result number\n");
    	scanf("%d", &n);*/
    	while(fgets(filedata, 4096, sp)!=NULL)
    	{
    		byte[0] = filedata[0];
    
    		// I look at the first letter in the word from the file
    		// and compare it to a, and the word from the file must also must have b in it somewhere.
    
    		if( (!strncmp (byte, a, 1)) && (strpbrk(filedata, b)) )
    		{
    			number++;
    
    			while(number < 18)
    			{
    				if(number==17)
    					fprintf(backup, "%s\n", filedata);
    				break;
    			}
    			// I commented out the switch statement. The switch helped me tweak the while loop below number++.
    			/*switch(n)
    			{
    
    
    			case 1:
    			{
    			while(number < 19)
    			{
    			if(number==18)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 2:
    			{
    			while(number < 20)
    			{
    			if(number==19)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 3:
    			{
    			while(number < 21)
    			{
    			if(number==20)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 4:
    			{
    			while(number < 22)
    			{
    			if(number==21)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 5:
    			{
    			while(number < 23)
    			{
    			if(number==22)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 6:
    			{
    			while(number < 24)
    			{
    			if(number==23)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 7:
    			{
    			while(number < 25)
    			{
    			if(number==24)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 8:
    			{
    			while(number < 26)
    			{
    			if(number==25)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 9:
    			{*/
    
    			if(number==80)
    			{
    				/* The bad.txt is opened and checked it the filedata and c are being paired */
    				/* if they are being paired then the statements are not run */
    				while(fgets(list_of_bad, 4096, read_bad)!=NULL)
    				{
    
    					if(((strstr(list_of_bad, c)) && (strstr(list_of_bad, filedata)))!=1)
    					{
    						if(filedata != NULL)
    						{
    							strcat(src, filedata);
    							delete_char(c, '\n', 0);
    							delete_char(filedata, '\n', 0);
    							fprintf(write, "%s %s\n", c, filedata);
    							memmove (copy, filedata, strlen(filedata)+1);
    							strcat(c, " ");
    							strcat(c, copy);
    							strcat(c, "\n");
    							while(fgets(list_of_bad, 4096, read_bad)!=NULL)
    								if(strcmp(copy,list_of_bad))
    									printf("found one\n");
    						}
    						break;
    					}
    					/* If the statements above are not run then this message is printed. */
    					/* i want this to give the next word in the sp list though, but i don't know how to do that */
    
    					else
    					{
    						printf("sorry, that is wrong.");
    						break;
    					}
    				}
    			}
    
    
    			/*while(number < 38)
    			{
    			if(number==37)
    			strcat(src, filedata);
    			else if(filedata == NULL)
    			{
    			fgets(filedata, 4096, sp);
    			strcat(src, filedata);
    			}
    			break;
    			}*/
    			/*}
    			break;
    
    			default:
    			{
    			while(number < 5)
    			{
    			if(number==4)
    			strcat(src, filedata);
    			break;
    			}
    			}
    
    
    			}*/
    		}
    
    	}
    	fclose(read_bad);
    	fclose(write);
    	fclose(backup);
    	fclose(sp);
    	return;
    }
    I've been at this for a while. I've googled and read about incrementing pointers and tried that:
    Code:
    *++filedata;
    But that just removed a letter when I tested with printf.
    So I'm stuck and need some help.

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    Here is my solution. It's not really the next in the list it's the previous in the list, which is just the same for my purposes in this program.

    Here is the solution code:
    Last edited by jeremy duncan; 12-13-2011 at 10:03 PM. Reason: removed the bad code

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Hey Jeremy! Got it fixed already.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    No it's not fixed. i am trying something else. I will post my new question in a bit after the code is easier to understand.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Location
    Seattle area
    Posts
    4
    The function "search" wasn't called from anywhere in the program above it. It looks way overcomplicated and not to be something you should add features to unless you've got it working and thoroughly tested that it does what it's supposed to do so far. (What it's supposed to do confuses me, because what you described in English seemed to be only that it's supposed to check whether there are any copies of a given sentence in a text file, which shouldn't require a function nearly that long.)

    By the way, I might be wrong on this, but according to what I've read I think you're supposed to add calls of free(arrayname) at the end, in reverse order of the corresponding calls to *arrayname = malloc(bytes), so that your program's runtime memory space doesn't go up by (bytes + bytes + bytes) times number of calls of search().

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    Well this code I made solved my problem for sure. It gives me a syllogism result if there is no same sentence in the bad file.

    The search function is part of a different bigger function that is thousands of lines long.

    Where did you see free should be used? In the search function and the main too? I will have a look at that after I get through updating my code a bit more.

    Here is the functional code I made.

    Code:
    /* The function to search text file for compatible word */
    
    void search(char *src, char *a, char *b, char *c)
    {
    	FILE *sp;
    	FILE *read_bad;
    	FILE *write;
    	int number = 0;
    	// int n = 0; // This variable is used for the switch statement.
    	char used[300] = {0};
    	char byte [4] = {0};
    	char *filedata = malloc(300);
    	char *backup = malloc(300);
    	char c_plus[C_PLUS] = {0};
    	char c_plus_plus[C_PLUS_PLUS] = {0};
    	char *list_of_bad = malloc(300);
    	char *copy = malloc(500);
    	int result = 0;
    	int result_two = 0;
    	sp = fopen("readfile.txt", "r");
    	if(!sp)
    	{
    		perror("Error: file readlist.txt was not found or opened");
    		return;
    	}
    	write = fopen("m_and_s.txt", "w");
    	read_bad = fopen("bad.txt", "r");
    	delete_char(c, '\n', 0);
    	// The printf and scanf are for the switch statement code.
    	/*printf("\nEnter a result number\n");
    	scanf("%d", &n);*/
    	while(fgets(filedata, 4096, sp)!=NULL)
    	{
    		byte[0] = filedata[0];
    
    		// I look at the first letter in the word from the file
    		// and compare it to a, and the word from the file must also must have b in it somewhere.
    
    		if( (!strncmp (byte, a, 1)) && (strpbrk(filedata, b)) )
    		{
    			number++;
    
    			if(number==70)
    					strcpy(backup, filedata);
    
    			// I commented out the switch statement. The switch helped me tweak the while loop below number++.
    			/*switch(n)
    			{
    
    
    			case 1:
    			{
    			while(number < 19)
    			{
    			if(number==18)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 2:
    			{
    			while(number < 20)
    			{
    			if(number==19)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 3:
    			{
    			while(number < 21)
    			{
    			if(number==20)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 4:
    			{
    			while(number < 22)
    			{
    			if(number==21)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 5:
    			{
    			while(number < 23)
    			{
    			if(number==22)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 6:
    			{
    			while(number < 24)
    			{
    			if(number==23)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 7:
    			{
    			while(number < 25)
    			{
    			if(number==24)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 8:
    			{
    			while(number < 26)
    			{
    			if(number==25)
    			strcat(src, filedata);
    			break;
    			}
    			}
    			break;
    
    			case 9:
    			{*/
    
    			if(number==80)
    			{
    				memmove(c_plus, c, strlen(c)+1);
    				strcat(c_plus, " ");
    				strcat(c_plus, filedata);
    
    				memmove(c_plus_plus, c, strlen(c)+1);
    				strcat(c_plus_plus, " ");
    				strcat(c_plus_plus, backup);
    				
    				
    				/* The bad.txt is opened and checked it the filedata and c are being paired */
    				/* if they are being paired then the statements are not run */
    				while(fgets(list_of_bad, 4096, read_bad)!=NULL)
    				{
    					if(!strcoll(c_plus, list_of_bad))
    					{
    						result++;
    					}
    					else if(!strcoll(c_plus_plus, list_of_bad))
    					{
    						result_two++;
    					}
    				}
    				if(result == 0)
    					{
    						strcat(src, filedata);
    						fprintf(write, "%s %s\n", c, filedata);
    					}
    					else if(result_two == 0)
    					{
    
    						strcat(src, backup);
    						fprintf(write, "%s %s\n", c, backup);
    					}
    			}
    
    
    			/*while(number < 38)
    			{
    			if(number==37)
    			strcat(src, filedata);
    			else if(filedata == NULL)
    			{
    			fgets(filedata, 4096, sp);
    			strcat(src, filedata);
    			}
    			break;
    			}*/
    			/*}
    			break;
    
    			default:
    			{
    			while(number < 5)
    			{
    			if(number==4)
    			strcat(src, filedata);
    			break;
    			}
    			}
    
    
    			}*/
    		}
    
    	}
    	fclose(read_bad);
    	fclose(write);
    	fclose(sp);
    	return;
    }
    What do you think?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Everything you malloc() or calloc() should be free(nameOfArray), when it's no longer being used. As suggested above, you delete a 2D array, in the reverse order it was allocated:

    Code:
    free(arrayName);                                //free's the first dimension 
    for(i=0;i<NumberOfRowsInArray;i++)  
       free(arrayName[i]);                         //free's the second dimension
    And Sonny_E, welcome to the forum!

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    I have added that free to the code and posted the source in the syllogism thread I made before: link Thank you both for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  2. Finding anagrams in a word list
    By 843 in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2011, 05:19 AM
  3. Replies: 10
    Last Post: 04-10-2011, 06:52 AM
  4. Replies: 2
    Last Post: 01-21-2008, 02:07 AM
  5. open file, search of word, replace word with another
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 06-05-2002, 01:16 PM