Im a beginner in C. Im using scanf function in my c program and its getting stuck and the program moves ahead only when i type exit.
The part of the code in which Im facing problem is:
while(fread(&emr,recsize_emr,sizeof(emr)/recsize_emr,fp)==1)
{
if(emr.emr_id==emr_id)
{
printf("%s %s %s %d %d %s %s %f \n\n",emr.fname,emr.lname,emr.company_name,emr.emr_id,emr.agt_id,emr.pol_start_date,emr.pol_end_date,emr.amount);
printf("Enter New First Name, Last Name, Company Name, EmployerID, AgentID, Policy Start Date, Policy End Date, Amount : ");
scanf("%s %s %s %d %d %s %s %f \n",emr.fname,emr.lname,emr.company_name,&emr.emr_id,&emr.agt_id,emr.pol_start_date,emr.pol_end_date,&emr.amount);
fseek(fp,-recsize_emr,SEEK_CUR);
fwrite(&emr,recsize_emr,sizeof(emr)/recsize_emr,fp);
break;
}
}
In
scanf("%s %s %s %d %d %s %s %f \n");
the final " \n" consumes all whitespace after the float has been read. That means the scanf only returns after it received a non-whitespace character after the float - for that character to reach the program, you typically need to enter a newline after it.
If you remove the final whitespace from the format string,
scanf("%s %s %s %d %d %s %s %f");
the scanf returns as soon as it finished reading the float. Then it will leave the newline - or whatever character follows the float in the input` in the input buffer, so it is probably necessary to clear the input buffer before further scans.
Related
Here is the code :
#include <stdio.h>
#include <string.h>
struct driverDetail
{
char name[20];
int licenseNumber;
char route[20];
int kms;
};
int main()
{
struct driverDetail drivers[1];
for (int i = 0; i < 1; i++)
{
printf("\nEnter details for driver %d :\n", i+1);
printf("Enter driver's name : ");
scanf("%[^\n]s%*c", &drivers[i].name);
printf("Enter driver's license number : ");
scanf("%[^\n]d%*c", &drivers[i].licenseNumber);
printf("Enter driver's route : ");
scanf("%[^\n]s%*c", &drivers[i].route);
printf("Enter driver's kilometers driven : ");
scanf("%[^\n]d%*c", &drivers[i].kms);
}
for (int i = 0; i < 1; i++)
{
printf("driver %d name : %s\n", i+1, drivers[i].name);
printf("driver %d license number : %d\n", i+1, drivers[i].licenseNumber);
printf("driver %d route : %s\n", i+1, drivers[i].route);
printf("driver %d kilometers driven : %d\n", i+1, drivers[i].kms);
}
return 0;
}
%[^\n]%*c works completely fine but why %[^\n]s%*c is not working.
when I use %[^\n]%*c \n gets rejected by the first format specifier and then %*c read the \n and discards it. But why *%c is not discarding \n when used with %[^\n]s%*c.
Same case with %d, *%c works with %d%*c but not with %[^\n]d%*c.
Why is this happening ? can somebody explain.
When scanf reads input according to one of the formats %[^\n]s%*c and %[^\n]d%*c, and the behavior is well defined (i.e. the object receiving the data corresponding to the %[ directive is not overrun), there must be either an input failure or a matching failure at the s or d. This is because scanf will expect each of these to match a literal character (a literal 's' or 'd'), but if there are any more characters available then the next one must be a newline. Anything else will have been consumed by the %[.
Perhaps you meant %[^\n]%d, %[^\n]%*s, or similar. The %s and %d directives skip leading whitespace, including newlines (unlike %c or %*c), so they would attempt to read past the newline (if any) up next in the input.
Note also that all of these alternatives afford the possibility of an input failure, as I already mentioned. That is, if the %[ consumes all the bytes up to the end of the stream, such that there are none left to read, then scanf quits. Its return value will reflect that. Take care, however, not to confuse "end of stream" with the temporary absence of characters to read. These are not at all the same thing.
I have the following code involving a struct including pointer variables and I am not able to retrieve the correct contents of each variable.
Someone can help me please?
struct MyData
{
ULONG Value[3];
wchar_t *Str1;
int Str1Len;
wchar_t *Str2;
int Str2Len;
};
// In the driver, on method that receives commands i have following:
struct MyData *pData = (struct MyData*) Irp->AssociatedIrp.SystemBuffer;
DbgPrint("0x%x \n 0x%x \n 0x%x \n %s \n %d \n %s \n %d", &pData->Value[0], &pData->Value[1], &pData->Value[2], &pData.Str1, &pData->Str1Len, &pData->Str2, &pData->Str2Len);
DbgPrint is called very much like printf is called, so you should pass the values, not the address of the values. So
DbgPrint("0x%x \n 0x%x \n 0x%x \n %s \n %d \n %s \n %d", &pData->Value[0], // etc...
should be
DbgPrint("0x%x \n 0x%x \n 0x%x \n %s \n %d \n %s \n %d", pData->Value[0], // etc...
This is a part of my 'Phonebook' program.
void viewall(){
int n, checking = 0;
char name[50];
fp = fopen("Phonebook.txt","r");
printf ("\n\n");
fscanf (fp, "%s %d", name, &n);
while (!feof(fp)){
printf (" %s +880%d\n", name, n);
fscanf (fp, "%s %d", name, &n);
++checking;
}
if (checking == 0){
printf (" Contact List is Empty. No Contacts to Show...");
}
printf ("\n\n");
fclose(fp);
menu();
}
This part displays all the contacts in list. But if any contact name has two parts they get separated. For example: I enter Anik Shahriar as name and then enter my number. I looked at my file and this data was there how it should be.
Anik Shahriar 01*******93
But when I wanted to display all the contacts. It got printed like this:
Anik 0
Shahriar 01*******93
How can i make the program print the whole line ?
This behaviour is caused by the lines
fscanf (fp, "%s %d", name, &n);
The format identifier %s scans for a single string. Any whitespace character ends the scan.
http://www.cplusplus.com/reference/cstdio/scanf/
%s
String of characters
Any number of non-whitespace characters,
stopping at the first whitespace character found. A terminating null
character is automatically added at the end of the stored sequence.
I suggest to use ca CSV file and use a format string like.
scanf("%[^,] %d", name, number)
Dont forget to test the scanf result value.
I am reading from text file hex, but I want to split it to two digits?
#include <studio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
void main() {
char hexa[100];
FILE *fp = fopen ("data2.txt", "r+");
fscanf(fp, "%s", hexa);
printf("\n first data = %s \n", hexa);
printf("\n first digit= %s \n", hexa[1]);
printf("\n second digit= %s \n", hexa[2]);
fclose(fp);
}
I read from the file successfully, but the result is:
first data = 16
segmentation fault
hexa[1] and hexa[2] are characters, but you're printing them as though they were strings, that is, character arrays. Since they're in the range 0-255, and this range of memory is inaccessible, you get a crash.
Do this:
printf("\n first digit= %c \n", hexa[1]);
and so on.
I suppose you may want to use:
printf("\n first data = %s \n", hexa);
printf("\n first digit= %c \n", hexa[1]);
printf("\n second digit= %c \n", hexa[2]);
hexa decays to a pointer to char, and hexa[1] is a char. The arguments corresponding to "%s" expected by printf() are char *s, so passing hexa[1] causes segmentation fault. To print a single character, you should use "%c" specifier.
int studentId,nOfWorkingDay;
char name[30],surname[30];
printf("Enter person information : name , surname ,studentId, nOfWorkingDay\n");
scanf("%s %s %d %d",&name,&surname,&studentId,&nOfWorkingDay);
printf("%s %s %d %d",name,surname,studentId,nOfWorkingDay);
I get a strange output. For example, when I enter:
birol genç 30 35
the output is:
birol gen┼ 30 35
What is the problem here?
scanf("%s %s %d %d",&name,&surname,&studentId,&nOfWorkingDay); should be
scanf("%s %s %d %d", name, surname,&studentId,&nOfWorkingDay);
ie, remove the & before name and surname which are already addresses of the character strings.
int studentId,nOfWorkingDay;
char name[30],surname[30];
printf("Enter person information : name , surname ,studentId,nOfWorkingDay\n");
scanf("%s %s %d %d",name,surname,&studentId,&nOfWorkingDay);
printf("%s %s %d %d",name,surname,studentId,nOfWorkingDay);
#include <stdio.h>
int main(){
int studentId,nOfWorkingDay;
char name[30],surname[30];
printf("Enter person information : name , surname ,studentId,nOfWorkingDay\n");
scanf("%s %s %d %d",name,surname,&studentId,&nOfWorkingDay);
printf("%s %s %d %d",name,surname,studentId,nOfWorkingDay);
}
description:
scanf("%s",firstname);
The %s placeholder is used to read in text, but only until the first white space character is encountered. So a space or a tab or the Enter key terminates the string. (That sucks.) Also, firstname is a char array, so it doesn’t need the & operator in the scanf() function.