c语言写一个文件操作程序

2024-12-28 16:09:14
推荐回答(3个)
回答1:

#include

void main()
{
FILE *fp_in,fp_out;
int start,length;
fp_in=fopen("..\\要复制的路径名","r");
fp_out=fopen("..\\直接写要复制到文件名称","w");
printf("输入起始位置\n");
scanf("%d",&start);
printf("输入要复制的字符长");
scanf("%d",&length);
char buff[length];
fread(&buff,length,1,fp_in);
fwrite(&buff,length,1,fp_out);
fclose(fp_in);
fclose(fp_out);
}
我的思路是这样的,你可以自己根据这个内容在修改一下,有什么问题在提出来,大家共同解决!

回答2:

//c++版本,调试通过。
#include
#include
FILE *fpRead,*fpWrite;
FILE * FileOpen(char FileName[]) //文件打开函数
{
FILE *fp;
if((fp=fopen(FileName,"r"))==NULL)
{
fp=fopen(FileName,"w");
cout<<"文件打开失败重新创建记录文件";
return fp;
}
fp=fopen(FileName,"r+");
return fp;
}

void FileClose(FILE *fp) // 文件关闭函数
{
if(fclose(fp)==0);
else
cout<<"文件关闭失败"<}
void main()
{
char rFileName[15],wFileName[15],Stringtemp[30];
int placestart=0,number=0;
cout<<"请输入要读取的文件的名称:(如d:\\test.txt)";
cin>>rFileName;
cout<<"请输入从第几字节开始读取:(如d:\\test.txt)";
cin>>placestart;
cout<<"请输入读取字节数:";
cin>>number;
fpRead=FileOpen(rFileName);
fseek(fpRead,placestart,0);
fread(&Stringtemp,number,1,fpRead);
cout< FileClose(fpRead);
cout<<"请输入要写入的文件的名称";
cin>>wFileName;
cout<<"请输入从第几字节开始写";
cin>>placestart;
fpWrite=FileOpen(wFileName);
fseek(fpWrite,placestart,0);
fwrite(&Stringtemp,number,1,fpWrite);
FileClose(fpWrite);
cout<<"写入完成"<}

回答3:

内容不全,linux下的dd程序源码

int
main (int argc, char **argv)
{
int i;
int exit_status;
off_t offset;

initialize_main (&argc, &argv);
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);

/* Arrange to close stdout if parse_long_options exits. */
atexit (close_stdout);

page_size = getpagesize ();

parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, VERSION,
usage, AUTHORS, (char const *) NULL);
if (getopt_long (argc, argv, "", NULL, NULL) != -1)
usage (EXIT_FAILURE);

/* Initialize translation table to identity translation. */
for (i = 0; i < 256; i++)
trans_table[i] = i;

/* Decode arguments. */
scanargs (argc, argv);

apply_translations ();

if (input_file == NULL)
{
input_file = _("standard input");
set_fd_flags (STDIN_FILENO, input_flags, input_file);
}
else
{
if (fd_reopen (STDIN_FILENO, input_file, O_RDONLY | input_flags, 0) < 0)
error (EXIT_FAILURE, errno, _("opening %s"), quote (input_file));
}

offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
input_seekable = (0 <= offset);
input_offset = offset;
input_seek_errno = errno;

if (output_file == NULL)
{
output_file = _("standard output");
set_fd_flags (STDOUT_FILENO, output_flags, output_file);
}
else
{
mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
int opts
= (output_flags
| (conversions_mask & C_NOCREAT ? 0 : O_CREAT)
| (conversions_mask & C_EXCL ? O_EXCL : 0)
| (seek_records || (conversions_mask & C_NOTRUNC) ? 0 : O_TRUNC));

/* Open the output file with *read* access only if we might
need to read to satisfy a `seek=' request. If we can't read
the file, go ahead with write-only access; it might work. */
if ((! seek_records
|| fd_reopen (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0)
&& (fd_reopen (STDOUT_FILENO, output_file, O_WRONLY | opts, perms)
< 0))
error (EXIT_FAILURE, errno, _("opening %s"), quote (output_file));

#if HAVE_FTRUNCATE
if (seek_records != 0 && !(conversions_mask & C_NOTRUNC))
{
uintmax_t size = seek_records * output_blocksize;
unsigned long int obs = output_blocksize;

if (OFF_T_MAX / output_blocksize < seek_records)
error (EXIT_FAILURE, 0,
_("offset too large: "
"cannot truncate to a length of seek=%"PRIuMAX""
" (%lu-byte) blocks"),
seek_records, obs);

if (ftruncate (STDOUT_FILENO, size) != 0)
{
/* Complain only when ftruncate fails on a regular file, a
directory, or a shared memory object, as POSIX 1003.1-2004
specifies ftruncate's behavior only for these file types.
For example, do not complain when Linux 2.4 ftruncate
fails on /dev/fd0. */
int ftruncate_errno = errno;
struct stat stdout_stat;
if (fstat (STDOUT_FILENO, &stdout_stat) != 0)
error (EXIT_FAILURE, errno, _("cannot fstat %s"),
quote (output_file));
if (S_ISREG (stdout_stat.st_mode)
|| S_ISDIR (stdout_stat.st_mode)
|| S_TYPEISSHM (&stdout_stat))
error (EXIT_FAILURE, ftruncate_errno,
_("truncating at %"PRIuMAX" bytes in output file %s"),
size, quote (output_file));
}
}
#endif
}

install_signal_handlers ();

start_time = gethrxtime ();

exit_status = dd_copy ();

quit (exit_status);
}