User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,588 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,582 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1250 | Replies: 10 | Solved
Reply
Join Date: Oct 2007
Posts: 20
Reputation: pdwivedi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pdwivedi pdwivedi is offline Offline
Newbie Poster

file handeling

  #1  
Nov 3rd, 2007
I want to open a text file in a c program which is on desktop.How can I print the texts written in the file on screen.Please help


Regards
Prashant
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: file handeling

  #2  
Nov 3rd, 2007
Simple file io. First hit on google - http://www.cprogramming.com/tutorial/cfileio.html. All you need to do is point the file pointer to your desktop. You can hard code that if you want. Then open the file for reading (see link), and print its contents to screen.
Last edited by twomers : Nov 3rd, 2007 at 8:36 am.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Oct 2007
Posts: 20
Reputation: pdwivedi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pdwivedi pdwivedi is offline Offline
Newbie Poster

Re: file handeling

  #3  
Nov 3rd, 2007
i tried to create a simple test program bu it could not print the contents of the file myfile.txt.
please help!

  1. #include<stdio.h>
  2. int main()
  3. {
  4. FILE *fp;
  5. fp=fopen("C:\\Documents and Settings\\Administrator\\Desktop\\myfile.txt", "w");
  6. fprintf(fp, "Testing...\n");
  7. getch();
  8. }

Regards
Prashant
Last edited by WaltP : Nov 3rd, 2007 at 1:54 pm. Reason: Removed unnecessary color and Added CODE tags -- you actually typed right over how to use them when you entered this post...
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: file handeling

  #4  
Nov 3rd, 2007
Do you want to print the contents to your screen or to the file? The way you opened the file there was for writing, that's what the w stands for. I was under the impression that you wanted to read the file. Use r for that. Look into fgets, or fscanf (if memory serves). http://www.cplusplus.com/reference/c...dio/fgets.html
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Oct 2007
Posts: 20
Reputation: pdwivedi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pdwivedi pdwivedi is offline Offline
Newbie Poster

Re: file handeling

  #5  
Nov 3rd, 2007
I want to print the contents on screen and I also tried with "r" instead of "w" but it is not working!!

  1. #include<stdio.h>
  2. #include<conio.h>
  3. int main()
  4. {
  5. FILE *fp;
  6. fp=fopen("C:\\Documents and Settings\\Administrator\\Desktop\\myfile.txt", "r");
  7. fprintf(fp, "Testing...\n");
  8. getch();
  9. }
Last edited by WaltP : Nov 3rd, 2007 at 1:52 pm. Reason: Removed unnecessary color and Added CODE tags -- you actually typed right over how to use them when you entered this post...
Regards
Prashant
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: file handeling

  #6  
Nov 3rd, 2007
Did you even look at the link I sent? Gah!

http://www.cplusplus.com/reference/c...dio/fgets.html
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Oct 2007
Posts: 20
Reputation: pdwivedi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pdwivedi pdwivedi is offline Offline
Newbie Poster

Re: file handeling

  #7  
Nov 5th, 2007
can u help me what I am doing wrong in above program??
Regards
Prashant
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: file handeling

  #8  
Nov 5th, 2007
Originally Posted by pdwivedi View Post
can u help me what I am doing wrong in above program??

Yes, read the link you were given twice. It tells you how to do what you want. If not, ask a specific question.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: Oct 2007
Posts: 20
Reputation: pdwivedi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pdwivedi pdwivedi is offline Offline
Newbie Poster

Re: file handeling

  #9  
Nov 5th, 2007
Originally Posted by WaltP View Post
Yes, read the link you were given twice. It tells you how to do what you want. If not, ask a specific question.

my question is should I give the full path name as C :\\Documents and Settings\\Administrator\\Desktop\\myfile.txt in the as fopen("C:\\Documents and Settings\\Administrator\\Desktop\\myfile.txt", "r") function or should I give only fopen ("myfile.txt" , "r") in order to open a file from desktop and display its content on screen.
Regards
Prashant
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: file handeling

  #10  
Nov 5th, 2007
Originally Posted by pdwivedi View Post
my question is should I give the full path name as C :\\Documents and Settings\\Administrator\\Desktop\\myfile.txt in the as fopen("C:\\Documents and Settings\\Administrator\\Desktop\\myfile.txt", "r") function or should I give only fopen ("myfile.txt" , "r") in order to open a file from desktop and display its content on screen.

Then that's what you should ask.

Use the full path...
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 6:37 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC