0
void writeFile()
{
const char * filePath = "numbers.txt";
int fd = open(filePath,O_RDWR);
int index,numAdd;
index = validIndex();
numAdd = validNumberToAdd();
void *addr = mmap(nullptr,1000,PROT_WRITE, MAP_SHARED| MAP_ANONYMOUS, fd,0);
char *p = (char *)addr;
p[index] = numAdd;
cout << "wrote " << numAdd << endl;
close(fd);
}

So I am able to read data in the numbers.txt when i manually put data into it. But when I try to write data using this function it will delete all the data in the file and leave it blank.

The intended function should be that I am able to open a file called numbers.txt pick an index and write a value from numAdd to the text file. Then when I close it and run again it will have the data stored into it.

rest of the code is on this pastebin not needed though but this writing function is my main problem. https://pastebin.com/UYfx6QdP

daviCo
  • 1
  • 2
  • What kinds of error checking are you doing? And are you aware that you can't use `mmap()` to extend the size of a file? (And I do hope you're not using `mmap()` for performance. If you *are*, please read the comments on this question - and follow the links: http://stackoverflow.com/questions/258091/when-should-i-use-mmap-for-file-access) – Andrew Henle Apr 02 '17 at 20:03
  • Please forgive my ignorance... What is the problem, and what is the question? – jww Apr 02 '17 at 22:00
  • [man mmap](https://linux.die.net/man/2/mmap): "MAP_ANONYMOUS. The mapping is not backed by any file; its contents are initialized to zero. The **fd and offset arguments are ignored**" – kaylum Apr 02 '17 at 23:09
  • I am not using mmap() for perfroamnce just using it for school. Also the problem is stated in first paragraph. Problem: But when I try to write data using this function it will delete all the data in the file and leave it blank. – daviCo Apr 03 '17 at 08:18

0 Answers0