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.Originally Posted by RyanC
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.Originally Posted by RyanC
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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: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.
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.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; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.Originally Posted by RyanC
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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".
I think you are talking about installing a library, but you might be talking about using files for input.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.
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.
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; }
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.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
Since you used f, I will also use f.
Now if we imagine that we got the number 100 from somewhere, we can call the function like this: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; }
But as you probably noticed, the parsing will need to change to get the number from the file.Code:int *p = dynamic_allocation(numberfromfile);
In the file, we have the line:
"dynamic allocation: int arr1 100"
The dynamic allocation part is handled with this snippet:
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.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(); } }
Here is a small example that calls the same function I wrote earlier:
Of course, you may find it easier to break this main function up into smaller functions.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; }
HTH.
Last edited by whiteflags; 06-04-2015 at 04:04 PM.
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:
Long, but effective.Code:while (parse_start[0] != '\0' && parse_start[1] != '\0' && !(isspace(parse_start[0]) && isdigit(parse_start[1])))
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:
I really am in need for solving this problem and would be much appreciated for everyone gives any cooperation!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; }
thanks!
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.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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!!
In "dynamic allocation: int arr1 1", what is the significance of "arr1"?
Do you have a separate array for "static allocation"?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)