Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help me with this memory maped file code

Status
Not open for further replies.

kamal2010

Programmer
Apr 11, 2008
2
US
hello everybody,

I am new to this forum..I am trying to run a code..
I craeted a text file first with the name mmap.txt and wrote some lines in that....but I dont know why its not working It reads integer value for the position three times...but doesnt reads a character....can somebody please help me with that?
the code is:

Code:
#include<unistd.h>
#include<sys/mman.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdio.h>
#include<string.h>
int main()
{
int fd;
char *fileptr;
int i;
int x;
char ch;
fd=open("mmap.txt",O_RDWR);
fileptr=mmap(0,26,PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);
for(i=0;i<3;i++)
{
printf("Enter the position in the file you want to change: ");
scanf("%d",&x);
printf("\n");
printf("Enter the new letter for that position: ");
scanf("%c",&ch);
printf("\n");
fileptr[x-1]=ch;
printf("\n");
}
munmap(fileptr,26);
close(fd);
return 1;
}
 
Try using gets to read the line. It is probably reading the CR. Something like
Code:
char buff[2];
..
/* replacement for scanf("%c", &ch) */
gets (buff);
ch = buff[0];
 
hi xwb,

thats working!..but I used scanf() instead of gets...
Thanks a lot xwb...I appreciate your help...
 
Scanf the most complicated function in the whole of the C library. You can run a one day course on what goes into the format string. The behaviour is also a bit strange. It is best to use it as fscanf - for reading in file data. Using it to read data typed in on the console can be quite trying at the best of times. Use a combination of gets and sscanf but using scanf directly can cause a lot of problems. The behaviour changes from one OS to the next.

1) scanf only starts getting the info after you have hit CR
2) if your input string is %c and you have type x followed by CR, what you get is the last character typed i.e. CR.
3) you can get a single character using getchar but it is best not to mix scanf and getch/gets. They use different buffers and your program can easily go out of sync with its input.

Not many books cover the use of regular expressions in the format string. Note also that scanf returns the number of items read. So, unlike most C functions, 0 doesn't mean OK.
 
> Try using gets to read the line. It is probably reading the CR. Something like
What a horrible suggestion!.
gets() is the world's most dangerous function. Never to be used again, never to be suggested as a 'fix' for some problem.
It's just a big disaster waiting to happen.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
If you do decide to change your life by using fgets, remember that unlike gets, you get the line terminators as well.

The problem with "dangerous" functions is that they all assume that you know what you are doing. Sometimes the buffers will overflow but equally, you could specify the wrong size buffer size on fgets and end up in the same situation when you 'think' you're safe.
Code:
char safe[20];
char* incomplete = malloc(20);
char* possiblecrash = malloc (20);
...
fgets(safe, sizeof (safe), stdin);
fgets(incomplete, sizeof (incomplete), stdin);
fgets(possiblecrash, 40, stdin);
There is no foolproof method and the 'safe' functions can be equally as dangerous as the 'dangerous' functions.

The other common problem is Hungarian notation but that is another story.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top