Project in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 4emens
    New Member
    • Nov 2009
    • 6

    Project in C

    Hello i have to make a project in C that will make MS DOS move command in C

    i have done this and this does copy the file but i cant make a code to paste it too...can someone hlp me?

    #include <stdio.h>
    #include <string.h>

    int main(int argc, char *argv[] ){
    FILE *alfa;
    FILE *tempf;
    FILE *vita;
    char c, name[15], name2[15];
    int i=0;

    if (argc<3){
    printf ("\n \n");
    return 1;
    }

    strcpy(name, argv[1]);
    strcpy(name2, argv[2]);


    while (name[i]!='\0'){
    if (name[i]=='_'){
    name[i]=' ';
    i++;
    }
    else{
    name[i]=name[i];
    i++;
    }
    }

    i=0;

    while (name2[i]!='\0'){
    if (name2[i]=='_'){
    name2[i]=' ';
    i++;
    }
    else{
    name2[i]=name2[i];
    i++;
    }
    }

    alfa=fopen(name ,"r");



    tempf=fopen("te mp","w");

    while ((c = fgetc (alfa)) != EOF)
    fputc(c,tempf);


    fclose (tempf);
    fclose(alfa);
    remove (name);

    tempf=fopen("te mp","r");
    vita=fopen(name 2,"w");
    while ((c = fgetc (tempf)) != EOF)
    fputc(c,vita);


    fclose (tempf);
    remove ("temp");
    fclose (vita);




    return 0;

    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Copy the file top an array.

    The paste is just copying that array wherever you want.

    Comment

    • 4emens
      New Member
      • Nov 2009
      • 6

      #3
      I dont understand what u mean.....

      Comment

      • RRick
        Recognized Expert Contributor
        • Feb 2007
        • 463

        #4
        I think what 4emens is asking: How to copy a file if the file is copied and then pasted/dropped on the executable via the gui. Or is it the other way around? Pasting/dropping the program on a file.

        Is this possible? How does one name the new file?

        Comment

        • 4emens
          New Member
          • Nov 2009
          • 6

          #5
          I have to make ms dos move command in C...this command copies a file and paste it anywhere u want...
          for example if i write at DOS move.exe copy.txt D:\project
          if my projects name is move then its going to copy the txt file and paste it at D:\project but this code copies the file...i need some help on how to paste it

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            I guess I don't understand what you mean by "move" , "copy" and "paste"

            To me a move copies the file to a destination location and deletes the file at the source location.

            A copy does the same thing to a without deleting the source location.

            A paste is a copy.

            Comment

            • RRick
              Recognized Expert Contributor
              • Feb 2007
              • 463

              #7
              I agree with W4cats that there is a lot of confusion in what you mean by move, copy and paste. For clarity, lets ignore the paste word and use only move and copy.

              Let's look at the command you are trying to mimic in C.
              move.exe copy.txt D:\project
              First of all, move is a special type of command where the file is copied to a new location and original file is deleted. You will have to use some system command to delete the original file.

              Take a look at your parameter list. The first param is a file and the second param is a directory. For this specific move, you need to
              1. Copy "copy.txt" to "D:\project\cop y.txt"
              2. Delete copy.txt

              The file names in #1 are the names of the files you have to open.

              Comment

              • 4emens
                New Member
                • Nov 2009
                • 6

                #8
                To make all clear....I want to copy the txt file from the original directory, delete the original file and paste it at the directory that i give

                My program copies the file,deletes the original but it doesnt paste it at the directory that i give

                and something last...what include does system command needs??

                Comment

                • 4emens
                  New Member
                  • Nov 2009
                  • 6

                  #9
                  i tried use the system command but it still doesnt paste the file see this and help me if u can


                  #include <stdlib.h>
                  #include <stdio.h>
                  #include <string.h>

                  int main(int argc, char *argv[] ){
                  FILE *alfa;
                  FILE *tempf;
                  FILE *vita;
                  char c, name[15], name2[15];
                  int i=0;

                  strcpy(name, argv[1]);
                  strcpy(name2, argv[2]);


                  while (name[i]!='\0'){
                  if (name[i]=='_'){
                  name[i]=' ';
                  i++;
                  }
                  else{
                  name[i]=name[i];
                  i++;
                  }
                  }

                  i=0;

                  while (name2[i]!='\0'){
                  if (name2[i]=='_'){
                  name2[i]=' ';
                  i++;
                  }
                  else{
                  name2[i]=name2[i];
                  i++;
                  }
                  }

                  alfa=fopen(name ,"r");

                  tempf=fopen("te mp","w");

                  while ((c = fgetc (alfa)) != EOF)
                  fputc(c,tempf);

                  fclose (tempf);
                  fclose(alfa);
                  remove (name);

                  tempf=fopen("te mp","r");
                  vita=fopen(name 2,"w");
                  while ((c = fgetc (tempf)) != EOF)
                  fputc(c,vita);

                  fclose (tempf);
                  remove ("temp");
                  fclose (vita);

                  printf ("\n\n finished \n\n");

                  system ("pause");
                  return 0;

                  }

                  Comment

                  • RRick
                    Recognized Expert Contributor
                    • Feb 2007
                    • 463

                    #10
                    Your name for the destination is a directory, not a file. For fopen to work correctly it needs a file name.

                    You need to check the FILE * values after you call fopen and make sure they are not 0. That is a failure result from fopen. If you don't check the value and try to use the bad FILE *, your program usually crashes and burns with a seg fault.

                    Comment

                    • newb16
                      Contributor
                      • Jul 2008
                      • 687

                      #11
                      You talk like windows explorer with its 'copy-paste' metaphor to copy file. Actually when you 'copy' file in explorer, nothing is read, just name of the file is remembered. If you delete it before you issue the 'paste' command, paste will fail.

                      system() is in stdlib.h

                      Then, 'real' move command performs no read or write, it modifies only directory content but not the file data. See rename() function documentation.

                      Comment

                      • 4emens
                        New Member
                        • Nov 2009
                        • 6

                        #12
                        I just tried to explain the move command...i wanted to say that the file changes directory and there is no file at the old one...

                        Can someone explain to me hat system() command does?

                        Comment

                        • weaknessforcats
                          Recognized Expert Expert
                          • Mar 2007
                          • 9214

                          #13
                          The system() command is a function that has srguments used to call OS commands directly.

                          Since the OS commands vary by OS, the valid system() arguments also vary. So using the system() command renders your program non-portable, which may or may not be a problem.

                          I usually avoid this thing and use the OS SDK to make my system calls,.

                          Comment

                          Working...