Thread: question about the file

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    41

    question about the file

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /*
     * 
     */
    int main(void) {    
        /* Create a file */
        FILE *file1, *file2, *file3;
        char buff[BUFSIZ];
        int cha;  
        int cha1;
        int tem  = 0;
        file1 = fopen("infile1","w");    
        /* Get charater from the file and compare to EOF */
        while( fgets(buff, sizeof(buff), stdin)) {
            tem = 0;
            while ( tem < strlen(buff) ) {           
                fputc((int)buff[tem],file1);
                tem = tem + 1;
            }
        }
        fclose(file1);  
        return 0;
    }
    At a line file1 = fopen("infile1","w"); arethere any modes to extend the file ( add more information into file without deleting the old information)
    Last edited by thungmail; 03-13-2008 at 09:13 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes. "a".

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    41
    I changed as you said and everything is one line. I assume I input 2 lines of character from input and I want to get 2 lines of character in infile1.
    Last edited by thungmail; 03-13-2008 at 09:19 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Works for me. I had to add
    #include<string.h>
    though.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    41
    even though, I added #include<string.h>,it sitll does not work. I got one line of characters in infile1.Can you post the code up

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by thungmail View Post
    even though, I added #include<string.h>,it sitll does not work. I got one line of characters in infile1.Can you post the code up
    No. I used your code. Whatever I typed I got in the file -- multiple lines until I hit EOF (ctrl-Z on this win machine; you might need ctrl-D on a *nix).

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    41
    If I hit EOF (ctrl-Z on win machine) i got nothing
    If I hit Ctrl-C on win machine I got a single line
    I switch to linux and I got what i want. I dont understand

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    When doing this from the keyboard, ^Z has to be on a line by itself. You can also redirect a file from the command line.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /*
     * 
     */
    int main(void) {    
        /* Create a file */
        FILE *file1;
        char buff[BUFSIZ];
        file1 = fopen("infile1","a");    
        /* Get charater from the file and compare to EOF */
        while( fgets(buff, sizeof(buff), stdin)) {
            size_t tem = 0;
            while ( tem < strlen(buff) ) {           
                fputc((int)buff[tem],file1);
                tem = tem + 1;
            }
        }
        fclose(file1);  
        return 0;
    }
    Input:
    Code:
    abc
    def
    ^Z
    Resulting file
    Code:
    abc
    def
    MSVC++2008 Express
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    41
    after that how can I make "infile1" file empty

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    open it with "w"
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    41
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /*
     * 
     */
    int main(void) {    
        /* Create a file */
        FILE *file1;
        char buff[BUFSIZ];
        file1 = fopen("infile1","a");    
        /* Get charater from the file and compare to EOF */
        while( fgets(buff, sizeof(buff), stdin)) {
            size_t tem = 0;
            while ( tem < strlen(buff) ) {           
                fputc((int)buff[tem],file1);
                tem = tem + 1;
            }
        }
        fclose(file1);
        file1 = fopen("infile1","w");      
        return 0;
    }
    can i put at then end ( the red line)

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    yes - but it means - after you program is finished your file will be empty...
    and do not forget to close it as well
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    41
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /*
     * 
     */
    int main(void) {    
        /* Create a file */
        FILE *file1;
        char buff[BUFSIZ];
        file1 = fopen("infile1","a");    
        /* Get charater from the file and compare to EOF */
        while( fgets(buff, sizeof(buff), stdin)) {
            size_t tem = 0;
            while ( tem < strlen(buff) ) {           
                fputc((int)buff[tem],file1);
                tem = tem + 1;
            }
        }
        fclose(file1);
        file1 = fopen("infile1","w");      
        fclose(file1);
        return 0;
    }
    so is it better now ?

  15. #15
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Save your computer some CPU cycles, and write the best version of this program.
    Code:
    int main(void) {
        return 0;
    }
    because you just wrote a program that writes a file and deletes the written file,
    so why have a program within main anyway?
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM