codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <stdio.h> #include <stdlib.h> //argc holds how many parameters the program received when called //argv holds their values int main( int argc , char const *argv[] ) { /* ENSURE THE FILE NAME WAS PASSED */ if( argc != 2 ) //make sure they submitted the file name we are going to copy { printf( "Please list the file name as a parameter to this program.\n" ); exit(1); //exit with an error } /* ENSURE THE FILE EXISTS, AND WE CAN OPEN A TEMP FILE */ char const* file_name = argv[1]; //use the filename passed in from the command line char tmp_name[L_tmpnam]; //stores the name of the temporary file, L_tmpnam holds the size it needs to be tmpnam(tmp_name); //get a name for our temporary output file, see http://www.gnu.org/s/libc/manual/html_node/Temporary-Files.html FILE *fin = fopen( file_name , "r" ); //fin is the file input pointer FILE *fout = fopen( tmp_name , "w" ); //we will copy the desired contents into the output file if( !fin ) printf( "%s could not be opened.\n" , file_name ); //make sure the file we are reading from exists if( !fout ) printf( "There was an error opening a file.\n" ); //make sure the file we are writing to exists if( !fin || !fout ) exit(1); //exit with an error /* COPY THE FILE */ int c; //we will store the file's character in c for( c=getc(fin) ; c != EOF ; c=getc(fin) ) //count characters and new lines in the file { fputc( c , fout ); //copy it to the new file if( c == '\n' ) //if it is a newline fputc( '\n' , fout ); //then copy it agian } /* REPLACE THE ORIGINAL FILE WITH THE NEW ONE */ fclose(fin); //close the files fclose(fout); remove(file_name); //remove the single-lined file rename(tmp_name,file_name); //rename the double-lined file to hold it's value return 0; }
Private
[
?
]
Run code