LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2004, 12:04 PM   #1
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Rep: Reputation: 30
Angry 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
 
Old 01-15-2004, 12:28 PM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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>

Last edited by kev82; 01-15-2004 at 12:32 PM.
 
Old 01-15-2004, 12:32 PM   #3
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Original Poster
Rep: Reputation: 30
lol all right let me claify that....I don't want to use anything but the standard C library.
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>

Last edited by WindowsBurner; 01-15-2004 at 12:33 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble working with file dates cboxall Linux - Newbie 2 07-21-2005 11:11 AM
Trouble with XF86Config File masterweaver Linux - Software 4 09-16-2004 02:09 AM
Trouble with an 8GB file Jonasx Linux - Software 7 05-26-2004 12:33 AM
Trouble with file permissions daekpon Linux - General 8 03-15-2004 10:57 AM
trouble with file sharing in RH8 ayman Linux - Networking 0 04-22-2003 03:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration