×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

help me with this memory maped file code

help me with this memory maped file code

help me with this memory maped file code

(OP)
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;
}
[/code]

RE: help me with this memory maped file code

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];

RE: help me with this memory maped file code

(OP)
hi xwb,

thats working!..but I used scanf() instead of gets...
Thanks a lot xwb...I appreciate your help...

RE: help me with this memory maped file code

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.

RE: help me with this memory maped file code

> 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.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1049157810&id=1043284351
http://c-faq.com/stdio/getsvsfgets.html
 

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

RE: help me with this memory maped file code

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close