Thread: file writing crashes

  1. #1
    test
    Guest

    file writing crashes

    this code crashes, why ?:
    Code:
    #include <stdio.h>
    
    main()
    {
    	FILE *fp;
    	
    	char *text;
    	
    	fp = fopen("sometext.txt", "w");
    	
    	gets(text);
    
    	printf("%s", text);
    
    	fprintf(fp, text);
    
    	fclose(fp);
    	
    }

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    you have not allocated any memory for char *text, it is just a random uninitialized pointer.
    hello, internet!

  3. #3
    Unregistered
    Guest

    but

    i am using gets() to store the typed stuff... shouldn't that work ?

    i used the same thing... except i replaced
    *text with text[100] and it works...

    how do i do the same thing with pointers ? (i dont
    want to limit it to 100)

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    gets() just stores the data, it does not allocate memory for it.

    to use pointers you would:

    char *foo;

    foo = malloc (sizeof (char) * num);

    // do your stuff with foo now

    free (foo);

    // the end


    where num is the number of chars you want to allocate.

    but as you can see in this example, there is no advantage to doing it this way; just use char foo[somenumberthat'sbigenough]
    hello, internet!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > i am using gets() to store the typed stuff... shouldn't that work
    Never use gets()
    See http://www.eskimo.com/~scs/C-faq/q12.23.html

    > how do i do the same thing with pointers ?
    There is no standard input routine which automatically extends memory to cater for the volume of input data.
    You must read using fgets() into a fixed sized buffer, then copy that buffer to some memory which you get via malloc

  6. #6
    Unregistered
    Guest

    1 last question.

    ok. One last question...
    I am now able to do this.... I want to create an EXE that lets me type stuff continuously... it could be very long.. until the user presses ctrl + z .. and it should save it in a text file.

    if i limit the number in fgets, this is not possible, right ?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: 1 last question.

    Originally posted by Unregistered
    ok. One last question...
    I am now able to do this.... I want to create an EXE that lets me type stuff continuously... it could be very long.. until the user presses ctrl + z .. and it should save it in a text file.

    if i limit the number in fgets, this is not possible, right ?
    Read Salem's last sentence.

    Depending on what you are doing, an array, a list or a tree of some sort might suit your needs.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Unregistered
    Guest
    does this look right?

    Code:
    #include <stdio.h>
    
    main()
    {
    	FILE *fp;
    	
    	char buf[80];
    	
    	fp = fopen("some****.txt", "w");
    
    	fgets(buf, 80, stdin);
    
    	fputs(buf, fp);
    
    	fclose(fp);
    	
    }

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >main()
    Being picky here, but this should be declared as
    >int main(void)

    and your need to add something like this at the end of main:
    >return 0;

    And some error checking would be nice particularly on the fopen().

    Other than that, it looks OK to me.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Unregistered
    Guest

    now

    now how do i increase it to unlimited lines ? It should keep taking the values and write to the file, just like notepad, without GUI..

    and ending at kbhit() = 27 (escape)

    ?

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >how do i increase it to unlimited lines ? It should keep taking the values and write to the file, just like notepad, without GUI..

    If all you are doing is reading from the keyboard, then writing straight out the a file, just stick fgets() and fputs() in a loop. As you are writing straight out, you only need one buffer.

    If you want to store the data in memory, then you'll need dynamic allocation, via malloc(), and something to keep it altogether, like the linked list or trees I mentioned earlier.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >It should keep taking the values and write to the file
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      FILE *fp;
      char buf[80];
      
      fp = fopen("some****.txt", "w");
      if ( fp != NULL ) {
        /*
        ** Read until the user hits CTRL+D or CTRL+Z
        */
        while ( fgets(buf, 80, stdin) != NULL )
          fputs(buf, fp);
        fclose(fp);
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  13. #13
    Unregistered
    Guest
    Thans very much for the help..


    I typed the following:
    adladsffkjadsf adf lkjasdf l;kjadf ;lkajdf als;kjf adls;kfjadf;l akj l;kjadf
    i am testing this again to see if this really works. it has to keep 80 chars in
    the buffer and
    adsf adfa d
    adf
    a
    aadsf
    adf
    f
    af
    af
    af
    ^C
    but only this got saved...
    adladsffkjadsf adf lkjasdf l;kjadf ;lkajdf als;kjf adls;kfjadf;l akj l;kjadf
    How do I make it work completely just like notepad ? it takes in stuff only after pressing enter... and that too, it is not taking new lines.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This works fine for me:
    adladsffkjadsf adf lkjasdf l;kjadf ;lkajdf als;kjf adls;kfjadf;l akj l;kjadf
    i am testing this again to see if this really works. it has to keep 80 chars in
    the buffer and
    adsf adfa d
    adf
    a
    aadsf
    adf
    f
    af
    af
    af
    ^Z
    If you CTRL+C out of the program then it won't work as you expect it to.

    -Prelude
    My best code is written with the delete key.

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It looks like you hit CTRL+C to terminate the program. This kills the prog mid flow, and the chances are the output buffer wasn't flushed. If you're on a Windows Console, use CTRL+Z instead.

    If you're worried about loosing buffer data, use fflush(fp) to flush the output buffer whenever you want.

    [edit]beaten!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 05:13 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Writing input from a file into shared memory
    By RazielX in forum C Programming
    Replies: 2
    Last Post: 09-23-2004, 12:34 PM