Thread: Beginner file open and close

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    10

    Beginner file open and close

    Hello i'am beginner, i dont see why this arnt running.

    Code:
    #include <stdio.h>
    int main ()
    {
      ;; char pFile, oFile;
    
    
      FILE pFile, oFile;
      pFile = fopen ("my.txt","r");
      oFile = fopen ("mymymy.txt" "w");
      fclose (pFile, oFile);
      return 0;
    }
    i will only that the comment in "my.txt" will by copy to "mymymy.txt"
    thanks meny time for any help
    regards
    Mauri

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    Well just opening both files isn't going to copy anything unless you read from one and write to the other.

    Also, fclose() can only close one file at a time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,784
    Also, you may want to turn on your compiler warnings and check what type fopen() returns as well as making sure that it's not NULL

  4. #4
    Registered User
    Join Date
    Oct 2018
    Posts
    10
    Hello together
    thank you for your advice that it has certainly brought me a little further!

    Unfortunately I now have the problem that it no longer compiles, since I have probably added some errors :-)

    Code:
    #include <stdio.h>
    int main ()
    
     {
      FILE *source, *destin;
      char ch;
      source = fopen ("my.txt","r");
      if(source==NULL)
    
      destin = fopen ("mymymy.txt","w");
      if(source==NULL)
     }
       do
     {
       ch = fgetc(source);
       fputc(ch, destin);
     } 
    
      while(ch!=EOF);
    
      fclose (source);
      fclose (destin);
    
      return 0;
    }
    Compiling Error Message:
    Code:
    C:\Temp\tcc>tcc.exe 25.c
    25.c:12: error: too few arguments to function
    I'm still on the right track here with my snippets?
    regards
    Last edited by mauric; 07-18-2020 at 04:26 AM.

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    10
    now its running, but i need little more help!?
    why it's do on the last line this symbol?

    Code:
    
    


    nice :-)
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int main()
    {
     FILE *source, *destination;
     char ch;
     source = fopen("c://folder//filename.txt","r");
    
    
     if(source==NULL)
     {
      printf("File error!");
      return 1;
     }
    
    
     destination = fopen("//server//folder//filename.txt","w");
     if(source==NULL)
     {
      printf("File error!");
      return 1;
     }
    
    
     do
     {
      ch = fgetc(source);
      fputc(ch, destination);
     }
      while(ch != EOF);
    
    
     fclose(source);
     fclose(destination);
    
    
     printf("Program completed...");
    
    
     return 0;
    }

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    That's probably because you are not "syncing" your read and write operations properly.

    First it is usually a mistake to use EOF to control a data entry loop, remember that EOF is not set until after the file tries to read past the end of file.

    Second in your do loop you are checking for EOF after you try to write the information to the file so you will probably get some garbage characters written to the file.

    Something like the following would probably be better,
    Code:
        while((ch = fgetc(source)) != EOF)
        {
           fputc(ch, destination);
        }

  7. #7
    Registered User
    Join Date
    Oct 2018
    Posts
    10
    thanks for your update... yes this entry i had read from any page googleing little... now i have this implement.
    but stil exist

    Code:
    
    

    Code:
    destination = fopen("c://temp//tcc//mymymy.txt","w"); if(source==NULL)
     {
      printf("File error!");
      return 1;
     }
    
    
        while ((ch = fgetc(source)) != EOF);
          {
           fputc(ch, destination);
          }
    
    
     fclose(source);
     fclose(destination);

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    > while ((ch = fgetc(source)) != EOF);
    Remove the trailing ;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    739
    Two other problems from your code above:
    Code:
     destination = fopen("//server//folder//filename.txt","w");
     if(source==NULL)
     {
      printf("File error!");
      return 1;
     }
    Do you see a problem?

    ch should be an int, not a char. EOF is an integer not a char. An int can contain a char or a full int, such as EOF.

    Run the following:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int ch = 0;
    
       ch = 'a';
       printf("Hex: %x, int: %d, char: %c\n", ch, ch, ch);
    
       ch = EOF;
       printf("Hex: %x, int: %d\n", ch, ch);
    
       return 0;
    }
    output:
    Code:
    Hex: 61, int: 97, char: a
    Hex: ffffffff, int: -1

  10. #10
    Registered User
    Join Date
    Oct 2018
    Posts
    10
    thanks !!!

    ok now after this update, the result from "Desintation" are correctly visible, realy very very very nice!!

    also i delete the "
    if (source & destionation == NULL)" line
    and the output are right!?

    please one question, the code here sems to right? or its horrible :-)

    also one more question here in this example "i copy the inside document like copy+paste" what are the way to realy copy any file from "a to b"


    Update from Code
    Code:
    #include<stdio.h>
    int main()
    {
     printf(" lalala \n");
     printf(" (MCCopyFile) V0.2\n");
     printf("Program completed...");
    
     FILE *source, *destination;
     int ch;
    
     source = fopen("c:\\temp\\tcc\\my.txt","r");
     destination = fopen("c:\\temp\\tcc\\mymymy.txt","w");
        
       while(1)
       {
         ch = fgetc(source);
         if(ch == EOF)
         {
           break;
         }
         else
       {
         fputc(ch,destination);
       }
     }
     fclose(source);
     fclose(destination);
    
     return 0;
    }


    thanks

  11. #11
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    739
    You eliminated the checks to see if the fopen() calls worked correctly. You should always check the return from fopen(). (And many other Standard Library functions!) If the file does not open correctly, it makes no sense to continue to run the program

    Providing you can successfully open the two files correctly, yes, your code as written, does work. You should always initialize your local variables.

    There is no function in the Standard Library to copy one file to another. It is possible that some compilers may have non-standard extension functions. Otherwise, you have to create that file yourself.

    One other way to copy one file to another is to use fread() and fwrite(), to read in chunks of bytes at a time, such as 1024 bytes, or larger. (Or fgets() for text files) Especially for large files.

  12. #12
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    302

    Post

    Code:
    #include <stdio.h>
    
    int respond(char *filename) {
        FILE *fp = fopen(filename,"r");
        if (fp != NULL) { fclose(fp);
            return 1;
        } else { return 0; }
    }
    
    int main() {
        
        if ( respond( "example.txt" ) ) {
            printf("file found. ") ;
        } else {
            printf("file not found. ");
        }
    
        return 0;
    }
    Code:
    #include <stdio.h>
    
    int respond(char *filename);
    
    int main() {
        if (respond("my.txt")) {
            // 
        } else {
            if (respond("mymymy.txt")) {
                //
            } else {
                //
            }
        }
    }
    Last edited by Structure; 07-21-2020 at 04:22 PM.
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to open/close the CD/DVD/ROM/RW ?
    By automat in forum C Programming
    Replies: 4
    Last Post: 10-21-2011, 02:38 PM
  2. Replies: 2
    Last Post: 12-05-2007, 10:56 AM
  3. Open IE, go to URL, close
    By ober in forum C++ Programming
    Replies: 10
    Last Post: 04-07-2006, 02:21 PM
  4. open() and close()
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 04-08-2005, 01:16 PM
  5. Open, search, act, close.
    By RoD in forum C++ Programming
    Replies: 12
    Last Post: 10-13-2002, 03:48 PM

Tags for this Thread