none
C: How would I get the next word in a list?

    Question

  • 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:

    /* 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:

    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:

    *++filedata;
    



    But that just removed a letter when I tested with printf.
    So I'm stuck and need some help.

    Wednesday, December 14, 2011 2:40 AM

Answers

  • I got my solution after a few hours working on it and here it is:

     

     

    /* 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;
    }
    

     


    The part in the while loop where it gets number 80 is where the bulk of the updating was.

    I'm going to be busy doing a bit more updating. If you want I can post the source to my syllogism program?


    • Edited by brownie ri Wednesday, December 14, 2011 10:47 AM
    • Marked as answer by brownie ri Wednesday, December 14, 2011 1:09 PM
    Wednesday, December 14, 2011 10:46 AM

All replies

  • The general idea with any parsing is to process the characters in the file one character at a time, and keep context about what you are doing, and then emit (process) the individual words as you encounter them.  This essentially turns a stream of characters into a stream of words.

    Breaking things up into words is not easy.  You can't do it on character classes alone.  Consider the word I'm.  This is really all one word.  The apostrophe is not a word separator in all cases.  But it might be if you typed The boy said 'hello'.  but not if you just type I'm.  So it's kind of hard to determine just by character alone what constitutes a word separator.  And there are all kinds of whitespace characters too, like newlines (CR and LF) and tabs.

    Is there a specific part of this task you want help with?  Your example code is a bit large to embody the problem of just reading words from a file.

    Wednesday, December 14, 2011 3:29 AM
  • I'll post in a bit as right now I'm getting my question formatted so the code is more logical.
    • Edited by brownie ri Wednesday, December 14, 2011 4:01 AM
    Wednesday, December 14, 2011 4:01 AM
  • I got my solution after a few hours working on it and here it is:

     

     

    /* 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;
    }
    

     


    The part in the while loop where it gets number 80 is where the bulk of the updating was.

    I'm going to be busy doing a bit more updating. If you want I can post the source to my syllogism program?


    • Edited by brownie ri Wednesday, December 14, 2011 10:47 AM
    • Marked as answer by brownie ri Wednesday, December 14, 2011 1:09 PM
    Wednesday, December 14, 2011 10:46 AM
  • I discovered my previous thread to my syllogism program wasn't deleted, it was only moved. I have updated my program today and will post the link in case you want to see how the code in this thread fits in my syllogism program:

     

    link
    Thursday, December 15, 2011 7:31 PM