LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Trouble with file I/O (https://www.linuxquestions.org/questions/programming-9/trouble-with-file-i-o-135122/)

WindowsBurner 01-15-2004 12:04 PM

Trouble with file I/O
 
Hey all.. I'm trying to write a text-editor without using any add-on libraries(every piece of code is written by me.) And I'm having trouble with this piece of code :

#include <stdio.h>

int main()
{
FILE *edit_fp;
char buffer[256];
int i = 0;

if (( edit_fp = fopen("me", "w")) == NULL)
{
puts("Trying to open");
puts("me");
puts("in mode \'w\'.Could not.");
exit(-1);
}

while(( gets(buffer)) != NULL)
fprintf(edit_fp,"%s", buffer);

fclose(edit_fp);
return 0;
}

Its supposed to open the file me in mode w print the text the user types to the file.
Could somebody tell me how I'm doing the
while(( gets(buffer)) != NULL)
fprintf(edit_fp,"%s", buffer);
piece wrong?

Thanks in advance.
WB

kev82 01-15-2004 12:28 PM

by WindowsBurner
without using any add-on libraries(every piece of code is written by me.)

you seem to be using a little bit of glibc :)

it works fine for me, after youve finished typing press C-d to signify EOF this will make gets() return NULL and the loop will end.

<edit>
if i dont say it someone else will so to save them the trouble: dont use gets() you are just asking for a buffer overflow use fgets(stdin, ...) instead
</edit>

WindowsBurner 01-15-2004 12:32 PM

lol all right let me claify that....I don't want to use anything but the standard C library.:D
Thanks kev.....my problem was that I kept hitting C-c instead of C-d.
Thanks alot.Now all I have left to tackle is memory-buffer management...:( this is gonna be hard....oh well....thanks agian

WB

<edit>
Okay. I was just thinking of that myself...I'll use that instead...thanks!
</edit>


All times are GMT -5. The time now is 04:12 AM.