Thread: trying to loop through a text file to get a different word each time I use the file

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

    trying to loop through a text file to get a different word each time I use the file

    So the program is really big so I'll just post the two parts that need to be fixed.

    The first part is the main function:

    This version doesn't work right, it asks if the answer was right but then it just gives the same answer when I choose zero:
    Code:
    /* the main program */ 
    int main ()
    {   
    	/* declaring and initiaizing variables */
    	FILE *book;
    	FILE *a;
    	FILE *b;
    	FILE *c;
    	char * pch = malloc(300);
    	char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	char buffer[LINES];
    	int period = 0;
    	int work = 0;
    	int one = 1;
    	char sentence[4096] = {0};
    	/* The function is called so I don't have to type getch */
    	atexit(MyExit);    
    
    	/* Inputing a single sentence, generating one sentence then passing it to the rest of the program */
    	sentence_generator();
    	/* The function is called and creates the text file */
    	split_by_sentence();
    
    	/* The text file just created is opened */
    	book = fopen("readtext.txt", "r");
    
    	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);
    
    				printf("Does the work please you? 1 for yes, 0 for no\n");
    				scanf("%d", &work);
    				if(work < 1)
    				{
    					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);
    
    					printf("Does the work please you? 1 for yes, 0 for no\n");
    					scanf("%d", &work);
    
    				}
    
    			if(period > one)
    			{
    				memset(&sentence[0], 0, sizeof(sentence));
    			}
    			period = 0;
    		}
    	}
    	fclose(book);
    
    	/* 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 the search function. I tried to send the word filedata to the array used, but then when I chose zero in main the same word was used so the search isn't ignoring that word that was just used:

    Code:
    /* The function to search text file for compatible word */
    
    void search(char *src, char *a, char *b, char *c)
    {
    	FILE *sp;
    	char used[300] = {0};
    	char byte [4] = {0};
    	char *filedata = malloc(300);
    	sp = fopen("readfile.txt", "r");
    	if(!sp)
    	{
    		perror("Error: file readlist.txt was not found or opened");
    		return;
    	}
    	while(fgets(filedata, 4096, sp)!=NULL)
    	{
    		byte[0] = filedata[0];
    
    		if( (!strncmp (byte, a, 1)) && (strpbrk(filedata, b)) && strcmp(filedata, used))
    			{
    
    				strcat(src, filedata);
    				memmove(used, filedata, strlen(filedata)+1);
    				/* write the text file comparison result to file */
    				/*fprintf(fp, "%s\n", text);*/
    				break;
    			}
    		else if( (!strncmp (byte, a, 1)) && (strpbrk(filedata, b)) )
    			{
    
    				strcat(src, filedata);
    				memmove(used, filedata, strlen(filedata)+1);
    				/* write the text file comparison result to file */
    				/*fprintf(fp, "%s\n", text);*/
    				break;
    			}
    		
    	}
    	fclose(sp);
    	return;
    }
    I can get the program to work fine if the first return from sp is all I want, but I want to get the next word that fits the if condition in the search function if I want.

    I wasn't going to ask for help but I have been at this for a long time so I thought I should.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're referring to this block of code?

    Code:
    				printf("Does the work please you? 1 for yes, 0 for no\n");
    				scanf("%d", &work);
    				if(work < 1)
    				{
    					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);
    
    					printf("Does the work please you? 1 for yes, 0 for no\n");
    					scanf("%d", &work);
    
    				}
    Isn't it running the same code no matter what choice you make for the work variable? Lines 5 through 11 appear to be code copied from the other choice's code.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    I found my solution.
    The main function was returned to the way it was:

    Code:
    /* the main program */ 
    int main ()
    {   
    	/* declaring and initiaizing variables */
    	FILE *book;
    	FILE *a;
    	FILE *b;
    	FILE *c;
    	char * pch = malloc(300);
    	char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	char buffer[LINES];
    	int period = 0;
    	int one = 1;
    	char sentence[4096] = {0};
    	/* The function is called so I don't have to type getch */
    	atexit(MyExit);    
    
    	/* 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");
    
    	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);
    
    			if(period > one)
    			{
    				memset(&sentence[0], 0, sizeof(sentence));
    			}
    			period = 0;
    		}
    	}
    	fclose(book);
    
    	/* 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;
    }
    But the search function was given a switch statement to pick the word from the text file:

    Code:
    /* The function to search text file for compatible word */
    
    void search(char *src, char *a, char *b, char *c)
    {
    	FILE *sp;
    	int number = 0;
    	int n = 0;
    	char used[300] = {0};
    	char byte [4] = {0};
    	char *filedata = malloc(300);
    	sp = fopen("readfile.txt", "r");
    	if(!sp)
    	{
    		perror("Error: file readlist.txt was not found or opened");
    		return;
    	}
    	printf("Enter 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++;
    			switch(n)
    			{
    
    
    			case 1:
    				{
    					while(number < 2)
    					{
    						if(number==1)
    							strcat(src, filedata);
    						break;
    					}
    				}
    				break;
    
    			case 2:
    				{
    					while(number < 3)
    					{
    						if(number==2)
    							strcat(src, filedata);
    						break;
    					}
    				}
    				break;
    
    			case 3:
    				{
    					while(number < 4)
    					{
    						if(number==3)
    							strcat(src, filedata);
    						break;
    					}
    				}
    				break;
    
    			case 4:
    				{
    					while(number < 5)
    					{
    						if(number==4)
    							strcat(src, filedata);
    						break;
    					}
    				}
    				break;
    
    			case 5:
    				{
    					while(number < 6)
    					{
    						if(number==5)
    							strcat(src, filedata);
    						break;
    					}
    				}
    				break;
    
    			case 6:
    				{
    					while(number < 7)
    					{
    						if(number==6)
    							strcat(src, filedata);
    						break;
    					}
    				}
    				break;
    
    			case 7:
    				{
    					while(number < 8)
    					{
    						if(number==7)
    							strcat(src, filedata);
    						break;
    					}
    				}
    				break;
    
    			case 8:
    				{
    					while(number < 9)
    					{
    						if(number==8)
    							strcat(src, filedata);
    						break;
    					}
    				}
    				break;
    
    			case 9:
    				{
    					while(number < 10)
    					{
    						if(number==9)
    							strcat(src, filedata);
    						break;
    					}
    				}
    				break;
    
    			default:
    				{
    					while(number < 5)
    					{
    						if(number==4)
    							strcat(src, filedata);
    						break;
    					}
    				}
    
    
    			}
    		}
    
    	}
    	fclose(sp);
    	return;
    }
    So I don't need your help with this anymore. Thanks.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That was an easy fix! You're welcome, Jeremy.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    Hi Adak,

    I have a new problem. I posted in my previous post the main and search function with the switch statement. That works fine, but now what I want to do is after the program has run I want the program to ask if the answer was correct and if the answer is no then go to a different switch choice.
    a) This means the switch starts at choice 1, then I get the results and I'm asked if the answer was good.
    b) If not the program is run again and the next choice in the switch statement is used and so forth.
    c) Until there is no more switch options then the program gives a comment and closes.

    Can you help me solve these problems please. I can post the source if you want: new.zip.txt
    Just remove the txt extension and unzip the file.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I can try. Obviously, you know how to code a switch statement, so I'm not sure what I can do to help.

    More specifics would be good, but please, not the whole program. What's the problem with making another switch statement? (Which I believe you stated that you needed.)

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    I like the switch statement idea though. I don't know how I can be more detailed in what I want to do. The idea is, if the switch runs then I want to get asked if the result was good, or else to run the switch again.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Perhaps

    Code:
    int good = 0;
    while(!good) {
       switch statement here
       //end of switch statement here
       printf("Was that result good? [1=yes, 0=no]: ");
       scanf("%d",&good); // was the result good?
    }  //end of while statement

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    I'll try that. Thanks.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    Hey, I solved my problem! This code works fine:

    Code:
    /* the main program */ 
    int main ()
    {   
    	/* declaring and initiaizing variables */
    	FILE *book;
    	FILE *a;
    	FILE *b;
    	FILE *c;
    	char * pch = malloc(300);
    	char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	char buffer[LINES];
    	int period = 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);    
    
    	printf("Type 1 to run the program\n");
    	scanf("%d", &x);
    	/* The sentence generator section is run*/
    	while(!good) 
    	{
    		switch(x)
    		{
    		case 1:
    			{
    				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");
    
    				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);
    						printf("Was that result good? [1=yes, 0=no]: ");
    						scanf("%d",&good);
    						if(period > one)
    						{
    							memset(&sentence[0], 0, sizeof(sentence));
    						}
    						period = 0;
    						break;
    					}
    				}
    				fclose(book);
    
    				/* 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;
    }
    Thanks for your help Adak.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well done, Jeremy, and you're welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. find word in a text file
    By 26friends26 in forum C++ Programming
    Replies: 7
    Last Post: 03-01-2010, 09:37 PM
  2. reading text-and-numbers file word by word
    By bored_guy in forum C Programming
    Replies: 22
    Last Post: 10-26-2009, 10:59 PM
  3. word search in a text file
    By willie in forum C Programming
    Replies: 3
    Last Post: 11-20-2008, 11:10 AM
  4. searching a text file for a word
    By satory in forum C Programming
    Replies: 5
    Last Post: 02-22-2005, 01:04 PM
  5. Help reading text file word by word
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-25-2002, 05:13 PM