Sponsored Content
Top Forums Programming Difference between cp and mv linux command Post 302424665 by dharshini123 on Wednesday 26th of May 2010 03:18:31 AM
Old 05-26-2010
MySQL Difference between cp and mv linux command

Hi,

I am facing one problem only with mv command not with cp command. I have a test program

Code:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <fcntl.h>
#include <errno.h>

int sync_file(char *file)
{
FILE *fp=NULL;
int fd;

printf("file is %s\n",file);
fp = fopen(file, "r");
if(!fp)
return -1;

fd = fileno(fp);
fflush(fp);
fsync(fd);
ioctl (fd, BLKFLSBUF, 0);
fclose(fp);
return 0;

}

int main()
{
int len=0;
FILE *fp = NULL;
char buf[1024];
char *fname = "/etc/test.conf";
char fname_tmp[129] = "";


len = sprintf(buf, "%s\n", "Newly added Line is there");

snprintf(fname_tmp, 128, "%s.tmp", fname);

if( (fp = fopen(fname_tmp,"a")) == NULL )
printf(" ERROR: open(), error - %s\n",strerror(errno));

fprintf(fp,"%s",buf);
fflush(fp);

fsync(fileno(fp));
fclose(fp);
system("cp -f /etc/test.conf.tmp /etc/test.conf");
// system("mv -f /etc/test.conf.tmp /etc/test.conf");
sync_file(fname);
return 0;
}

Here i am opening a tmp file for writing. Then i am copying/moving for original file. Then i do a fflush, fsync(), ioctl() to the original file. Then i run this binary in linux machine(ext2 file system, 2.6.23.5 kernel) after that immediately power off the machine. Then power on machine, the file is disappeared or written data lost or file gets corrupted if i move the tmp file to the original file. And there is a no problem if i copy the tmp file to original file. So i want to know the difference between the cp and mv command. Can you please give me suggestion on it?

Thanks,
Indira

Moderator's Comments:
Mod Comment Moved post to its own thread - please no "threadjacking"; removed colour formatting; added code tags
 

9 More Discussions You Might Find Interesting

1. Programming

Difference in LINUX and SOLARIS

/* * one-way-pipe.c - example of using a pipe to communicate data between a * process and its child process. The parent reads input * from the user, and sends it to the child via a pipe. * The child prints the received data to the... (1 Reply)
Discussion started by: SQ4LIFE
1 Replies

2. UNIX for Dummies Questions & Answers

Difference between UNIX and Linux

OK, I've used various versions of UNIX(Solaris, HPUX, etc..) over the years. Now the organization I work for is leaning towards more Linux based systems(Redhat, Suse, etc..) I do see differences in in comands and how to accomplish basic adminstration, but nothing mind blowing. So, what is it... (5 Replies)
Discussion started by: pbonilla
5 Replies

3. UNIX for Dummies Questions & Answers

su command difference between unix and linux

Hello all the su with -l option is running normal with linux but when i try to run it on unix AIX 5.2.7 it's not working with -l option any help (6 Replies)
Discussion started by: islam.said
6 Replies

4. UNIX for Dummies Questions & Answers

difference between unix and linux

Hi I am new to linux I have dout waht is the difference between UNIX and LINUX Is there any soft for insatallation for UNIX OS Thanks (0 Replies)
Discussion started by: sanjaya
0 Replies

5. Linux

Difference between Windows and Linux

Hi, What is the difference between Linux and Windows? Thanks. (1 Reply)
Discussion started by: billcrosby
1 Replies

6. Linux

What is the difference between Linux and Windows?

Hi, What is the difference between Linux and Windows? Thanks. (5 Replies)
Discussion started by: billcrosby
5 Replies

7. UNIX for Dummies Questions & Answers

Difference between UNIX and Linux

hi experts please tell me the real difference between unix and linux at kernel structure (1 Reply)
Discussion started by: linurag
1 Replies

8. Red Hat

Difference Redhat Linux/RH Enterprise Linux

what is the difference between Redhat Linux and Redhat Enterprise Linux. whereas Redhat linux have Server installation options too. (2 Replies)
Discussion started by: hananabbas
2 Replies

9. UNIX for Dummies Questions & Answers

AIX to Linux command difference

Moving from AIX 6.1 to RHEL 6.6, I have noticed a few command differences. One that has been causing issue is a simple echo command when I have to use it this way -> "echo -e" On the AIX it outputs to "-e" but since RHEL has "-e" as an option for echo and hence it outputs to blank here. All... (3 Replies)
Discussion started by: aster007
3 Replies
FFLUSH(3)						     Linux Programmer's Manual							 FFLUSH(3)

NAME
fflush - flush a stream SYNOPSIS
#include <stdio.h> int fflush(FILE *stream); DESCRIPTION
For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underly- ing write function. For input streams associated with seekable files (e.g., disk files, but not pipes or terminals), fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application. The open status of the stream is unaffected. If the stream argument is NULL, fflush() flushes all open output streams. For a nonlocking counterpart, see unlocked_stdio(3). RETURN VALUE
Upon successful completion 0 is returned. Otherwise, EOF is returned and errno is set to indicate the error. ERRORS
EBADF stream is not an open stream, or is not open for writing. The function fflush() may also fail and set errno for any of the errors specified for write(2). ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7). +----------+---------------+---------+ |Interface | Attribute | Value | +----------+---------------+---------+ |fflush() | Thread safety | MT-Safe | +----------+---------------+---------+ CONFORMING TO
C89, C99, POSIX.1-2001, POSIX.1-2008. POSIX.1-2001 did not specify the behavior for flushing of input streams, but the behavior is specified in POSIX.1-2008. NOTES
Note that fflush() flushes only the user-space buffers provided by the C library. To ensure that the data is physically stored on disk the kernel buffers must be flushed too, for example, with sync(2) or fsync(2). SEE ALSO
fsync(2), sync(2), write(2), fclose(3), fileno(3), fopen(3), setbuf(3), unlocked_stdio(3) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. GNU
2017-09-15 FFLUSH(3)
All times are GMT -4. The time now is 09:59 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy