Print header defined size string [closed] - c

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
I'm trying to print in a file a string with a fixed size. Something like this:
#define SIZE 30
main()
{
FILE *fp = fopen("myfile.txt","w+");
char s[10];
sprintf(s, "my text");
fprintf(fp, "%SIZEs", s);
fclose(fp);
}
but I keep getting errors.. help?

You should define your format string like the following:
fprintf(fp, "%*s", SIZE, s); // Right aligned string
fprintf(fp, "%-*s", SIZE, s); // Left aligned string
From the printf man page:
The precision
Instead of a decimal digit string one may write "*" to specify that the precision is given in the next argument

Related

putting '0' before ID number that the user input [closed]

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 3 years ago.
Improve this question
I'm a new c programmer.
I was asked to get an ID number as an input and if the ID length < 9 I need to put '0' before the number.
example:
input: 12345
output: 000012345
I can use the libraries: stdio, stdlib, string, math and time.
You can use printf for this:
#include <stdio.h>
int main () {
printf("%09d\n", 12345);
return(0);
}
Documentation
There is an easy solution in here.
But if you want a hard solution, where you juggle around with strings and such, you can do the following:
convert your id into a string (if it isn't already one, but argv should a string by default)
calculate the number of zeros you need, by doing 9-idString Length
create a "buffer" string variable
for loop from 0 to 9-idString and use strcat to add "0"s to your buffer string.
add your id to your buffer string using strcat
Example code would be something like this:
int id = 12345;
char idstring[10];
char buffer[10];
sprintf(idstring, "%d", id);
int missingZerosCount = 9 - strlen(idstring);
strcpy(buffer, "");
while (missingZerosCount > 0) {
strcat(buffer, "0");
missingZerosCount--;
}
strcat(buffer, idstring);
argv variant:
char idstring[10];
char buffer[10];
sprintf(idstring, "%s", argv[1]);
int missingZerosCount = 9 - strlen(idstring);
strcpy(buffer, "");
while (missingZerosCount > 0) {
strcat(buffer, "0");
missingZerosCount--;
}
strcat(buffer, idstring);

How can I extract the integer from a string with mixed alphabetic, punctuation and integers in C? [closed]

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
I have a string that looks like "AGE:83". I want to take the integer "83" out of this string, and I know that I should use the "sscanf" function. However, there is no white space between this string. How can I do that?
Use this:
const char *str = "AGE:83";
int age;
sscanf(str, "%*[^0-9]%d", &age);
This format will skip any non digits and parse an integer after that.
Note that it will fail if there are no non digit characters before the number. To handle the case where the number can be at the start of the string, use first try a direct match:
if (sscanf(str, "%d", &age) != 1
&& sscanf(str, "%*[^0-9]%d", &age) != 1) {
// no number found;
return 1;
}
// age was correctly extracted from `str`
Negative numbers cannot be parsed this way, unless you know the prefix does not contain a '-' (using format "%*[^0-9-]%d")
You can also use a string function to skip the prefix and convert the rest for atoi() or strtol():
age = atoi(str + strcspn(str, "0123456789"));
As noted by Enzo Ferber, if the format of the string is fixed and is known to start with AGE:, you can just convert the remainder of the string with:
age = atoi(str + 4);
But this would invoke undefined behavior if the string is shorter than 4 characters as you would be potentially dereferencing invalid addresses.

List of "Always wrong" snippets in C [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Was talking to a colleague today on one-spot errors - I.e. errors (or at least patterns that should ring an alarm bell) in code that a decent programmer should be able to spot at a single glance like
x = malloc (strlen(y));
while (!feof (f)) {
...
}
char *f(){
char x[100];
...
return x;
}
Who has similar snippets of such patterns? I would suggest anyone who has been on SO for a while will have his personal favourites of those
char *buf;
scanf("%s", buf);
This is wrong, because no memory has been allocated for buf.
char buf[100];
scanf("%s", &buf);
This is wrong, because scanf expects a char *, not a char (*)[n].
char c;
while ((c = getchar()) != EOF)
putchar(c);
This is wrong, because EOF does not fit in the range of a char. Use int instead.
fflush(stdin);
fflush is undefined for input streams, like stdin, albeit this is implemented as an extension in some compilers, like Microsoft C.
#define IN 0;
Do not put semicolons at the end of a #define.
blk = realloc(blk, n);
If realloc fails, any contents in blk will be lost, because realloc will return NULL. To solve the problem, copy the return value into a temporary and only if the temporary is not NULL, copy to the final destination.

C read in string instead file [closed]

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 have this
int main(int argc, char* argv[])
{
FILE *file = fopen("text", "rb");
char line[128];
while(fgets(line, sizeof(line), file))
{
printf("%s", line);
}
return 0;
}
I would like to remove the load from the file.
This is part of the program.
I want to leave while fgets
I try this
char file[] = "name = xxxx\nsurname = xxx\n adress = xxx";
If I understand your question, and you're on a system that supports it, fmemopen() might do what you want.

Parse int and char into two separate values [closed]

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'm having the issues of trying to separate 1s or 2d into int 1 and char 's' and then storing them as separate values. How would I go about doing this. I know I can't do it with strtok since there are no delimiters.
Use strtol. For example,
char *endptr;
val = strtol(input_str, &endptr, 10;
next_char = *endptr;
As discussed in the manpage, http://man7.org/linux/man-pages/man3/strtol.3.html, the second parameter is a pointer to a char pointer and after the conversion, the char pointer points to the following character.
You can access each char from the string by index. To convert char to int (not by ascii value, '1' to 1) you just do the following:
int a = c[0]-'0';
and for the char:
char b = c[1];

Resources