Hi I'm new here and I have a problem which is hindering me.
I have opened a file and wrote some stuff to it. Then later on I want to open the file and read it back in to a buffer. When I try to read the file back in to the buffer all it reads is the first line. I cannot understand why, also I cannot open the file I created in any text editor, they complain about the encoding.
Here is the code which writes to the file:
if((fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) == -1)
{
printf("A file of the name \"%s\" exists or the file could not be created\n",filename);
exit (5);
}
write(fd, seperator, strlen(seperator));
write(fd, "\n", 1);
write(fd, argv[2], strlen(argv[2]));
write(fd, "\n", 1);
write(fd, buffer, strlen(buffer));
write(fd, seperator, strlen(seperator));
free(buffer);
close(fd);
Now if I cat out the file it has created it has exactly what I want in it, however gedit won't open it.
The following code is opening the file and attempting to write it to the buffer:
if((fd = open(argv[2], O_RDONLY)) == -1)
{
printf("%s cannot be opened\n", argv[2]);
exit (2);
}
eof = 0;
while(!eof)
{
status = read(fd,buffer,filesize);
if (status == -1)
{
printf("Error reading file %s\n", argv[2]);
exit (4);
}
else if (status == 0)
{
eof = 1;
}
}
Now if I print out the buffer it just contains the first line of the file and a few newlines. Any ideas?
BTW I am using Ubuntu 10.04
Thanks