Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to take user input using the getline() function. I store the input and point to it with a char *pointer.
Now I want to split the string at the white space, if there is any, but I can't change a string literal. So my idea was to transfer a copy of the input to a char array so I could then play around with it. The only issue is I don't know the size of the users input yet so I can't specify the size of the array I want.
Any ideas how I can get around this, I'm probably missing something, I'm new to C from a Java background.
Many Thanks!
You read the line, figure out its size, then make a copy of that size.
If you store a user input with some function getline (there is no such a function in C Standard) then you can split it into tokens by using standard C function strtok declared in header <string.h> If you do not want to change the original string then you can write the required function yourself by means of searching blank and non-blank characters in the string.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I rarely log on and am very new to the C language, therefore I apologize if this is a duplicate question or if this is a silly query.
I'm currently learning C and am hitting a wall with strings. I understand that char arrays are used in place of strings in the language. My question is, is there a better way than to assign an arbitrary value when declaring a char[] for user input(i.e setting the size of the array to one value when the user might enter more or less than that amount of characters)?
If you're on a POSIX system (basically anything that's not Windows), you can use getline(3) to do this. It will automatically allocate a buffer of the right size for you. Otherwise, you'll have to guess a length, allocate that, then read the input up to that length, and if you guessed wrong, use realloc to increase your guess and try again.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Fore example lets assume the following is a text in a file:
"I want to find number of characters from the first character is the nth occurrence of a character in a text file, but, I want to do this without declaring an array, storing the text file in it and applying the strchr function."
Lets say I want to find the position of the second new line character in the text? How many number of characters is the new line from the first character in the text? For e.g. The first occurrence character 't' is the 6th character of the text file.
If it is possible can someone please explain how? If no, can someone please explain why?
That can be done in an operating system that supports memory mapped files. This includes Windows and POSIX operating systems such as Linux.
In POSIX it is done using mmap(); there is example code in the documentation.
For Windows there is an API for for memory mapped files. Again, sample code is included in the documentation.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What is the best way to create a function that reads each part of a file like described in the following picture and saves it to arrays and integers, it must read and save the second part (above word_count): word; orientation; row; col; points; jogador until a number(Turn) is read.
Start by creating 3 struct's. One for people. One for words and one to aggregate the full structure. For the third, you will need to decide which arrays can be sized at compile time and which need to use malloc or calloc to allocate space for the people or word structures.
Next write a function to populate a person from a one line string and one to populate a word from a one line string. You could use strchr to find the semicolons or for less error durability you might look at sscanf.
Finally write your loading function to read the file line by line detecting 'mode' changes by (strchr(line, ';') == -1), and calling the appropriate convert function. You can then return the aggregate structure as a pointer to a malloc'ed struct.
Don't forget to write a function, that takes that pointer, to dispose of everything you malloc'ed so that a caller does not need to know your allocation details and can just say "get me one from that file" followed by "throw this away".
Unfortunately, C is unlike Java or C# in that the heavy lifting is not built in or covered by copious included libraries. You need to find libraries or write low level code yourself.
Good luck with you project.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working on a program plot.c to plot a function f(t). I can't use nested For loops unfortunately which make this harder for me.For example a function like : f(t)=t^2-4t+5.
The values of t will be between two values specified as low and high in the program. For each value of t, i want to store an asterisk in the element of a string (i.e. an array of chars) corresponding to the function value f(t), while all leading elements before the asterisk are blank.
This is of course assuming that the f(t) values are rounded to integers.In terms of the array in C, its size could be a variable. For instance:
int m=3*6;
char ex[m];
Here is what the outputs are supposed to look like :
If you have integer values, you could abuse printf format specifications.
If you want to print a * preceeded by n spaces, you could use something like:
printf("%*s\n", n, "*");
Bear in mind tha tif this is for a class assignment, you also need to be able to explain why this works, but the relevant section should be in the man page for printf.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Can anybody explain the variations in printf syntax..i am confused in some of it
like
printf(5+"hello my friend");//i have problem this addition of 5
printf("hello""my""friend");//i have problem with double apostrophe
what kind of printf prototype do these follows ?
Is this have anything to do with dynamic linking?
Can anybody show some other weird printfs and explain them.
A string in C is accessed via a pointer to a char (see H2CO3's comment for a more precise definition). If you add 5 to a pointer to a char, you start the string 5 characters later. So 5+"hello my friend" points to " my friend", skipping "hello".
When a C compiler sees two strings with nothing (except possibly whitespace) in between, it treats them as a single string. This makes it easier to break long strings in multiple lines.
So "hello""my""friend" compiles into exactly the same thing as "hellomyfriend" or
"hello"
"my"
"friend"
None of these has anything to do with printf, and a lot to do with strings.