how to separate a digit after semicolon using sscanf in c [closed] - c

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 7 years ago.
Improve this question
//I wanted to separate last digit i.e."2" as an int from "hello.mp4;2" Here is the code:
int main()
{
char str[30];
int separate = 0;
strcpy( str, "hello.mp4;2" );
sscanf(str, "%*[^;]%d", &separate);
printf("%d\n",separate);
return 0;
}
and it is not woriking...

Change the sscanf to:
sscanf(str, "%*[^;];%d", &separate);
I.e., you need to match the semicolon ; after the string that excludes it. The portion in the square brackets matches the string that precedes the semicolon, leaving ;2. So you then need to match the semicolon ; before trying to match the 2.

Related

First letter is changed into # in copied string [closed]

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 2 years ago.
Improve this question
Here is the code for copying string in C.
#include <stdio.h>
int main()
{
char s1[100], s2[100];
int i;
printf("Enter string s1:");
fgets(s1,sizeof(s1),stdin);
for(i=1;s1[i]!='\0';i++)
{
s2[i]=s1[i];
}
s2[i]='\0';
printf("%s",s2);
}
However, when I enter "How are you?", the copied string become "#ow are you?"
Since arrays start at 0, your for loop should look like this
for(i=0;s1[i]!='\0';i++) {
// ^ 0, not 1
The for-loop must start at the index 0. So change your for loop to this:
for(i=0;s1[i]!='\0';i++)
{
s2[i]=s1[i];
}

Problem with reading strings from File in C without string library [closed]

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 3 years ago.
Improve this question
I'm experiencing a problem in the resolution of an exercise. I need to read N strings from file, but I can only read the first. How can I fix it?
#include <stdio.h>
int main() {
/* variable declarations */
FILE *fp;
char vet[100];
fp = fopen("file.txt","r"); /* open file with N strings */
while(!feof(fp)) {
fgets(vet, 100, fp);
vet[100]='\0';
printf("%s\n", vet);
}
}
vet[100]='\0' this will generate error in runtime, also you don't need this line of code because fgets will handle the end of the string itself.

How to write a datastructure to a file with fput in c? [closed]

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 9 years ago.
Improve this question
What i have here is a struct that i want to print to a file. The structure consists of a series of singel character ints where pek3 Points at the first object containing a number in the structure.
fprintf didnt work and this just gives me the error:
missing ')' Before '->'
FILE *filen;
int h;
talstrul *tepek = pek3;
filen = fopen("summadata.txt","w");
for(h=1; h<=maxlen; h++)
{ int fput(tepek->num,filen);
tepek = tepek->next;
}
fclose(filen);
Your example is incomplete - so we have to guess.
f = fopen("summadata.txt","w");
for(int h=1; h<=maxlen; h++) {
fprintf(f, "%d\n", tepek->num);
tepek = tepek->next;
}
fclose(f);
should work.
fprintf works as follows:
the first argument is the file handle, that is what you get from fopen.
the format string, here "%d\n", describes, what you want to print. Here it is a integer ("%d") and then a newline ("\n").
then comes the arguement(s) to the formatstring. In this case the integer, I guess that is tepek->num.

Receiving strange number from fscanf - int conversion? [closed]

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 8 years ago.
Improve this question
I am very new to C and having a problem here. I am attempting to pass a file numbers.in through the below script. numbers.in contains 2 lines as follow:
12,34,56789
123456,789,0123
I am attempting to recognize the comma delineation.
#include <stdio.h>
void main(int argc, char *argv[])
{
int p ,n ,x ; //Converted ints.
while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 );
{
printf("got the sequence (%d,%d,%d)\n",x,p,n);
}
}
I am running the script like:
./a.out < numbers.in
Currently my script returns completely different numbers! What am I doing wrong here? Is the file sending them as characters so I need to somehow convert to ints? (I tried saving as chars and then later converting chars to ints and also got strange numbers - but different strange numbers!)
SOLVED, bad semicolon usage >_<
while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 ); <-- remove this semicolon

Comparing if two arrays are equal [closed]

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 9 years ago.
Improve this question
I want to compare if two arrays are equal. I know i have to loop through both arrays and see if they match. But how do i determine the longest array to use as an end to the loop.
EDIT
if (intToRoman(roman_integer, result))
{
for(int i = 0; i < ???; i++ )
}
for example,
roman_integer[] = "MMM"
result[] = "MMMDCCLXXX"
use strlen function to find the length of greatest string, like
int greatestlength=arrlength(a);
if(greatestlength<arrlength(b))
{
greatestlength=arrlength(b);
}
use strlen inside of arrlength or directly or write your own code in arrlength whatever you want

Resources