please give me a simple file handling program

Reply

Join Date: Jan 2008
Posts: 13
Reputation: wolverine_ramir is an unknown quantity at this point 
Solved Threads: 0
wolverine_ramir wolverine_ramir is offline Offline
Newbie Poster

please give me a simple file handling program

 
0
  #1
Feb 11th, 2008
i need a sample program for handling text files using c language.. i don't know the syntaxes.. please help me and provide some comments on the source code so that i'll be on track..

Things that i need:
1) How to locate the text file
2) How to input values in that text file
3) How to read the texts in the text file
4) What are the things that i need to consider in creating the program


Thanks a lot...
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: please give me a simple file handling program

 
0
  #2
Feb 11th, 2008
>How to locate the text file
What do you mean by this? Do you mean "open" the file when you already know the name? Or are you searching for a text file on your hard drive?

>How to input values in that text file
>How to read the texts in the text file

http://www.cprogramming.com/tutorial/cfileio.html

>What are the things that i need to consider in creating the program

Make sure you don't use feof() to check if you're at the end of the file. See this explanation:
http://www.gidnetwork.com/b-58.html
Last edited by John A; Feb 11th, 2008 at 9:56 pm. Reason: ack, I forgot this is C, not C++!
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 13
Reputation: wolverine_ramir is an unknown quantity at this point 
Solved Threads: 0
wolverine_ramir wolverine_ramir is offline Offline
Newbie Poster

Re: please give me a simple file handling program

 
0
  #3
Feb 11th, 2008
yes.. to locate a textfile on my hard drive so that i could read the text file and display its contents.. thanks for the links..
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: please give me a simple file handling program

 
0
  #4
Feb 12th, 2008
the very basic basics of C file handling.

  1. if ((fp = fopen(filename,"r"))==NULL)
  2. {
  3. printf("cannot open file `%s`\n",filename);
  4. exit (1);
  5. }
  6.  
  7. while (fgets(buf,80,fp)) // this assumes text lines are less than 80 chars!
  8. {
  9. if (strstr(buf,"waldo") != NULL) {
  10. printf("found him!");
  11. waldo++;
  12. }
  13. printf("I found waldo on %d lines.\n",waldo);
  14. }
  15.  
  16. fclose (fp);

this will get you started.

remember you always need to #include <fileio.h>
Last edited by jephthah; Feb 12th, 2008 at 1:57 am.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,821
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 117
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: please give me a simple file handling program

 
0
  #5
Feb 12th, 2008
Originally Posted by jephthah View Post
the very basic basics of C file handling.



remember you always need to #include <fileio.h>
I guess you meant stdio.h .
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: please give me a simple file handling program

 
0
  #6
Feb 12th, 2008
Originally Posted by ithelp View Post
I guess you meant stdio.h .
oops, yeah. thanks

and i also meant to put the last "printf" statement *AFTER* the close of the while() block.

Last edited by jephthah; Feb 12th, 2008 at 2:22 am.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: please give me a simple file handling program

 
0
  #7
Feb 12th, 2008
Originally Posted by jephthah View Post
Originally Posted by ithelp View Post
I guess you meant stdio.h .
oops, yeah. thanks

and i also meant to put the last "printf" statement *AFTER* the close of the while() block.

This is why we recommend you test code you post. It generally doesn't help the poster if you give bad advice --- they rarely know it's bad.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: please give me a simple file handling program

 
0
  #8
Feb 12th, 2008
well I am new here .... but I didn't think the purpose of this site is to supply completely error-free and 100% syntactically-correct source code for people to cut-and-paste. thats what homework services do. im just giving ideas and a framework of code fragments on how to solve the problem.

anyhow, there's got to be some amount of effort and initiative to solve problems here. amirite?
Last edited by jephthah; Feb 12th, 2008 at 1:19 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: please give me a simple file handling program

 
0
  #9
Feb 12th, 2008
Originally Posted by jephthah View Post
well I am new here ....
So? Isn't it obvious even to someone new you don't want to answer a question with invalid advice?


Originally Posted by jephthah View Post
but I didn't think the purpose of this site is to supply completely error-free and 100% syntactically-correct source code for people to cut-and-paste. thats what homework services do. im just giving ideas and a framework of code fragments on how to solve the problem.

anyhow, there's got to be some amount of effort and initiative to solve problems here. amirite?
You are absolutely correct. But if the small snippets of code you post are filled with errors in syntax and/or logic, how have you really helped someone? They get confused, then post "I don't understand why your advice didn't work!", then you have to apologize and correct your post. They've lost a day because of an invalid code snippet. That's what I'm saying. So try to verify before you post, that's all.

And of course errors slip in. But fewer will by testing/proofreading.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: please give me a simple file handling program

 
0
  #10
Feb 12th, 2008
"filled with errors" ??

step off my grill, dog. the code snippet -- as it is written -- *works* and it is entirely suitable for someone wanting code handed to them.

now tell me, do you always engage in such tendentious pedantry on trivial matters, or do you reserve this unappealing behavior only for the new people?

:roll:
Last edited by jephthah; Feb 12th, 2008 at 3:27 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC