Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 30th, 2010, 8:12 AM   #1
singh
Newbie
 
Join Date: Jan 2010
Posts: 3
Rep Power: 0 singh is on a distinguished road
Angry reading problem from file called "INPUT"

c Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. FILE *f1;
  7. char c;
  8.  
  9. /* open the file INPUT*/
  10. f1=fopen("INPUT","r");
  11. /*read charchetrs from INPUT*/
  12. while((c=getc(f1)!=EOF))
  13. /*display character on screen*/
  14. printf("%c",c);
  15. fclose(f1);
  16.  
  17. return 0;
  18.  
  19. }
this is program for reading a file INPUT...but the problem is that when i gonna to execute the application the its run well but i cldnt able to read the data what i wrote in the file INPUT...i can write to a file INPUT well but i cldnt read a same data when i gonna to read the data....

Last edited by Ancient Dragon; Jan 30th, 2010 at 8:27 AM. Reason: add code tags
singh is offline   Reply With Quote
Old Jan 30th, 2010, 8:29 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 8,368
Rep Power: 22 DaWei will become famous soon enoughDaWei will become famous soon enough
Re: reading problem from file called "INPUT"

Read the "How to Post" thread, put your code in code tags, and explain what WAS wrong. I suspect your program closed before you had a change to read it, as it should. If so, put a getchar () immediately before the return, then press ENTER when you're ready to close.
__________________
Contributor's Corner:
Politically Incorrect
DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 30th, 2010, 8:31 AM   #3
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Re: reading problem from file called "INPUT"

line 7: variable c should be int, not char because getc() returns an int. Then on line 14 you will need to typecase c back to char like this: printf("%c", (char)c);

Otherwise I don't know why that program will not read the file. Maybe the file as an extension, such as INPUT.TXT ? Or if on *nix operating system the file name is not all upper-case like you posted? Or the file is not in the directory where you think it is. Lots of reasons why the program will not open the file.

[edit]what DaWei ^^^ said too.[/edit]
__________________
PFO's FAQ is here
Forum Rules

There is no cow level (Diablo III)
If you never push yourself you will never improve (Diablo III)
Ancient Dragon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a file in C++ GhostProgrammer C++ 14 Jan 26th, 2010 7:45 PM
Structures and Reading from File ccfc1986 C++ 14 Nov 24th, 2007 6:57 PM
Problem with opening a file raghuveerd C 8 Jun 25th, 2007 7:24 PM
Problem reading unknown # of inputs TCStyle C++ 13 Mar 14th, 2006 9:12 AM
A problem with File I/O names HaCkeR C++ 17 Mar 9th, 2006 11:29 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:31 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC