Thread: Determining what is the input's type.

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    How can I parse? I didn't get you very well! may you elaborate how to do that by a code or something like?
    Refer to my post #9 and Matticus' post #11. You can use fgets in a loop to read line by line. You can use say, strstr or strncmp to find or match keywords like "dynamic allocation" in the line.
    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

  2. #17
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Refer to my post #9 and Matticus' post #11. You can use fgets in a loop to read line by line. You can use say, strstr or strncmp to find or match keywords like "dynamic allocation" in the line.
    laserlight, I really am seriously, it has been two days on working about how to "phrase the input.txt or to link between it and my code of c itself" can you please write a code to understand the logic behind? I'm not demanding from you to write a complete code to me...just make it as general (not specific to this case!)...and really much appreciated if you help me how to phrase from the input.txt and link them to my code for working correctly with the given inputs.

    thanks

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    laserlight, I really am seriously, it has been two days on working about how to "phrase the input.txt or to link between it and my code of c itself" can you please write a code to understand the logic behind? I'm not demanding from you to write a complete code to me...just make it as general (not specific to this case!)...and really much appreciated if you help me how to phrase from the input.txt and link them to my code for working correctly with the given inputs.
    Okay, here's an example program that parses your sample input file for "dynamic allocation" and calls a function named dynamic_allocation whenever a match is found:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void dynamic_allocation(void)
    {
        printf("This is a dynamic allocation function.\n");
    }
    
    int main(void)
    {
        const char *filename = "input.txt";
        FILE *fp = fopen(filename, "r");
        if (fp)
        {
            char line[80];
            while (fgets(line, sizeof(line), fp))
            {
                /* match for "dynamic allocation" at the start of the line: */
                if (strstr(line, "dynamic allocation") == line)
                {
                    dynamic_allocation();
                }
            }
            fclose(fp);
        }
        else
        {
            fprintf(stderr, "There was an error opening '%s'\n", filename);
        }
        return 0;
    }
    In your actual code, there will probably be a dynamic_allocation_int, dynamic_allocation_char, etc, functions. So, after matching for "dynamic allocation", you then need to go on to match for "int", "char", etc.
    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

  4. #19
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Okay, here's an example program that parses your sample input file for "dynamic allocation" and calls a function named dynamic_allocation whenever a match is found:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void dynamic_allocation(void)
    {
        printf("This is a dynamic allocation function.\n");
    }
    
    int main(void)
    {
        const char *filename = "input.txt";
        FILE *fp = fopen(filename, "r");
        if (fp)
        {
            char line[80];
            while (fgets(line, sizeof(line), fp))
            {
                /* match for "dynamic allocation" at the start of the line: */
                if (strstr(line, "dynamic allocation") == line)
                {
                    dynamic_allocation();
                }
            }
            fclose(fp);
        }
        else
        {
            fprintf(stderr, "There was an error opening '%s'\n", filename);
        }
        return 0;
    }
    In your actual code, there will probably be a dynamic_allocation_int, dynamic_allocation_char, etc, functions. So, after matching for "dynamic allocation", you then need to go on to match for "int", "char", etc.
    where should I download the input.text file in the header files or what? for reading it...like when I need to use a file.h I install it on the head files of my current C file.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    where should I download the input.text file in the header files or what? for reading it...like when I need to use a file.h I install it on the head files of my current C file.
    Have you ever used fopen to read from a file? This file is just like that. Remember, it is just an input file. It is not code. It has nothing to do with your header files or your source files.

    In my example, I wrote "input.txt", so your file should be named input.txt and should be placed in the same directory as the executable program.
    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

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by RyanC View Post
    where should I download the input.text file
    When the file is just a name like "input.txt" the computer will assume the file is in the same folder as the program being executed. If you can't find where the program is to put the "input.txt" file there, I recommend using a thumb drive and absolute pathing instead, just for testing. For example when I insert a thumb drive on my computer the drive letter is E: - so I put the file on it and the path is simply "E:/input.txt".

    in the header files or what? for reading it...like when I need to use a file.h I install it on the head files of my current C file.
    I think you are talking about installing a library, but you might be talking about using files for input.

    Specific installation instructions depend on the library being used, unfortunately.

    When you use files on the computer as input, there is no complicated install, you simply have to know where the file is on the computer.

    Many programs have pre-determined paths stored that they will guess where the file is and try first, before finally failing. You have to key these guesses into the program code and handle possible failures when you try to open the file, if you want to make it easier for your end users. This way they don't have to remember long filepaths to key in.

  7. #22
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Have you ever used fopen to read from a file? This file is just like that. Remember, it is just an input file. It is not code. It has nothing to do with your header files or your source files.

    In my example, I wrote "input.txt", so your file should be named input.txt and should be placed in the same directory as the executable program.
    I had, but this is the first time I'm facing a serious problem for brand new programmers...so please I'm still learning and aspiring to master this language properly.
    well, I have done with the section parsing in my code and I've edited it, but I'm still stuck in how to get the value of the array after calling to the dynamic_allocation? I'm meaning with that, lets say I have a text just contain a "dynamic allocation: int arr1 100" inside and nothing else, I succeeded to parse and call to the function dynamic_allocation from the input.txt's phrase "dynamic allocation:", but still stuck in how to get the input value arr1 as it's in this case "100" to the function dynamic_allocation? if this case solved then ...whole my problem would be solved at all!
    I appreciate your attitude and your assistance Laser, and thank you in advance for your cooperation.
    here's my code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include<stdlib.h>
    
    
    void dynamic_allocation(void)
    {
        int i=0,j=0,f;//I used f for getting the value of arr1 from the input.txt...I couldn't succeed so I'm stuck here.//
        char arr1[100];
        for (i;i<100;i++)
            arr1[i]='*';
        i=0;
        for (j=99;j>=(100-f*sizeof(char));j--)// the parameter f is the given value of arr1 in the input.txt..but couldn't succeeded to get it from the input.txt file so the serious problem is over here//
            arr1[j]='X';
        for (i;i<100;i++)
        {
            printf("%c ",arr1[i]);
        }
        return;
    }
     
    int main(void)
    {
        const char *filename = "input.txt";
        FILE *fp = fopen(filename, "r");
        if (fp)
        {
            char line[80];
            while (fgets(line, sizeof(line), fp))
            {
                /* match for "dynamic allocation" at the start of the line: */
                if (strstr(line, "dynamic allocation") == line)
                {
                    dynamic_allocation();
                }
            }
            fclose(fp);
        }
        else
        {
            fprintf(stderr, "There was an error opening '%s'\n", filename);
        }
        return 0;
    }

  8. #23
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    well, I have done with the section parsing in my code and I've edited it, but I'm still stuck in how to get the value of the array after calling to the dynamic_allocation? I'm meaning with that, lets say I have a text just contain a "dynamic allocation: int arr1 100" inside and nothing else
    First the normal way to share information with a function is to pass that information as an argument, so dynamic_allocation will change. Arguments are variables that come from elsewhere. They have types and scope like any other variable. You should read the function tutorial on the parent site for more information than what's in this post.

    Since you used f, I will also use f.
    Code:
    int* dynamic_allocation_int (int f); /* prototype */
    
    int* dynamic_allocation_int (int f)
    {
       printf("Another dynamic allocation function: f=%d\n", f);
       return NULL;
    }
    Now if we imagine that we got the number 100 from somewhere, we can call the function like this:
    Code:
    int *p = dynamic_allocation(numberfromfile);
    But as you probably noticed, the parsing will need to change to get the number from the file.

    In the file, we have the line:
    "dynamic allocation: int arr1 100"
    The dynamic allocation part is handled with this snippet:
    Code:
            char line[80];
            while (fgets(line, sizeof(line), fp))
            {
                /* match for "dynamic allocation" at the start of the line: */
                if (strstr(line, "dynamic allocation") == line)
                {
                    dynamic_allocation();
                }
            }
    Now we just have to find a way to extend the parsing to the rest of the line. Eventually, this will have to include "int arr1" and whatever that communicates to the program. I'm a big believer in step-by-step development though, so we can ignore that for now and focus on the number. To build on what is already done, I recommend converting from a string to an int with strtol.

    Here is a small example that calls the same function I wrote earlier:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main(void)
    {
    	const char *filename = "E:/input.txt";
    	FILE *fp = fopen(filename, "r");
    	if (fp)
    	{
    		char line[80];
    		while (fgets(line, sizeof(line), fp))
    		{
    			/* match for "dynamic allocation" at the start of the line: */
    			if (strstr(line, "dynamic allocation") == line)
    			{
    				int numberfromfile = 0;
    				char *parse_start = line;
    				char *parse_end = NULL;
    				
    				/* find a starting point for a number: a number after a space */
    				while (!(isspace(parse_start[0]) && isdigit(parse_start[1]))) /* http://linux.die.net/man/3/isdigit */
    				{
    					++parse_start;
    				}
    
    				numberfromfile = strtol(parse_start, &parse_end, 10); /* http://linux.die.net/man/3/strtol */
    				if (*parse_end == '\n' || *parse_end == '\0')
    				{
    					dynamic_allocation_int(numberfromfile);
    				}
    			}
    		}
    		fclose(fp);
    	}
    	else
    	{
    		fprintf(stderr, "There was an error opening '%s'\n", filename);
    	}
    	return 0;
    }
    Of course, you may find it easier to break this main function up into smaller functions.

    HTH.
    Last edited by whiteflags; 06-04-2015 at 04:04 PM.

  9. #24
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by whiteflags View Post
    First the normal way to share information with a function is to pass that information as an argument, so dynamic_allocation will change. Arguments are variables that come from elsewhere. They have types and scope like any other variable. You should read the function tutorial on the parent site for more information than what's in this post.

    Since you used f, I will also use f.
    Code:
    int* dynamic_allocation_int (int f); /* prototype */
    
    int* dynamic_allocation_int (int f)
    {
       printf("Another dynamic allocation function: f=%d\n", f);
       return NULL;
    }
    Now if we imagine that we got the number 100 from somewhere, we can call the function like this:
    Code:
    int *p = dynamic_allocation(numberfromfile);
    But as you probably noticed, the parsing will need to change to get the number from the file.

    In the file, we have the line:
    "dynamic allocation: int arr1 100"
    The dynamic allocation part is handled with this snippet:
    Code:
            char line[80];
            while (fgets(line, sizeof(line), fp))
            {
                /* match for "dynamic allocation" at the start of the line: */
                if (strstr(line, "dynamic allocation") == line)
                {
                    dynamic_allocation();
                }
            }
    Now we just have to find a way to extend the parsing to the rest of the line. Eventually, this will have to include "int arr1" and whatever that communicates to the program. I'm a big believer in step-by-step development though, so we can ignore that for now and focus on the number. To build on what is already done, I recommend converting from a string to an int with strtol.

    Here is a small example that calls the same function I wrote earlier:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main(void)
    {
        const char *filename = "E:/input.txt";
        FILE *fp = fopen(filename, "r");
        if (fp)
        {
            char line[80];
            while (fgets(line, sizeof(line), fp))
            {
                /* match for "dynamic allocation" at the start of the line: */
                if (strstr(line, "dynamic allocation") == line)
                {
                    int numberfromfile = 0;
                    char *parse_start = line;
                    char *parse_end = NULL;
                    
                    /* find a starting point for a number: a number after a space */
                    while (!(isspace(parse_start[0]) && isdigit(parse_start[1]))) /* http://linux.die.net/man/3/isdigit */
                    {
                        ++parse_start;
                    }
    
                    numberfromfile = strtol(parse_start, &parse_end, 10); /* http://linux.die.net/man/3/strtol */
                    if (*parse_end == '\n' || *parse_end == '\0')
                    {
                        dynamic_allocation_int(numberfromfile);
                    }
                }
            }
            fclose(fp);
        }
        else
        {
            fprintf(stderr, "There was an error opening '%s'\n", filename);
        }
        return 0;
    }
    Of course, you may find it easier to break this main function up into smaller functions.

    HTH.
    VERY very efficient!
    but why is it telling me everytime I compile the code (following the code what you've posted) it prints "there was an error opening" although I already have downloaded the text files- I think there's an error while downloading the file..

    thanks

  10. #25
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by RyanC View Post
    VERY very efficient!
    but why is it telling me everytime I compile the code (following the code what you've posted) it prints "there was an error opening" although I already have downloaded the text files- I think there's an error while downloading the file..

    thanks
    I copied my code from my source file. My input file is on a thumb drive - the E: drive. If your file is stored differently, the string will need to be changed to reflect the real location.

    Also there is a danger of a crash in the loop for parsing if there is no number. You can prevent this with a different condition:
    Code:
    while (parse_start[0] != '\0' && parse_start[1] != '\0' && !(isspace(parse_start[0]) && isdigit(parse_start[1])))
    Long, but effective.

  11. #26
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by whiteflags View Post
    I copied my code from my source file. My input file is on a thumb drive - the E: drive. If your file is stored differently, the string will need to be changed to reflect the real location.

    Also there is a danger of a crash in the loop for parsing if there is no number. You can prevent this with a different condition:
    Code:
    while (parse_start[0] != '\0' && parse_start[1] != '\0' && !(isspace(parse_start[0]) && isdigit(parse_start[1])))
    Long, but effective.


    Quote Originally Posted by whiteflags View Post
    I copied my code from my source file. My input file is on a thumb drive - the E: drive. If your file is stored differently, the string will need to be changed to reflect the real location.

    Also there is a danger of a crash in the loop for parsing if there is no number. You can prevent this with a different condition:
    Code:
    while (parse_start[0] != '\0' && parse_start[1] != '\0' && !(isspace(parse_start[0]) && isdigit(parse_start[1])))
    Long, but effective.
    Well, to sum up -all what I wanted to my code to do is:
    there's a given array with size 100 as it resembles as a RAM'S memory but we imagine it as an array!, and the occupy processing of the generally occupying of memory the same thing, but here in my case I'm dealing with an Array!!
    we initialize all the array's bytes with "*" and when there's an occupy byte we indicate the byte as "X", the code reads from the file "input.txt" the input that's written inside(mustn't use any scanf..just reading from the input text file), the inputs contain also an int number which by that number we can determine how many bytes must be occupied then after compiling and doing the required functions..prints the result of the given input of the input.txt's file;for instance, lets say in the input.txt there's just "dynamic allocation: int arr1 5", then the result must be printed as :
    XXXXXXXXXXXXXXXXXXXX****************************** ************************************************** ************************************************** ***********************************
    (Clarifying:the number of the occupied bytes in this statement is sizeof(int)*5(the given number)=20, then we change the first 20 places to 'X' in the given array which that indicates on the occupying bytes, and the others places (100-20=80)of the array we keep them as its "*")
    Another example:
    lets say there's in the input.txt "dynamic allocation: int arr1 2" => after compiling the code and reading from the input.txt file, must print:
    XXXXXXXX****************************************** ************************************************** ************************************************** ****************
    (occupying 4(sizeof(int))*2=8...8 bytes!....bla bla bla...etc )
    that's briefly what my code gonna do!

    I've tried hard1 and been struggling three days respectively, that's what I arrived and need really help to print the proper result (my code isn't printing the correct result)
    here it's:
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    // in this statement, The RAM is for me as an Array with size 100 byte!!//
    int* dynamic_allocation_int (int f); /* prototype */
    int* dynamic_allocation_int (int f)
    {
        int i=0,j=0,f;//I used f for getting the value of arr1 from the input.txt..//
        char arr1[100];//it's an array of length 100 byte//
        for (i;i<100;i++)
            arr1[i]='*';//putting all the 100's values as '*' into the array// 
        i=0;
        for (j=99;j>=(100-f*sizeof(char));j--)
            arr1[j]='X';// putting the value 'X' into all possible places that my char occupy into the RAM// 
        for (i;i<100;i++)
        {
            printf("%c ",arr1[i]);
        }
        return;
    }
      
     
    int main(void)
    {
        const char *filename = "E:/input.txt";
        FILE *fp = fopen(filename, "r");
        if (fp)
        {
            char line[80];
            while (fgets(line, sizeof(line), fp))
            {
                /* match for "dynamic allocation" at the start of the line: */
                if (strstr(line, "dynamic allocation") == line)
                {
                    int numberfromfile = 0;
                    char *parse_start = line;
                    char *parse_end = NULL;
                     
                    /* find a starting point for a number: a number after a space */
                    while (!(isspace(parse_start[0]) && isdigit(parse_start[1]))) 
                    {
                        ++parse_start;
                    }
     
                    numberfromfile = strtol(parse_start, &parse_end, 10); 
                    if (*parse_end == '\n' || *parse_end == '\0')
                    {
                        dynamic_allocation(numberfromfile);
                    }
                }
            }
            fclose(fp);
        }
        else
        {
            fprintf(stderr, "There was an error opening '%s'\n", filename);
        }
        return 0;
    }
    I really am in need for solving this problem and would be much appreciated for everyone gives any cooperation!

    thanks!

  12. #27
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Additianally- I used the parameter "f" as the int number that's appeared after arr1 in the input "dynamic allocation:int arr1 2" ..in this example f=2.

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What does this have to do with dynamic allocation of memory then? It seems to me that you just need to represent an array of 100 bytes, but you don't actually need to create any arrays at all: rather, compute the number of bytes that are to be occupied, then print the output accordingly.
    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

  14. #29
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    What does this have to do with dynamic allocation of memory then? It seems to me that you just need to represent an array of 100 bytes, but you don't actually need to create any arrays at all: rather, compute the number of bytes that are to be occupied, then print the output accordingly.
    I haven't submit all the mission requirtments because I need to complete it by myself..., there's need for a dynamic allocation like the same thing we do in the Pc's RAM, actually ee are imagining here the RAM's Capacity as an array...
    Lets say we have input the "dynamic allocation: int arr1 1" three times, => the output must be :
    1*(sizeof(int))*3(three times)=> 12 bytes occupied and indicating that
    By `X' in the array.!(the other's places of the array keeping them as its '*')
    printing:
    XXXXXXXXXXXX************************************** ************************************************** *****************************************.

    That's all what I wanted fornow and I will complete the rest of the mission by my self....so help me please!!

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In "dynamic allocation: int arr1 1", what is the significance of "arr1"?

    Do you have a separate array for "static allocation"?
    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. Determining function based an input
    By Suchy in forum C++ Programming
    Replies: 7
    Last Post: 02-20-2008, 02:52 PM
  2. Determining if input is int, double, or char
    By Lucid003 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2005, 04:16 PM
  3. Determining a user-defined class type
    By Mingzhi in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2004, 05:33 PM
  4. Determining whether input is an integer.
    By scottmanc in forum C++ Programming
    Replies: 18
    Last Post: 02-29-2004, 05:39 PM
  5. Determining a variable type...
    By -leech- in forum C Programming
    Replies: 3
    Last Post: 11-17-2002, 03:02 PM