Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jun 19th, 2007, 1:32 AM   #1
raghuveerd
Newbie
 
Join Date: Jun 2007
Posts: 1
Rep Power: 0 raghuveerd is on a distinguished road
Problem with opening a file

Hi
I am a beginer to C Programming.I was trying to open a .txt file for reading but couldnt do that this is my code..Help me out.
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
clrscr();
fp=fopen("raghu.txt","r");
if(fp==NULL)
printf("ERROR IN OPENING THE FILE");
else
{
printf("hi");
fprintf(fp,"\n HI");
}

fclose(fp);
getch();
}
raghuveerd is offline   Reply With Quote
Old Jun 19th, 2007, 2:23 AM   #2
niteice
Programmer
 
niteice's Avatar
 
Join Date: Aug 2005
Posts: 98
Rep Power: 4 niteice is on a distinguished road
Send a message via AIM to niteice
What do you mean "couldnt do that"? Your code looks like it should work.
niteice is offline   Reply With Quote
Old Jun 19th, 2007, 3:01 AM   #3
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 311
Rep Power: 3 Fall Back Son is on a distinguished road
This won't fix whatever problem you are having, but make that

int main ()

and

return 0 ; at the end. DaWei said that on here once, so do it! Lol.

this might fix your problem:


Your code looks like it should work to me. You might consider that you won't be able to open a file for reading if that file doesn't exist. I'm not sure if that is the case (it might create a blank file called raghu.txt), but in any case, you shouldn't try to read from a file that doesn't exist. So look in your directory and make sure that there is a file in that directory called raghu.txt .

Also, you made a call to fprintf, and sent the output to "fp". However, fp was used for reading, not for writing. You are trying to write to a file that is open for reading. This will yield unexpected results, I would imagine. If you want to open for reading and writing, use r+ instead.

Last edited by Fall Back Son; Jun 19th, 2007 at 3:13 AM.
Fall Back Son is offline   Reply With Quote
Old Jun 19th, 2007, 7:03 AM   #4
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3 pegasus001 is on a distinguished road
Post any errors that your compiler shows, if there is any. Maybe the conio.h doesn`t support the clrscr() function.
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Jun 20th, 2007, 6:33 AM   #5
cheap freelancer
Newbie
 
Join Date: Jun 2007
Location: askexpert.info
Posts: 4
Rep Power: 0 cheap freelancer is on a distinguished road
open the file in write mode.
cheap freelancer is offline   Reply With Quote
Old Jun 20th, 2007, 7:01 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
cheap freelancer, please review the forum's rules regarding commercial signatures.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 25th, 2007, 6:57 AM   #7
java_roshan
Professional Programmer
 
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4 java_roshan is on a distinguished road
Quote:
Originally Posted by pegasus001 View Post
Maybe the conio.h doesn`t support the clrscr() function.
conio.h supports clrscr(); within Turbo C++ 3.0

@OP: please post any errors that you encounter while compiling, as mentioned earlier.
__________________
Visit: http://www.somaiya.edu
java_roshan is offline   Reply With Quote
Old Jun 25th, 2007, 12:46 PM   #8
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3 pegasus001 is on a distinguished road
Quote:
Originally Posted by java_roshan View Post
conio.h supports clrscr(); within Turbo C++ 3.0
By the way how do you know what compiler is the OP using????
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Jun 25th, 2007, 7:24 PM   #9
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,195
Rep Power: 5 lectricpharaoh will become famous soon enough
You need to decide if you're trying to read from a file, write to a file, or both. As it stands, you're opening the file in read mode:
fp=fopen("raghu.txt","r");
You then try using an output function on this file:
fprintf(fp,"\n HI");
You want to either change the fopen() to use write mode (or read/write), or change the fprintf() to an input function, like fgets() or fscanf() (depending on your needs).

You might want to check this reference if you don't have documentation for the C standard library. It doesn't have everything, but it's got the essential stuff for C and C++.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh 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
problem processing file into a char array csrocker101 C++ 1 May 9th, 2007 12:50 AM
converting string to float beginnerCCC C 22 Oct 3rd, 2006 12:59 AM
OnlineTextEditor.Com! Sane Show Off Your Open Source Projects 43 Jun 16th, 2006 9:55 AM
After execution - Error cannot locate /Skin File? wchar Visual Basic 1 Mar 5th, 2005 10:04 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 5:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:11 AM.

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