Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am trying to get an input from a text file,whose name is of the format
(without spaces) .Eg, 3shop where 3 is number of inputs in file.
How do I extract this number from the string ?
Also content of the file is of same format and variable length.
**i.e complete 3shop.txt is of the form
1soap 3toothpate 5biscuits
8biscuits
9toothpaste 5 soap
There is no limit on size of integer,otherwise i could have used an array and extracted the number.
Please, suggest some good ways to do this.
Thanks in advance.
char name[32] = "3shop.txt", *p = name;
long id;
id = strtol(name, &p, 10);
printf("id=%ld name=%s\n", id, p);
10 in strtol means base 10 (decimal)
After the call to strtol, p points to the next character in name after the numerical value
Related
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 3 years ago.
Improve this question
I was making a script that is calculating the additions between two natural numbers which decimal lengths should be smaller or same with 10000, and printing a result of the sum.
Of course, there ain't any variable type that can hold a integer which length is 10000 in C.
So, I made the program by utilizing the simple additions' calculating logic that all we learn in a school when we were young. And also, I just should use strings to get those gigantic numbers.
But some results were starting with zero. I knew why did the zero appeared there, but I did prefer to have a result that is like "1234", not "01234". By the way, all other stuffs were perfect.
I needed a function that gets input as string, and erases a single zero starts with a string if it exists.
And could you make it instead of me, please? You should probably consider that the strings we will deal with can have such a length that is smaller or same with 10000.
Maybe this:
char * f( char * str )
{
while ( *str == '0' && str[1] )
str++; // skips all zero-s when it is not last character in string
return str;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
This is input: Francetic, Petra#13/12/1930 Trg zrtava Uskoka 156 (Skopje) 800893452/2008
It wont fscanf properly it show's m=0 and that's how I know that while loop was not successful.
while(fscanf(mrtvaciTxt, "%[^,],%[^#]#%d/%d/%d %[^0-9]%d (%[^)]) %[^/]/%[^\n] ",
&pomrli[m].prezime, &pomrli[m].ime,
&pomrli[m].dan,&pomrli[m].mjesec,&pomrli[m].godina, &pomrli[m].adresa,&pomrli[m].brUlice,
&pomrli[m].brOsobne, &pomrli[m].godSmrti )== 9)
{
m++;
}
printf("%d\n\n", m);
How can I fscanf this and is there any tutorial how can i be better at this because it takes me so much time.
Francetic
Petra
13
12
1930
Trg zrtava Uskoka 156 (Skopje)
800893452
2008, I want fscanf to look like this
There are multiple errors in the scanf format.
Here is the correct scanf format string:
" %[^,], %[^#]#%d/%d/%d %[^(](%[^)]) %[^/]/%d"
Here is the list of errors:
there are 11 % while you expect only 9 fields
lacking a space in front of the format string to consume the newline
there is a / in front of the first %d which shouldn’t be there
you are using a [^\n] where you should use [^(]( for the adresa
you are using %*[\n] for unknown reason
you should use [^)] to get the grad
etc.
you modified you question, so I don’t know what else you did.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
When I am trying to print or write data in following way its automatically concatenating more than one string into one.
Following is the structure holding data:
typedef struct Data{
int id;
char fname[10];
char lname[11];
char address[27];
char area[18];
char city[21];
char cityCode[3];
char pinCode[5];
char contact1[13];
char contact2[13];
char email[33];
}Data;
Data d;
When i try to print this in following manner
printf("\nData is as follow:\n");
printf("\nID:%d",d.id);
printf("\nfname:%s",d.fname);
printf("\nlname:%s",d.lname);
printf("\naddress:%s",d.address);
printf("\narea:%s",d.area);
printf("\nCity:%s",d.city);
printf("\nCityCode:%s",d.cityCode);
printf("\npinCode:%s",d.pinCode);
printf("\ncontact1:%s",d.contact1);
printf("\ncontact2:%s",d.contact2);
printf("\nemail:%s",d.email);
This is what i got
Data is as follow:
ID:1
fname:abc
lname:xyz
address: 6649 N test lane
area:Test City
City:testtest
CityCode:XYZ12345 123-456-6789789-456-6123
pinCode:12345 123-456-6789789-456-6123
contact1: 123-456-6789789-456-6123
contact2:789-456-6123
email:ttest#gmail.com
2
Same thing happened when I tried to write it on file using fprintf().
1,abc,xyz 6649 N test lane,Test City,testtest,XYZ12345 123-456-6789789-456-6123,12345 123-456-6789789-456-6123, 123-456-6789789-456-6123,789-456-6123,ttest#gmail.com
2
My main aim is to write the data on file separated by (,) and the data is
1,abc,xyz, 6649 N test lane,Test City,testtest,XYZ,12345, 123-456-6789,789-456-6123,ttest#gmail.com
I'm just guessing here, but if we take a look at the cityCode member, it is an array of three characters. To be a "string" of three characters, it needs space for four characters, to include the terminating '\0' character.
Make sure all arrays have space for, and have, the string terminator character.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm being bugged right now because I'm making a little password validation thing in C.
the case is
It should have at least 8 characters entered
It should have at least 1 number
It should have at least 1 uppercase / lowercase letter.
For example:
if the rules are not complied it would show
Enter your password: doratheexplorer
NOT A GOOD PASSWORD
NO DIGIT
NO UPPERCASE LETTER
else
continue
how would I do something like this? do I need to use string.h?
Suppose your password is stored in a character array named password. Then you can do the validation as follows including string.h:
int digitflag=0;
int upperflag=0;
int lowerflag=0;
int i;
if(strlen(password)<8)
{
printf("Length is less than 8");
}
else
{
for(i=0;i<strlen(password);i++)
{
if(isdigit(password[i]))
digitflag=1;
if(isupper(password[i]))
upperflag=1;
if(islower(password[i]))
lowerflag=1;
}
if(digitflag==0)
printf("No digits");
if(upperflag==0)
printf("No Upper Case Letters");
if(lowerflag==0)
printf("No Lower Case Letters");
}
At first you should check length of the password if it is less than 8 then you can give a error message.And for digit,uppercase and lowercase character you can use separate flag and then you have to read each character set appropriate flag.When you finished reading all character you can check whether all flags are set or not and take appropriate actions.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Part of the program I am writing requires an array to be created using malloc instead of the regular way of doing it. I have to have the user enter a number, assign that number the name MAX, and create an array using malloc() with numbers 2 through the number entered. How would I go about coding this?
You create an "array" with malloc() by specifying the size (in bytes) of the array and assigning the return value to a pointer of the appropriate type. If you're intending for this to be an array of objects that are larger than one byte, you can multiply the number of objects by the size of the object, which can be obtained with the sizeof operator.
For example, you can create an "array" of fifty int objects like so:
int *ar = malloc(50 * sizeof (int) );
You can do that by:
T *dynamic_memory;
....... //Get the desired array size from user input and store in 'array_max_size'
dynamic_memory=malloc((sizeof(T) * array_max_size);
T : data type of array
You can then use dynamic_memory for your purpose.