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); }
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); }
you have not allocated any memory for char *text, it is just a random uninitialized pointer.
hello, internet!
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)
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!
> 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
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.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 ?
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]
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); }
>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]
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)
?
>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]
>It should keep taking the values and write to the file
-PreludeCode:#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; }
My best code is written with the delete key.
Thans very much for the help..
I typed the following:
but only this got saved...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
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.adladsffkjadsf adf lkjasdf l;kjadf ;lkajdf als;kjf adls;kfjadf;l akj l;kjadf
This works fine for me:
If you CTRL+C out of the program then it won't work as you expect it to.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
-Prelude
My best code is written with the delete key.
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]