Thread: fgets() make the string it gets turn into an array to match a char

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    105

    fgets() make the string it gets turn into an array to match a char

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    
    
    #define MEETING_ARRAY_LENGTH 5
    #define MAXCHAR 255
    
    
    int main(void)
    {
    
    
    	char meeting_array[MEETING_ARRAY_LENGTH][20] = {
    						"meeting 1",
    						"meeting 2",
    						"meeting 3",
    						"meeting 4",
    						"meeting 5",
    	};
    
    
    	char string[15];
    	char string_2[15];
    	int meeting_id_number;
    	int date_id;
    	char line[200];
    	int num = 1;
    	char str[MAXCHAR];
    
    
    	FILE *fp;
    
    
    	fp = fopen("meeting_2.txt", "a+");
    
    
    	do
    	{
    		printf("> ");
    
    
    		fgets(line, sizeof line, stdin);
    		sscanf(line, "%s %s %d %d", string, string_2, &meeting_id_number, &date_id);
    
    
    		if(meeting_id_number > MEETING_ARRAY_LENGTH)
    		{
    			printf("Program will produce an error!\n");
    			break;
    		}
    
    
    		else if ((strcmp(string, "MEETING") == 0) && (strcmp(string_2, "LIST") == 0) && meeting_id_number)
    		{
    			printf("%s\n", meeting_array[meeting_id_number - 1]);
    		}
    
    
    		else if ((strcmp(string, "MEETING") == 0) && (strcmp(string_2, "ADD") == 0) && meeting_id_number && date_id > 1)
    		{
    			// print data to console
    
    
    			printf("array: %s\ninputted date_id: %d\n", meeting_array[meeting_id_number - 1], date_id);
    			
    			// print data to data stream pointer (text file)
    		
    			fprintf(fp, "%d_%d\t", meeting_id_number, date_id);
    		}
    
    
    		else if ((strcmp(string, "PRINT") == 0) && (strcmp(string_2, "FILE") == 0) && meeting_id_number)
    		{
    			char underscore = '_';
    
    
    			int i, counter;
    			counter = 0;
    
    
    			while(fgets(str, MAXCHAR, fp) != NULL)
    			{
    				if (str)
    				{
    					if(str == underscore)
    					{
    						counter++;
    						printf("%d", counter);
    					}
    				}
    			}
    			/*
    			for(i = 0; i < MAXCHAR; i++)
    			{
    				while (fgets(str, MAXCHAR, fp) != NULL)
    				{
    					// puts(str);
    				}
    
    
    				fclose (fp);
    			}
    			*/
    
    
    		}
    		
    		else
    		{
    			printf("Error, command not recognized\n");
    		}
    
    
    	} while (strcmp(string, "0") != 0);
    
    
    	
    	return 0;
    }
    good afternoon all, what is the best way to break this str string into an array so I can get an array out of and individually see what characters to match "underscore" or an '_'? Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,503
    A string is already an array: an array of characters whose elements in use are at index 0 up to the index of the first null character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-15-2019, 12:57 PM
  2. Replies: 2
    Last Post: 03-02-2014, 05:30 PM
  3. String Array fgets
    By deeisenberg in forum C Programming
    Replies: 2
    Last Post: 12-01-2011, 03:22 PM
  4. need help with char array and fgets
    By fr0zen in forum C Programming
    Replies: 25
    Last Post: 12-07-2007, 04:46 AM
  5. Turn a string into an array
    By The Letter J in forum C++ Programming
    Replies: 8
    Last Post: 12-22-2004, 12:08 PM

Tags for this Thread