Portál AbcLinuxu, 8. listopadu 2025 17:44
Řešení dotazu:
man fseek
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(void)
{
FILE *out;
char text[] = "Ahoj!";
int rv;
if((out = fopen("soubor.dat", "w")) == NULL)
{
fprintf(stderr, "Chyba při otvírání souboru: %s\n", strerror(errno));
rv = 1;
}
else
{
fseek(out, 68, SEEK_SET);
if(fwrite(text, strlen(text), 1, out) == 1)
{
rv = 0;
}
else
{
rv = 2;
}
}
return rv;
}
Nebo existuje druhá možnost:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(void)
{
int out;
char text[] = "Ahoj!";
int rv;
if((out = open("soubor.dat", O_WRONLY)) == -1)
{
fprintf(stderr, "Chyba při otvírání souboru: %s\n", strerror(errno));
rv = 1;
}
else
{
lseek(out, 68, SEEK_SET);
if(write(out, text, strlen(text)) != strlen(text))
{
rv = 0;
}
else
{
rv = 2;
}
}
return rv;
}
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.